COMPUTER
SCIENCE
PYTHON PRACTICAL FILE
12/6/2022
RAVMEET KAUR, GRADE 11th COMMERCE
BEHS Sumati Academy
Page |2
BASIC PROGRAMS
To find the Simple Interest
p=float(input(“enter principle:”))
r=float(input(“enter rate of interest:”))
t=float(input(“enter time:”))
SI=p*r*t
print(“the calculated SI is:”,SI)
To find the area of rectangle
l=float(input(“enter length:”))
b=float(input(“enter breadth:”))
a=l*b
print(“the area of rectangle is:”,a)
To swap two numbers
a=int(input(“enter first number:”))
b=int(input(“enter second number:”))
print(“original numbers:”, a, b)
a, b=b, a
print(“numbers after swapping:”, a, b)
Page |3
LIST PROGRAMS
To reverse a list
lst=[10, 11, 12, 13, 14, 15]
print(“list:”,lst)
lst.reverse()
print("reversed list:", lst)
OUTPUT= list: [10, 11, 12, 13, 14, 15]
reversed list: [15, 14, 13, 12, 11, 10]
To find the length of a list
l=([10, 20, 30])
print(“list:”,l)
print(“the length of list is:”, len(l))
OUTPUT= list: [10, 20, 30]
the length of list is: 3
To find the largest number in a list
l=([10, 80, 37, 99, 29, 200])
print("list:",l)
print(“largest num in the list:”, max(l))
OUTPUT= list: [10, 80, 37, 99, 29, 200]
largest number in the list: 200
Page |4
STRING PROGRAMS
To find the length of a string
s=(“Shah Rukh Khan”)
print(“the string is:”, s)
print(“length of the string is:”, len(s))
OUTPUT= the string is: Shah Rukh Khan
length of the string is: 14
To find the position of a character in a string
s=(“bluebunny”)
letter=(“n”)
print(“the position of the letter ‘n’
is:”, s.rfind(letter))
OUTPUT=the position of the letter ‘n’ is: 7
To check if the string contains only alphabets
or not
str1=(“star”)
str2=(“moon555”)
print(“string1: star”, str1.isalpha())
print(“string2: moon555”, str2.isalpha())
OUTPUT=string1: star True
string2: moon555 False
Page |5
TUPLE PROGRAMS
To find the length of a tuple
t=(‘a’, ‘e’, ‘i’, ‘o’, ‘u’)
print(“the tuple is:”, t)
print(“the length of tuple is:”, len(t))
OUTPUT=the tuple is: ('a', 'e', 'i', 'o', 'u')
the length of tuple is: 5
To find the minimum elements in a tuple
t=(9,7,69,11,11,555)
print("the tuple is:", t)
print("the sorted tuple is:", sorted(t))
OUTPUT= the tuple is: (9, 7, 69, 11, 11, 555)
the sorted tuple is: [7, 9, 11, 11, 69, 555]
To count the frequency of an element in a
tuple
t=(9,7,69,11,11,555)
print(“the tuple is:”, t)
print(“the frequency of ‘11’ in the tuple
is:”, t.count(11))
OUTPUT=the tuple is: (9, 7, 69, 11, 11, 555)
the frequency of '11'in the tuple is: 2
Page |6
DICTIONARY PROGRAMS
To pop an element from a dictionary
d={1:'push-ups',2:'pull-
ups',7:'squats',9:'sit-ups'}
print("original dictionary:",d)
p=d.pop(1)
print("popped out element:",p)
print("new dictionary:",d)
OUTPUT=
original dictionary: {1: 'push-ups', 2:'pull-
ups', 7: 'squats', 9: 'sit-ups'}
popped out element: push-ups
new dictionary: {2: 'pull-ups', 7: 'squats',
9: 'sit-ups'}
To merge two dictionaries
d1={1:'r',2:'a',3:'v'}
d2={4:'m',5:'e',6:'e',7:'t'}
print("dictionary1:",d1)
print("dictionary2:",d2)
d1.update(d2)
print("updated/merged dictionary:",d1)
Page |7
OUTPUT=
dictionary1: {1: 'r', 2: 'a', 3: 'v'}
dictionary2: {4: 'm', 5: 'e', 6: 'e', 7: 't'}
updated/merged dictionary: {1: 'r', 2: 'a', 3:
'v', 4: 'm', 5: 'e', 6: 'e', 7: 't'}
To find the length of a dictionary
d={1:'r',2:'a',3:'v'}
print(“dictionary:”, d)
print(“length of dictionary:”, len(d))
OUTPUT= dictionary: {1: 'r', 2: 'a', 3: 'v'}
length of dictionary: 3