Practical No - 1 WAP in Python To Find The Factorial of A Nu
Practical No - 1 WAP in Python To Find The Factorial of A Nu
def fact(n):
f=1
while(n>0):
f=f*n
n=n-1
return f
if(n<0):
elif(n==0):
print("Factorial of 0 is 1")
else:
factorial=fact(n)
OUTPUT:
Enter a number to find the factorial: 5
Factorial of 5 is = 120
Factorial of 0 is 1
sum=0
for i in range(0,n+1):
sum=sum+x**i
print("Sum of series=",sum)
OUTPUT:
Enter the value of x: 4
Sum of series= 85
Program NO.-3 : Write a Program that generate a set of
prime numbers and another set of odd numbers. Display
the result of union, intersection, difference and
symmetric difference operations
primes=set()
for i in range(2,10):
j=2
f=0
while j<I/2
ifi%j==0:
f=1
j+=1
if f==0:
primes.add(i)
OUTPUT:
Odd Numbers: {9 ,7 ,5 ,3 ,1}
Union: {9 ,7 ,5 ,4 ,3 ,2 ,1}
Intersection: {7 ,5 ,3}
Difference: {9 ,1}
if (a0==2%):
return 1
else:
return 0
if (oddeven(num)==1):
elif (oddeven(num)==0):
OUTPUT:
Enter a number: 7
Enter a number: 6
(b) Write a program to create a mirror of the given string. For example,
“wel” = “lew“.
def rev(str1):
str2=''
i=len(str1-)1
while i>=0:
str+2=str1[i]
i-=1
return str2
print("\n The Mirror image of the given string is: ", rev(word) )
OUTPUT:
Enter a String: school
f=open("abc.txt","r")
line=f.readline()
lc=0
while line:
if line[0]=='s' or line[0]=='S':
print(line,end="")
lc=lc+1
line=f.readline()
f.close()
OUTPUT:
Sam
Sameer
Sanjay
Sunil
importcsv
with open('c:\\pyprg\\player.csv','w') as f:
w = csv.writer(f)
n=1
while (n<=10):
w.writerow([name,score])
n+=1
f.close()
f=open('c:\\pyprg\\player.csv','r')
reader =csv.reader(f)
lst=[]
lst.append(row)
q=0
if searchname in row:
print(row)
q+=1
if(q==0):
f.close()
OUTPUT:
Player Name?:Rohit Sharma Score: 264
import pickle
S={}
f=open('stud.dat','wb')
c='y'
S['RollNo']=rno
S['Name']=name
pickle.dump(S,f)
f.close()
f=open('stud.dat','rb')
K={}
m=0
try:
while True:
K=pickle.load(f)
if K["RollNo"] == rno:
print(K)
m=m+1
except EOFError:
f.close()
if m==0:
import pickle,random
N=[]
f=open("sps.txt","wb")
for i in range(10):
N.append(random.randint(1,40))
pickle.dump(N,f)
f.close()
print("File Created:")
print("Content of File:")
f=open("sps.txt","rb")
data=pickle.load(f)
for i in data:
print(i)
f.close()
OUTPUT:
File Created:
Content of File:
24
14
18
14
26
33
10
33
Program No. - 9: Write a program in Python to create a
CSV file with the details of 5 students.
import csv
f=open("student.csv","w",newline='')
cw=csv.writer(f)
cw.writerow(['Rollno','Name','Marks'])
for i in range(5):
sr=[rollno,name,marks]
cw.writerow(sr)
f.close()
OUTPUT:
Student Record of 1
Enter Marks: 34
Student Record of 2
Enter Marks: 37
Student Record of 3
Enter Marks: 24
Student Record of 4
Enter Roll No. : 28
Enter Marks: 40
Student Record of 5
Enter Marks: 37
import csv
f=open("student.csv","r")
cr=csv.reader(f)
for r in cr:
print(r)
f.close()
OUTPUT:
Content of CSV File:
#Default Parameter
def show(a,b,c=8):
print("Sum=",(a+b+c))
show(5,6)
show(5,6,10)
def show1(a,b=9,c=8):
print("Sum1=",(a+b+c))
show1(5)
show1(5,6)
show1(5,6,10)
#Positional Parameter
def show2(a,b):
print("Sub=",(a-b))
show2(15,6)
show2(6,15)
OUTPUT:
Sum= 19
Sum= 21
Sum1= 22
Sum1= 19
Sum1= 21
Sub= 9
Sub= -9
data=f.read()
V=['A','E','I','O','U','a','e','i','o','u']
C=['B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X', \
'Y','Z','b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w', \ 'x','y','z']
cv=0
cc=0
for i in data:
if i in V:
cv=cv+1
elif i in C:
cc=cc+1
ct=0
for i in data:
ct=ct+1
f.close()
OUTPUT:
Number of Vowels in the file= 184
data=f.read()
cu=0
cl=0
for i in data:
if i.isupper():
cu=cu+1
elif i.islower():
cl=cl+1
ct=0
for i in data:
ct=ct+1
f.close()
OUTPUT:
Number of Uppercase letters in the file= 139
S={}
f=open('stud.dat','wb')
c='y'
S['RollNo']=rno S['Name']=name
S['Marks']=marks
pickle.dump(S,f)
f.close()
f=open('stud.dat','rb+')
f.seek(0,0)
m=0
try:
while True:
pos=f.tell()
S=pickle.load(f)
if S["RollNo"] == rno:
f.seek(pos)
S["Marks"]=marks
pickle.dump(S,f)
m=m+1
except EOFError:
f.close()
if m==0:
else:
f=open('stud.dat','rb')
try:
while True:
S=pickle.load(f)
print(S)
except EOFError:
f.close()
OUTPUT:
Enter the roll no. of the student : 1
E={}
f=open('emp.dat','wb')
c='y'
while c=='y' or c=='Y':
E['Emp_Id']=eid
E['Emp_Name']=ename
E['Salary']=salary
pickle.dump(E,f)
f.close()
f=open('emp.dat','rb+')
f.seek(0,0)
m=0
try:
while True:
pos=f.tell()
E=pickle.load(f)
if E["Emp_Id"] == eid:
f.seek(pos)
E["Salary"]=salary
pickle.dump(E,f)
m=m+1
except EOFError:
f.close()
if m==0:
else:
f=open('emp.dat','rb')
try:
while True:
E=pickle.load(f)
print(E)
except EOFError:
f.close()
OUTPUT:
----Python-SQL Connectivity-----
Program No - 1: Write a menu driven program to
demonstrate add, display, update, delete and exit.
Performed on a table club containing (club_id, cname,
city) through python-MySql connectivity.
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("use spsharmag")
mycursor.execute("create table if not exists club (cid int primary key, cname
varchar(20),city varchar(20))")
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
con.commit()
elif(choice==2):
myclubs=mycursor.fetchall()
for x in myclubs:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
cid=int(input("Enter the club id for delete: "))
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
1. Press 1 for add new club:
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("use spsharmag")
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
con.commit()
elif(choice==2):
mybooks=mycursor.fetchall()
for x in mybooks:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("use spsharmag")
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
con.commit()
elif(choice==2):
myproducts=mycursor.fetchall()
for x in myproducts:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
Press 1 for add new product:
Enter product Price: 600 Press 'y' for continue and 'n' for exit: y
import sqlite3
connection = sqlite3.connect("info.db")
cursor = connection.cursor()
for i in range(10):
n =len(who)
for i in range(n):
print("Displaying All the Records From student Table in Descending order of age")
print (*cursor.fetchall(),sep='\n' )
OUTPUT:
Enter 10 students names and their ages respectively:
Enter Name:Annamalai
Enter Age:17
Enter Age:23
Enter Name:Kumaran
Enter Age:30
Enter Name:Sivasakthiya
Enter Age:28
Enter Name:Leena
Enter Age:45
Enter Name:Meena
Enter Age:65
Enter Name:Kamalakannan
Enter Age:35
Enter Name:Sowmyaa
Enter Age:20
Enter Name:Ramaa
Enter Age:70
Enter Name:Melvin
Enter Age:35 Displaying All the Records From student Table in Descending order of
age
('Ramaa', 70)
('Meena', 65)
('Leena', 45)
('Kamalakannan', 35)
('Melvin', 35)
('Kumaran', 30)
('Sivasakthiya', 28)
('Sowmyaa', 20)
('Annamalai', 17)