PRACTICAL
PROGRAMS
PYTHON REVISION 1 & 2
1.To write a menu driven Python Program to perform
Arithmetic
operations (+, -, *, /).
while True:
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
ch=int(input("Enter your choice:"))
a=int(input("Enter the First Number:"))
b=int(input("Enter the Second Number:"))
if ch==1:
print("The Addition value is:",a+b)
elif ch==2:
print("The Subtraction value is:",a-b)
elif ch==3:
print("The Multiplication value is:",a*b)
elif ch==4:
print("The Division value is:",a/b)
else:
print("Invalid option")
break
2.To write a Python Program to display the
multiplication table of thegiven number.
num = int(input("Display multiplication table of:"))
for i in range(1, 11):
print(num, 'x', i, '=', num*i)
3 To write a python program to pass list to a
function and double
the odd values and half even values of a list
and display list element
after changing.
def test(x):
new=[]
for i in x:
if i%2==0:
new.append(i//2)
else:
new.append(i*2)
return new
org=eval(input("enter the list u want to process"))
print("the original list is",org)
new=test(org)
print("the new list is",new)
4.ODD AND EVEN NUMBERS COUNT IN TUPLE.
def CountOfEO(t):
EC = OC = 0
for i in t:
if(i % 2 == 0):
EC = EC + 1
else:
OC = OC + 1
return EC,OC
t=eval(input("Enter the tuple"))
print("Tuple Items are = ", t)
eCount, oCount = CountOfEO(t)
print("The Count of Even Numbers in t = ", eCount)
print("The Count of Odd Numbers in t = ", oCount)
5.VOWELS COUNTING
To write a Python program to pass a string to a
function and count how
many vowels present in the string.
def vowels(s):
v=0
for i in s:
if i in "aeiouAEIOU":
v=v+1
return v
s=input("Enter String:")
print("Number of vowels is string is:", vowels(s))
6.RANDOM NUMBER GENERATOR
Aim: - To write a Python program to generator
(Random Number) that
generates random numbers between 1 and 6
(simulates a dice) using
user defined function.
import random
while True:
ch=input("Enter (r) for roll dice or press
any other key to quit")
if (ch != ' r ' ):
break
n=random.randint(1,6)
print(n)
7.To write a Python program to function with
key and value, and update value at that key in
dictionary entered by user.
myDict = {}
while True:
choice=str(input("do u want to continue?"))
if choice=="yes":
key = input("Please enter the Key : ")
value = input("Please enter the Value : ")
myDict[key] = value
print("\nUpdated Dictionary = ", myDict)
8. MATHEMATICAL FUNCTIONS
Aim: - To write a python program to implement
python string functions.
import math
print("The sin value of 90 is:",math.sin(90))
print("The cos value of 45 is:",m.cos(45))
print("The tan value of 90 is:",m.tan(90))
print("The square root value of 81 is:",m.sqrt(81))
print("The power value of 2 power 3 is:",m.pow(2,3))
print("The ceil value of 1.3 is:",m.ceil(1.3))
print("The floor value of 1.5 is:",m.floor(1.5))
print("The absolute value of -5 is:",m.fabs(-5))
print("The factorial value of 4 is:",m.factorial(4))
9. STRING FUNCTIONS
Aim: - To write a python program to implement
python string functions.
s1="Hello India"
s2="Welcome India"
print("The Capitalize functions is:",s1.capitalize())
print("The title function is:",s2.title())
print("The lower function is:",s1.lower())
print("The Upper function is:",s2.upper())
print("The Swapcase function is:",s1.swapcase())
print("The Count function is:",s2.count('e'))
print("The find function is:",s2.find('e'))
print("The isalpha function is:",s1.isalpha())
print("The isdigit function is:",s2.isdigit())
print("The replace function
is:",s2.replace("Welcome","My"))
10. DISPLAY EACH WORD SEPARATED BY #
Aim:- To write a python program to read and
display file content line by
line with each word separated by #.
f=open("C:\\Users\\ADMIN\\Desktop\\rinarina.txt","
r")
data=f.read()
word=data.split()
for i in word:
print(i,end="#")
f.close()
11.
f=open("C:\\Users\\ADMIN\\Desktop\\sample.txt","r")
g=open("C:\\Users\\ADMIN\\Desktop\\x.txt","w")
h=open("C:\\Users\\ADMIN\\Desktop\\y.txt","w")
data=f.readlines()
for i in data:
if "a" in i:
g.write(i)
else:
h.write(i)
f.close()
g.close()
h.close()
f=open("C:\\Users\\ADMIN\\Desktop\\others.txt","w")
g=open("C:\\Users\\ADMIN\\Desktop\\lower.txt","w")
h=open("C:\\Users\\ADMIN\\Desktop\\upper.txt","w")
while True:
data=str(input("enter a string"))
if data=="quit":
break
for i in data:
if i.islower():
g.write(i)
elif i.isupper():
h.write(i)
else:
f.write(i)
f.close()
g.close()
h.close()
import pickle
f=open("C:\\Users\\ADMIN\\Desktop\\rin.dat","wb")
stud={}
n=int(input("enter the number of students"))
for i in range(n):
rollno=int(input("enter your number"))
name=str(input("enter your name"))
stud[rollno]=name
pickle.dump(stud,f)
f.close()
import pickle
f=open("C:\\Users\\ADMIN\\Desktop\\rin.dat","rb")
search=int(input("enter your number to search"))
data=pickle.load(f)
for i in data:
if i==search:
print(stud[i])
f.close()
import pickle
f=open("C:\\Users\\ADMIN\\Desktop\\rin.dat","wb")
stud={}
n=int(input("enter the number of students"))
for i in range(n):
rollno=int(input("enter your number"))
name=str(input("enter your name"))
marks=str(input("enter your marks"))
stud[rollno]={}
stud[rollno]["name"]=name
stud[rollno]["marks"]=marks
print(stud)
pickle.dump(stud,f)
f.close()
import pickle
f=open("C:\\Users\\ADMIN\\Desktop\\rin.dat","rb+")
search=int(input("enter your number to update"))
newmark=int(input("enter your updated marks"))
data=pickle.load(f)
for i in data:
if i==search:
stud[rollno]["marks"]=newmark
print(stud)
f.close()
import csv
f = open("C:\\Users\\ADMIN\\Desktop\\rin.csv","w", newline="")
writer = csv.writer(f)
heading=["EmpNo", "Name", "Marks"]
writer.writerow(heading)
n = int(input("Enter the number of students: "))
for i in range(n):
empno = int(input("Enter your number: "))
name = input("Enter your name: ")
marks = input("Enter your marks: ")
data=[empno, name, marks]
writer.writerow(data)
f.close()
import csv
f = open("C:\\Users\\ADMIN\\Desktop\\stud.csv","w", newline="")
writer = csv.writer(f)
n = int(input("Enter the number of students: "))
for i in range(n):
rollno = int(input("Enter your stud number: "))
name = input("Enter your stud name: ")
marks = int(input("Enter your stuf marks "))
data=[rollno, name, marks]
writer.writerow(data)
f.close()
f = open("C:\\Users\\ADMIN\\Desktop\\stud.csv","r", newline="")
csvreader = csv.reader(f)
n = int(input("Enter the emo num to search "))
for i in csvreader:
print("student number",i[0])
print("student name", i[1])
print("student marks",i[2])
f.close()
import csv
f = open("C:\\Users\\ADMIN\\Desktop\\rin.csv","w", newline="")
writer = csv.writer(f)
heading=["userid", "pwd"]
writer.writerow(heading)
while True:
choice= str(input("do u want to continue"))
if choice=="y":
uname = str(input("Enter your uname "))
pwd= input("Enter your pws ")
data=[uname,pwd]
writer.writerow(data)
else:
break
f.close()
f = open("C:\\Users\\ADMIN\\Desktop\\rin.csv","r", newline="")
reader = csv.reader(f)
uname=str(input("enter the username u want to search for "))
for i in reader:
if i[0]==uname:
print("hello your password is",i[1])
f.close()