PYTHON CODING
AND
C PROGRAMMING
EXAMPLES
PROGRAMMING FOR BEGINNERS
J KING
ASCII value of character in Python
In python, we use the function ord() to obtain an ASCII code of a character. Ord() accepts a character, returns its ASCII value.
Syntax:
ord(character);
Example:
Input:
char_var = 'A'
Function call:
ord(char_var)
Output:
Code to find ASCII value of a character
# python program to print ASCII
# value of a given character
# Assigning character to a variable
char_var = 'A'
# printing ASCII code
print("ASCII value of " + char_var + " is = ", ord(char_var))
char_var = 'x'
# printing ASCII code
print("ASCII value of " + char_var + " is = ", ord(char_var))
char_var = '9'
# printing ASCII code
print("ASCII value of " + char_var + " is = ", ord(char_var))
Output
ASCII value of A is = 65
ASCII value of x is = 120
ASCII value of 9 is = 57
Calculate simple interest
We use the following formula to calculate simple interest,
(P * R * T) / 100
Where,
- P Principle amount
- R Rate of the interest, and
- T Time in the years
Example:
Input:
p = 250000
r = 36
t = 1
# formula
si = (p*r*t)/100
print(si)
Output:
90000
Python program for simple interest
# Python program to find simple interest
p = float(input("Enter the principle amount : "))
r = float(input("Enter the rate of interest : "))
t = float(input("Enter the time in the years: "))
# calculating simple interest
si = (p*r*t)/100
# printing the values
print("Principle amount: ", p)
print("Interest rate : ", r)
print("Time in years : ", t)
print("Simple Interest : ", si)
Output
First run:
Enter the principle amount : 10000
Enter the rate of interest : 3.5
Enter the time in the years: 1
Principle amount: 10000.0
Interest rate : 3.5
Time in years : 1.0
Simple Interest : 350.0
Second run:
Enter the principle amount : 250000
Enter the rate of interest : 36
Enter the time in the years: 1
Principle amount: 250000.0
Interest rate : 36.0
Time in years : 1.0
Simple Interest : 90000.0
Calculate compound interest
We use the equation below to calculate compound interest
P(1 + R / 100) T
Where,
P Principle amount
R Rate of the interest, and
T Time in the years
Example:
Input:
p = 250000
r = 36
t = 1
# formula
ci = p * (pow((1 + r / 100), t))
print(ci)
Output:
339999.99999999994
Python program
# Python program to find compound interest
p = float(input("Enter the principle amount : "))
r = float(input("Enter the rate of interest : "))
t = float(input("Enter the time in the years: "))
# calculating compound interest
ci = p * (pow((1 + r / 100), t))
# printing the values
print("Principle amount : ", p)
print("Interest rate : ", r)
print("Time in years : ", t)
print("compound Interest : ", ci)
Output
First run:
Enter the principle amount : 10000
Enter the rate of interest : 3.5
Enter the time in the years: 1
Principle amount : 10000.0
Interest rate : 3.5
Time in years : 1.0
compound Interest : 10350.0
Second run:
Enter the principle amount : 250000
Enter the rate of interest : 36
Enter the time in the years: 1
Principle amount : 250000.0
Interest rate : 36.0
Time in years : 1.0
compound Interest : 339999.99999999994
Python program to check the given year is a leap year or not
A leap year is a year that can be divided by 4, except for the century year (a year that ends with 00). A year of a century is a leap year if it is divisible by 400. Here, the user presents a year, and we will test if the year in question is a leap year or not. We can solve this problem in two ways first by using the calendar module, and second by simply checking the state of the leap year.
1) By using the calendar module
Initially we learn a little about the calendar module before we go to solve the problem. Calendar module is built into Python which provides us with various functions to solve the date, month and year related problem.
PROGRAM
# importing the module
import calendar
# input the year
year=int(input('Enter the value of year: '))
leap_year=calendar.isleap(year)
# checking leap year
if leap_year: # to check condition
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')
Output
RUN 1:
Enter the value of year: 2020
The given year is a leap year.
RUN 2:
Enter the value of year: 2021
The given year is a non-leap year.
2) By simply checking method
As we know it is a leap year, or not, to test the year in question. So, we'll introduce the condition here and try to write the Python programme.
Program
# input the year
y=int(input('Enter the value of year: '))
# To check for non century year
if y%400==0 or y%4==0 and y%100!=0:
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')
Output
RUN 1:
Enter the value of year: 2020
The given year is a leap year.
RUN 2:
Enter the value of year: 2000
The given year is a leap year.
Python | Some of the examples of simple if else
Example1: Enter a number and check whether it is 10 or not
a=int(input("Enter A : "))
if a==10:
print("Equal to 10")
else:
print("Not Equal to 10")
Output
Enter A : 10
Equal to 10
Example2: Find which is largest of two numbers
a=int(input("Enter A: "))
b=int(input("Enter B: "))
if a>b:
g=a
else:
g=b
print("Greater = ",g)
Output
Enter A: 36
Enter B: 24
Greater = 36
Example3: Find which is largest of two numbers using single statement
a=int(input("Enter A: "))
b=int(input("Enter B: "))
c= a if a>b else b
print("Greater = ",c)
Output
Enter A: 24
Enter B: 36
Greater = 36
Calculate discount based on the sale amount in Python
The discount rates are:
Amount Discount
0-5000 5%
5000-15000 12%
15000-25000 20%
above 25000 30%
Program:
# input sale amount
amt = int(input("Enter Sale Amount: "))
# checking conditions and calculating discount
if(amt>0):
if amt<=5000:
disc = amt*0.05
elif amt<=15000:
disc=amt*0.12
elif amt<=25000:
disc=0.2 * amt
else:
disc=0.3 * amt
print("Discount : ",disc)
print("Net Pay : ",amt-disc)