Rishabh Singh 2
Rishabh Singh 2
Rishabh Singh 2
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:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
Factorial of 5 is = 120
>>>
Factorial of 0 is 1
>>>
>>>
#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:
>>>
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
Sum= 19
Sum= 21
Sum1= 22
Sum1= 19
Sum1= 21
Sub= 9
Sub= -9
>>>
1+x+x^2+x^3+ ----------------x^n
print("Sum of series=",sum)
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
Sum of series= 85
Practical No - 4: WAP in Python to read a text file and print the number of
vowels and consonants in the file.
f=open("Vowel.txt","r")
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
print("Number of Vowels in the file=",cv)
print("Number of Consonants in the file=",cc)
print("Number of Total Chars in the file=",ct)
f.close()
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
Practical No - 5: WAP in Python to read a text file and print the line or paragraph
starting with the letter ‘S’
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:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
Sam
Sameer
Sanjay
Sunil
Practical No - 6: WAP in Python to read a text file and print the number of
uppercase and lowercase letters in the file.
f=open("Vowel.txt","r")
data=f.read()
U=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', \
6 S P SHARMA SIR S P SHARMA CLASSES
CS Practical File Session 2021-22 Term 2 CLASS 12
'S','T','U','V','W','X','Y','Z',]
L=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r', \
's','t','u','v','w','x','y','z']
cu=0
cl=0
for i in data:
if i in U:
cu=cu+1
elif i in L:
cl=cl+1
ct=0
for i in data:
ct=ct+1
print("Number of Uppercase letters in the file=",cu)
print("Number of Lowercase Letters in the file=",cl)
print("Number of Total Chars in the file=",ct)
f.close()
'''
Practical No. 6: WAP in Python to read a text file and print the number of
uppercase and lowercase letters in the file
'''
f=open("Vowel.txt","r")
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
print("Number of Uppercase letters in the file=",cu)
print("Number of Lowercase Letters in the file=",cl)
print("Number of Total Chars in the file=",ct)
f.close()
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
Practical No - 7: WAP in Python to create a binary file with name and roll
number of the students. Search for a given roll number and display the name of
student.
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:
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
Practical No - 8: Create a binary file with roll_no, name and marks of some
students and update the marks of specific student.
import pickle
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:
Practical No - 9: Create a binary file with eid, ename and salary and update the
salary of the employee.
import pickle
E={}
f=open('emp.dat','wb')
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:
>>>
Practical No - 10: Create a text file and remove the lines from the file which
contains letter ‘K’
import sys
f=open("sps.txt","w+")
data = sys.stdin.readlines()
for i in data:
f.write(i)
f.close()
print("**********")
f=open("sps.txt","r")
data=f.read()
print(data)
f.close()
f=open("sps.txt","r+")
data=f.readlines()
f.seek(0)
for i in data:
if "K" not in i:
f.write(i)
f.truncate()
f.close()
print("**********")
f=open("sps.txt","r")
data=f.read()
print(data)
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
S P SHARMA CLASSES
HELLO INDIA
KARAN
SAM
RAM
KASHMIR
DELHI
**********
Content of File:
S P SHARMA CLASSES
HELLO INDIA
KARAN
SAM
RAM
KASHMIR
DELHI
**********
S P SHARMA CLASSES
HELLO INDIA
SAM
RAM
DELHI
>>>
Practical No - 11: Create a binary file with 10 random numbers from 1 to 40 and
print those numbers.
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:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
File Created:
Content of File:
24
14
18
14
26
33
10
33
Practical No - 12: 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:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
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 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:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
>>>
Practical No - 14: Write a menu driven program which insert, delete and display
the details of an employee such as eid, ename and salary using Stack.
Employee=[]
c='y'
while(c=="y" or c=="Y"):
print("1: Add Employee Detail: ")
print("2: Delete Employee Detail: ")
print("3: Display Employee Detail: ")
choice=int(input("Enter your choice: "))
if(choice==1):
eid=int(input("Enter Employee Id: "))
ename=input("Enter Employee Name: ")
salary=float(input("Enter Employee Salary: "))
emp=(eid,ename,salary)
Employee.append(emp)
elif(choice==2):
if(Employee==[]):
print("Stack Empty")
else:
print("Deleted element is: ",Employee.pop())
elif(choice==3):
L=len(Employee)
while(L>0):
print(Employee[L-1])
L=L-1
else:
print("Wrong Input")
c=input("Do you want to continue? Press 'y' to Continue: ")
OUTPUT:
>>>
Practical No - 15: Write a menu driven program which insert, delete and display
the details of a book such as book_id, book_name and price using Stack.
Book=[]
c='y'
while(c=='y' or c=='Y'):
if(choice==1):
B=(book_id,book_name,price)
Book.append(B)
elif(choice==2):
if(Book==[]):
print("Stack Empty")
else:
elif(choice==3):
L=len(Book)
while(L>0):
print(Book [L-1])
L=L-1
else:
print("Wrong Input")
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
>>>
Practical No - 16: Write a menu driven program which insert, delete and display
the details of a student such as roll_no, sname and course using Stack.
Student=[]
c='y'
while(c=='y' or c=='Y'):
if(choice==1):
Student.append(stu)
elif(choice==2):
if(Student==[]):
print("Stack Empty")
else:
elif(choice==3):
L=len(Student)
while(L>0):
print(Student[L-1])
L=L-1
else:
print("wrong input")
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
>>>
Practical No - 17: Write a menu driven program which insert, delete and display
the details of a movie such as movie_id, mname and rating using Stack.
movie=[]
c='y'
while(c=='y' or c=='Y'):
if(Choice==1):
mov= (mid,mname,rating)
movie.append(mov)
elif(Choice==2):
if(movie==[]):
print("stack empty")
else:
elif(Choice==3):
L=len(movie)
while(L>0):
print(movie[L-1])
L=L-1
else:
print("wrong input")
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
Practical No - 18: Write a menu driven program which insert, delete and display
the details of a product such as pid, pname and price using Stack.
Product=[ ]
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
Prd=(pid,pname,price)
Product.append(Prd)
elif(choice==2):
if(Product==[ ]):
print("Stack Empty")
else:
elif(choice==3):
L=len(Product)
while(L>0):
print(Product[L-1])
L=L-1
else:
print("Wrong input")
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
>>>
Practical No - 19: Write a menu driven program which insert, delete and display
the details of a club such as club_id, cname and city using Stack.
Club= []
c='y'
if (choice == 1):
clu = (cid,cname,city)
Club.append(clu)
elif (choice==2):
if(Club == []):
else:
L = len(Club)
while (L>0):
print(Club[L-1])
L=L-1
else:
print("Wrong Input")
OUTPUT:
39 S P SHARMA SIR S P SHARMA CLASSES
CS Practical File Session 2021-22 Term 2 CLASS 12
Enter choice: 1
Enter choice: 1
Enter choice: 1
Enter choice: 3
Enter choice: 2
Enter choice: 3
>>>
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 book (bid int primary key,bname varchar(20),bprice
float(5,2))")
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:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
>>>
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):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
>>>
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 Student (sid int primary key, sname varchar(20),
course varchar(20))")
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
con.commit()
elif(choice==2):
mystudents=mycursor.fetchall()
for x in mystudents:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
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):
mymovies=mycursor.fetchall()
for x in mymovies:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
= RESTART: C:\Users\S P SHARMA\AppData\Local\Programs\Python\Python38-32\P-4.py
>>>
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 Employee (eid int primary key, ename varchar(20),
salary float(8,2))")
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
con.commit()
elif(choice==2):
myemp=mycursor.fetchall()
for x in myemp:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32
>>>
>>>
Q – 1: Create a database.
Syntax:
Show databases;
Q – 2: To create a student table with the student id, class, section, gender,
name, dob, and marks as attributes where the student id is the primary key.
Ans:
Primary Key:
Primary key is a constraint of an attribute which cannot be NULL or duplicate.
Only one primary key is allowed in a table.
Syntax:
74 S P SHARMA SIR S P SHARMA CLASSES
CS Practical File Session 2021-22 Term 2 CLASS 12
To delete the specific record from the table use delete command/query.
Syntax:
Note: If we don’t use where clause It delete all the records one by one from the
table.
Q – 5: To increase the marks by 5% for those students who are scoring marks
more than 30.
Update:
Update is a SQL query used to update/change/modify the record of a table.
Syntax
Q – 7: To display student_id, name and marks of those students who are scoring
marks more than 30.
Aggregate Function:
Q – 10: To add a new column email in the student table with appropriate data
type.
Q – 11: To add the email id’s of each student in the previously created email
column.
Q – 12: To display the information of all the students, whose name starts with
‘S’
Q – 13: To display the student_id, name, dob of those students who are born
between ‘2005-01-01’ and ‘2005-12-31’.
Q – 14: To display the student_id, name, dob, marks, email of male students in
ascending order of their name.
Q – 15: To display the student_id, gender, name, dob, marks, email of students
in descending order of their marks.
Q – 16: To display the unique section name from the student table.
Q – 17: Create a student table with student id, name and marks as attribute,
where the student id is the primary key.
Q –20: Use the Select command to get the details of the students with marks more than 30.