Example Programs 11
Example Programs 11
expnumbers1.py
import math
n1=int(input("Enter first number"))
n2=eval(input("Enter second number"))
# The eval() method parses the expression passed to it and runs python expression(code) within the
program.
x=4
y=64
a=abs(n2)
c=math.ceil(n2)
#cm=cmp(n1,n2)
e=math.exp(x)
f=math.fabs(n2)
fl=math.floor(n2)
l=math.log(x)
lt=math.log10(x)
ma=max(1,2,3,4,5,6,7,8,9)
mi=min(0,1,2,3,4,5,6,-7,-10)
mo=math.modf(n1)
mo2=math.modf(n2)
p=pow(n1,x)
r=round(n2,3)
s=math.sqrt(y)
print("absolute value of %f is %d"%(n2,a))
print("ceil value of %f is %d"%(n2,c))
#print("compared value of %d,%f is %d"%(n1,n2,cm))
print("exponent value of %d is %d"%(x,e))
print("absolute value of %f is %d"%(n2,f))
print("floor of %f is %d"%(n2,fl))
print("Natural logarithm of %d is %d"%(x,l))
print("base 10 logarithm of %d is %d"%(x,lt))
print("maximum of given numbers is %d"%ma)
print("minimum of given numbers is %d"%mi)
print("modulo of n1 is %d",mo)
print("modulo of n2 is %d",mo2)
print(" %d power of %d is %d"%(n1,x,p))
print("rounding of %f is %d"%(n2,r))
print("square root of %d is %d"%(y,s))
OUTPUT:
Enter first number10
Enter second number20.45
absolute value of 20.450000 is 20
ceil value of 20.450000 is 21
exponent value of 4 is 54
absolute value of 20.450000 is 20
floor of 20.450000 is 20
Natural logarithm of 4 is 1
base 10 logarithm of 4 is 0
maximum of given numbers is 9
minimum of given numbers is -10
modulo of n1 is %d (0.0, 10.0)
modulo of n2 is %d (0.4499999999999993, 20.0)
10 power of 4 is 10000
rounding of 20.450000 is 20
square root of 64 is 8
##STRINGS EXAMPLE
##STRINGS EXAMPLE
expstrings1.py
from codecs import encode
import base64
s1=input("Enter first string in lower case: ")
s2=input("Enter second string: ")
s3 = input("Enter a string with some numberic value: ")
s4=input("Enter a string in upper case: ")
s5=input("Enter a string with first letter capitalized: ")#like 'This Is String Example....Wow!!'
s = "-"
seq = ("a", "b", "c") # This is sequence of strings.
print("Capitalized form of %s is %s"%(s1,s1.capitalize()))
print ("s1.center(40, 'a') : ", s1.center(40, 'a'))
sub = 'i'
print ("s1.count('exam', 10, 40) : ", s1.count(sub,10,40))
suffix='!!'
print (s1.endswith(suffix))
print (s1.endswith(suffix,20))
suffix='exam'
print (s1.endswith(suffix))
print (s1.endswith(suffix, 0, 19))
print (s1.find(s2))
print (s1.find(s2, 10))
print (s1.find(s2, 40))
print ("s3 is isalnum: ",s3.isalnum())
s3 is isalpha: True
s2 is isalpha: True
s1 is isdigit: False
s4 is islower: False
s1 is islower: True
s4 is isupper: True
s1 is isupper: False
s5 is istitle: True
s1 is istitle: False
OUTPUT
Capitalized form of this is string example....wow!! is This is string example....wow!!
True
True
False
True
15
15
-1
s3 is isalnum: True
s3 is isalpha: True
s2 is isalpha: True
s1 is isdigit: False
s4 is islower: False
s1 is islower: True
s4 is isupper: True
s1 is isupper: False
s5 is istitle: True
s1 is istitle: False
str="Hello World"
##TUPLES EXAMPLE
exptuples1.py
tuple1 = ('physics', 'chemistry')
tuple2 = (1, 2, 3, 4, 5, 6, 7 )
list1= ['maths', 'che', 'phy', 'bio']
#print(cmp(tuple1, tuple2)) #cmp() has been depricated from python3
print ("Length of First tuple: ", len(tuple1))
print ("Length ofSecond tuple : ", len(tuple2))
print ("Max value element : ", max(tuple1))
print ("Max value element : ", max(tuple2))
print ("min value element : ", min(tuple1))
print ("min value element : ", min(tuple2))
tuple3=tuple(list1)
print ("tuple3 elements : ", tuple3)
OUTPUT
Length of First tuple: 2
Length ofSecond tuple : 7
Max value element : physics
Max value element : 7
min value element : chemistry
min value element : 1
tuple3 elements : ('maths', 'che', 'phy', 'bio')
##IF
firstif.py
v1=input("Enter the value of v1: ")
if(int(v1)==10):
print("the value of v1 is: ",v1)
print("in first if")
v2=input("Enter the value of v2: ")
if(int(v2)==100):
print("the value of v2 is: ",v2)
print("in second if")
OUTPUT:
>>>
Enter the value of v1: 10
the value of v1 is: 10
in first if
Enter the value of v2: 100
the value of v2 is: 100
in second if
##1. IF ELSE
exnested.py
a=input("Enter the value for a: ")
b=input("Enter the value for b: ")
c=input("Enter the value for c: ")
if(a>b):
if(a>c):
print("a is larger than b and c")
else:
print("c is larger than a and b")
else:
if(b>c):
print("b is larger than a and c")
else:
print("c is larger a and b")
OUTPUT:
Enter the value for a: 3
Enter the value for b: 4
Enter the value for c: 5
c is larger a and b
>>>
Enter the value for a: 0
Enter the value for b: 6
Enter the value for c: 2
b is larger than a and c
##2. IF ELSE
nestedifex.py
age = int(input(" Please Enter Your Age Here: "))
if age < 18:
print(" You are Minor ")
print(" You are not Eligible to Work ")
else:
if age >= 18 and age <= 60:
print(" You are Eligible to Work ")
print(" Please fill in your details and apply")
else:
print(" You are too old to work as per the Government rules")
print(" Please Collect your pension!")
OUTPUT:
>>>
Please Enter Your Age Here: 13
You are Minor
You are not Eligible to Work
>>>
Please Enter Your Age Here: 20
You are Eligible to Work
Please fill in your details and apply
>>>
Please Enter Your Age Here: 65
You are too old to work as per the Government rules
Please Collect your pension!
##IF..ELIF..ELSE
fourif.py
a=input("Enter value for a: ")
if(int(a)<20):
print("a is less than 20")
if(int(a)==15):
print("a is 15")
elif(int(a)==10):
print("a is 10")
elif(int(a)==5):
print("a is 5")
elif(int(a)<5):
print("a is less than 5")
else:
print("a is greater than 15 and less than 20")
else:
print("a is greater than 20")
OUTPUT:>>> Enter value for a: 15
a is less than 20
a is 15
>>>
Enter value for a: 10
a is less than 20
a is 10>>>
Enter value for a: 5
a is less than 20
a is 5
>>>
Enter value for a: 22
a is greater than 20
## 1.FOR
factorial.py
a=[1,2,3,4,5]
#n=int(input("Enter the value of n"))
for i in range(5):
#a.append(input("Enter the %d item:"%i))
#for i in range(n):
f=1
for fact in range(1,a[i]):
f*=fact
fact+=1
print("factorial is %d is %d:"%(i,f))
OUTPUT:>>> factorial is 0 is 1
factorial is 1 is 1
factorial is 2 is 2
factorial is 3 is 6
factorial is 4 is 24
OR
OUTPUT
##2.FOR
firstfor.py
for letter in 'PYTHON':
print("Current letter: ",letter)
fruits=['banana','apple','grapes','mango','melon']
for fruit in fruits:
print("current fruit: ",fruit)
OUTPUT:
>>>
Current letter: P
Current letter: Y
Current letter: T
Current letter: H
Current letter: O
Current letter: N
current fruit: banana
current fruit: apple
current fruit: grapes
current fruit: mango
current fruit: melon
##FOR ELSE
##1.WHILE
firstwhile.py
count=0
while(count<5):
print(count,"is less than 5")
count+=1
else:
print(count,"is not less than 5")
OUTPUT:
>>>
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
## WHILE ELSE
secondwhile.py
reversedNumber = 0
n=input("Enter an integer: ")
while(int(n)>0):
remainder =int(n)%10
reversedNumber = reversedNumber*10 + remainder
n = int(n)/10;
print("Reversed Number = ", reversedNumber)
else:
print("reverse of a number is not possible")
OUTPUT:
nestediffun.py
# menu() function prints the main menu, and prompts for a choice
def menu():
#print what options you have
print("Welcome to calculator in Python")
print("your options are:")
print(" ")
print("1) Addition")
print("2) Subtraction")
print("3) Multiplication")
print("4) Division")
print("5) Quit calculator")
print(" ")
return int(input("Choose your option: "))
OUTPUT:
Welcome to calculator in Python
your options are:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
Expdict1.py
d1={}
n=int(input("Enter the size of dictionary"))
for i in range(n):
key=input("Enter the key to be added:")#d1 = {'Name': 'Zara', 'Age': 7}
value=input("Enter the value for the key to be added:")
d1.update({key:value})
print("dictionary d1 is:")
print(d1)
print("Length of d1 is: ",len(d1))
print("Equivalent String of d1: %s" % str (d1))
d3=d1.copy()
print("New dictionary after copied: %s"%str(d3))
d5={'gen':'female'}
d1.update(d5)
print("The updated d1 is : %s" % d1)
OUTPUT