0% found this document useful (0 votes)
15 views36 pages

Practical No - 1 WAP in Python To Find The Factorial of A Nu

The document contains a series of Python programs demonstrating various programming concepts such as calculating factorials, summing series, handling prime and odd numbers, file operations, and using functions. Each program includes code snippets, expected outputs, and user interactions for tasks like reading from and writing to files, performing mathematical calculations, and manipulating data structures. The programs cover a range of topics suitable for beginners to intermediate Python learners.

Uploaded by

singhanand08673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views36 pages

Practical No - 1 WAP in Python To Find The Factorial of A Nu

The document contains a series of Python programs demonstrating various programming concepts such as calculating factorials, summing series, handling prime and odd numbers, file operations, and using functions. Each program includes code snippets, expected outputs, and user interactions for tasks like reading from and writing to files, performing mathematical calculations, and manipulating data structures. The programs cover a range of topics suitable for beginners to intermediate Python learners.

Uploaded by

singhanand08673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

-----Python Prpgram-----

Program No. - 1: WAP in Python to find the factorial of a


number using function.

def fact(n):

f=1

while(n>0):

f=f*n

n=n-1

return f

n=int(input("Enter a number to find the factorial: "))

if(n<0):

print("Factorial of -ive number is not possible")

elif(n==0):

print("Factorial of 0 is 1")

else:

factorial=fact(n)

print("Factorial of ",n," is =",factorial)

OUTPUT:
Enter a number to find the factorial: 5

Factorial of 5 is = 120

Enter a number to find the factorial: 0

Factorial of 0 is 1

Enter a number to find the factorial: -5

Factorial of -ive number is not possible


Program No. - 2: Write a program in Python to input the
value of x and n and print the sum of the following series
1+x+x^2+x^3+ ----------------x^n

x=int(input("Enter the value of x: "))

n=int(input("Enter the value of n: "))

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

Enter the value of n: 3

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

odd=set([x*1+2 for x in range(0,5)])

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)

print("Odd Numbers: ", odd)

print("Prime Numbers: ", primes)

print("Union: ", odd.union(primes))

print("Intersection: ", odd.intersection(primes))

print("Difference: ", odd.difference(primes))

print("Symmetric Difference: ", odd.symmetric_difference(primes))

OUTPUT:
Odd Numbers: {9 ,7 ,5 ,3 ,1}

Prime Numbers: {7 ,5 ,4 ,3 ,2}

Union: {9 ,7 ,5 ,4 ,3 ,2 ,1}

Intersection: {7 ,5 ,3}

Difference: {9 ,1}

Symmetric Difference: {9 ,4 ,2 ,1}


Program No.-4:
(a) Write a program using functions to check whether a number is even or
odd
defoddeven(a):

if (a0==2%):

return 1

else:

return 0

num = int(input("Enter a number: "))

if (oddeven(num)==1):

print("The given number is Even")

elif (oddeven(num)==0):

print("The given number is Odd")

OUTPUT:
Enter a number: 7

The given number is Odd

Enter a number: 6

The given number is Even

(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

word = input("\n Enter a String: ")

print("\n The Mirror image of the given string is: ", rev(word) )

OUTPUT:
Enter a String: school

The Mirror image of the given string is: loohcs


Program 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()

print("Total number of lines start with 's' or 'S'=",lc)

OUTPUT:
Sam

Sameer

Sanjay

Sunil

Total number of lines start with 's' or 'S'= 4


Program No.-6: Write a program using python to get 10
players nameand their score.Write the input in a csv file.
Accept a player name using python.Read the csv file to
display the name and the score.If the player name is not
found give an appropriate message

importcsv

with open('c:\\pyprg\\player.csv','w') as f:

w = csv.writer(f)

n=1

while (n<=10):

name = input("Player Name?:" )

score = int(input("Score: "))

w.writerow([name,score])

n+=1

print("Player File created")

f.close()

searchname=input("Enter the name to be searched ")

f=open('c:\\pyprg\\player.csv','r')

reader =csv.reader(f)

lst=[]

for row in reader:

lst.append(row)

q=0

for row in lst:

if searchname in row:

print(row)

q+=1

if(q==0):

print("string not found")

f.close()
OUTPUT:
Player Name?:Rohit Sharma Score: 264

Player Name?:VirenderSehwag Score: 219

Player Name?:Sachin Tendulkar Score: 200

Player Name?:Dhoni Score: 190

Player Name?:Sachin Tendulkar Score: 250

Player Name?:ViratKohli Score: 148

Player Name?:Ganguly Score: 158

Player Name?:KapilDev Score: 175

Player Name?:Amarnath Score: 148

Player Name?:SunilGavaskar Score: 200

Player File created

Enter the name to be searched Sachin Tendulkar

['Sachin Tendulkar', '200']

['Sachin Tendulkar', '250']


Program 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'

while c=='y' or c=='Y':

rno=int(input("Enter the roll no. of the student : "))

name=input("Enter the name of the student: ")

S['RollNo']=rno

S['Name']=name

pickle.dump(S,f)

c=input("Do You Want to add more students(y/n): ")

f.close()

f=open('stud.dat','rb')

rno=int(input("Enter the roll no. of the student to be search: "))

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:

print("Student not Found")


OUTPUT:
Enter the roll no. of the student : 1

Enter the name of the student: Ram

Do You Want to add more students(y/n): y

Enter the roll no. of the student : 2

Enter the name of the student: Raj

Do You Want to add more students(y/n): y

Enter the roll no. of the student : 3

Enter the name of the student: Sam

Do You Want to add more students(y/n): n

Enter the roll no. of the student to be search: 2

{'RollNo': 2, 'Name': ‘Raj’}


Program No. - 8: 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:
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):

print("Student Record of ",(i+1))

rollno=int(input("Enter Roll No. : "))

name=input("Enter Name: ")

marks=float(input("Enter Marks: "))

sr=[rollno,name,marks]

cw.writerow(sr)

f.close()

print("File Created Successfully")

OUTPUT:
Student Record of 1

Enter Roll No. : 11

Enter Name: Shiva

Enter Marks: 34

Student Record of 2

Enter Roll No. : 23

Enter Name: Saanvi

Enter Marks: 37

Student Record of 3

Enter Roll No. : 27

Enter Name: Sachin

Enter Marks: 24

Student Record of 4
Enter Roll No. : 28

Enter Name: Ram

Enter Marks: 40

Student Record of 5

Enter Roll No. : 29

Enter Name: Raj

Enter Marks: 37

File Created Successfully

Program No. - 10: WAP in Python to read a CSV file.

import csv
f=open("student.csv","r")

cr=csv.reader(f)

print("Content of CSV File: ")

for r in cr:

print(r)

f.close()

OUTPUT:
Content of CSV File:

['Rollno', 'Name', 'Marks']

['11', 'Shiva', '34.0']

['23', 'Saanvi', '37.0']

['27', 'Sachin', '24.0']

['28', 'Ram', '40.0']


Program No. – 11: WAP in Python to implement default
and positional parameters.

#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

Program No. - 12: 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:
Number of Vowels in the file= 184

Number of Consonants in the file= 335

Number of Total Chars in the file= 650

Program No. - 13: 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:
Number of Uppercase letters in the file= 139

Number of Lowercase Letters in the file= 380

Number of Total Chars in the file= 650

Program No. - 14: 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'

while c=='y' or c=='Y':

rno=int(input("Enter the roll no. of the student : "))

name=input("Enter the name of the student: ")

marks=int(input("Enter the marks of the student: "))

S['RollNo']=rno S['Name']=name

S['Marks']=marks

pickle.dump(S,f)

c=input("Do You Want to add more students(y/n): ")

f.close()

f=open('stud.dat','rb+')

rno=int(input("Enter the roll no. of the student to be updated: "))

marks=int(input("Enter the updated marks of the student: "))

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:

print("Student not Found")

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

Enter the name of the student: Ram

Enter the marks of the student: 25

Do You Want to add more students(y/n): y

Enter the roll no. of the student : 2

Enter the name of the student: Raj

Enter the marks of the student: 35

Do You Want to add more students(y/n): y

Enter the roll no. of the student : 3

Enter the name of the student: Sam

Enter the marks of the student: 32

Do You Want to add more students(y/n): n

Enter the roll no. of the student to be updated: 2

Enter the updated marks of the student: 40

{'RollNo': 1, 'Name': 'Ram', 'Marks': 25}

{'RollNo': 2, 'Name': 'Raj', 'Marks': 40}

{'RollNo': 3, 'Name': 'Sam', 'Marks': 32}

Program No. - 15: 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'
while c=='y' or c=='Y':

eid=int(input("Enter the Emp Id of the Employee : "))

ename=input("Enter the name of the Employee: ")

salary=float(input("Enter the salary of the Employee: "))

E['Emp_Id']=eid

E['Emp_Name']=ename

E['Salary']=salary

pickle.dump(E,f)

c=input("Do You Want to add more employee(y/n): ")

f.close()

f=open('emp.dat','rb+')

eid=int(input("Enter the Emp Id of the employee to be updated: "))

salary=float(input("Enter the updated salary of the employee: "))

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:

print("Employee not Found")

else:

f=open('emp.dat','rb')

try:

while True:
E=pickle.load(f)

print(E)

except EOFError:

f.close()

OUTPUT:

Enter the Emp Id of the Employee : 101

Enter the name of the Employee: Ram

Enter the salary of the Employee: 15000

Do You Want to add more employee(y/n): y

Enter the Emp Id of the Employee : 102

Enter the name of the Employee: Raj

Enter the salary of the Employee: 18000

Do You Want to add more employee(y/n): y

Enter the Emp Id of the Employee : 103

Enter the name of the Employee: Sam

Enter the salary of the Employee: 25000

Do You Want to add more employee(y/n): n

Enter the Emp Id of the employee to be updated: 102

Enter the updated salary of the employee: 22000

{'Emp_Id': 101, 'Emp_Name': 'Ram', 'Salary': 15000.0}

{'Emp_Id': 102, 'Emp_Name': 'Raj', 'Salary': 22000.0}

{'Emp_Id': 103, 'Emp_Name': 'Sam', 'Salary': 25000.0}

----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("create database if not exists spsharmag")

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"):

print("1. Press 1 for add new club: ")

print("2. Press 2 for Show the details of club: ")

print("3. Press 3 for Update club Details: ")

print("4. Press 4 for Delete club Details: ")

print("5. Press 5 for Exit: ")

choice=int(input("Enter Your Choice 1 or 2 or 3 or 4 or 5: "))

if(choice==1):

cid=int(input("Enter club Id: "))

cname=input("Enter club Name: ")

city=input("Enter club city: ")

mycursor.execute("insert into club values(%s,%s,%s)",(cid,cname,city))

con.commit()

elif(choice==2):

mycursor.execute("select * from club")

myclubs=mycursor.fetchall()

for x in myclubs:

print(x)

elif(choice==3):

cid=int(input("Enter the club id for update: "))

cname=input("Enter club New Name: ")

city=input("Enter club New city: ")

mycursor.execute("update club set cname=%s,city=%s where cid=%s",


(cname,city,cid))

con.commit()

elif(choice==4):
cid=int(input("Enter the club id for delete: "))

mycursor.execute("delete from club where cid=%s",(cid,))

con.commit()

elif(choice==5):

break

else:

print("Wrong Choice")

c=input("Press 'y' for continue and 'n' for exit: ")

OUTPUT:
1. Press 1 for add new club:

2. Press 2 for Show the details of club:

3. Press 3 for Update club Details:

4. Press 4 for Delete club Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 1

Enter club Id: 101

Enter club Name: C1

Enter club city: Delhi

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new club:

2. Press 2 for Show the details of club:

3. Press 3 for Update club Details:

4. Press 4 for Delete club Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 1

Enter club Id: 102

Enter club Name: C2

Enter club city: Noida

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new club:

2. Press 2 for Show the details of club:


3. Press 3 for Update club Details:

4. Press 4 for Delete club Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 2

(101, 'C1', 'Delhi')

(102, 'C2', 'Noida')

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new club:

2. Press 2 for Show the details of club:

3. Press 3 for Update club Details:

4. Press 4 for Delete club Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 3

Enter the club id for update: 102

Enter club New Name: C2Z

Enter club New city: Noida

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new club:

2. Press 2 for Show the details of club:

3. Press 3 for Update club Details:

4. Press 4 for Delete club Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 2

(101, 'C1', 'Delhi')

(102, 'C2Z', 'Noida')

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new club:

2. Press 2 for Show the details of club:

3. Press 3 for Update club Details:

4. Press 4 for Delete club Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 4


Enter the club id for delete: 102

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new club:

2. Press 2 for Show the details of club:

3. Press 3 for Update club Details:

4. Press 4 for Delete club Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 2

(101, 'C1', 'Delhi')

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new club:

2. Press 2 for Show the details of club:

3. Press 3 for Update club Details:

4. Press 4 for Delete club Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 5

Program No - 2: Write a menu driven program to


demonstrate add, display, update, delete and exit.
Performed on a table Book containing (bid, bname,
bprice) through python-MySql connectivity.

import mysql.connector

con=mysql.connector.connect(host="localhost",username="root",passwd="root")

mycursor=con.cursor()

mycursor.execute("create database if not exists spsharmag")

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"):

print("1. Press 1 for add new book: ")

print("2. Press 2 for Show the details of Books: ")

print("3. Press 3 for Update Book Details: ")

print("4. Press 4 for Delete Book Details: ")

print("5. Press 5 for Exit: ")

choice=int(input("Enter Your Choice 1 or 2 or 3 or 4 or 5: "))

if(choice==1):

bid=int(input("Enter Book Id: "))

bname=input("Enter Book Name: ")

bprice=float(input("Enter Book Price: "))

mycursor.execute("insert into book values(%s,%s,%s)",(bid,bname,bprice))

con.commit()

elif(choice==2):

mycursor.execute("select * from book")

mybooks=mycursor.fetchall()

for x in mybooks:

print(x)

elif(choice==3):

bid=int(input("Enter the book id for update: "))

bname=input("Enter Book New Name: ")

bprice=float(input("Enter Book New Price: "))

mycursor.execute("update book set bname=%s,bprice=%s where bid=%s",


(bname,bprice,bid))

con.commit()

elif(choice==4):

bid=int(input("Enter the book id for delete: "))

mycursor.execute("delete from book where bid=%s",(bid,))

con.commit()

elif(choice==5):

break

else:
print("Wrong Choice")

c=input("Press 'y' for continue and 'n' for exit: ")

OUTPUT:

1. Press 1 for add new book:

2. Press 2 for Show the details of Books:

3. Press 3 for Update Book Details:

4. Press 4 for Delete Book Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 1

Enter Book Id: 101

Enter Book Name: CS CRACKER

Enter Book Price: 350

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new book:

2. Press 2 for Show the details of Books:

3. Press 3 for Update Book Details:

4. Press 4 for Delete Book Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 1

Enter Book Id: 102

Enter Book Name: CS with Python

Enter Book Price: 500

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new book:

2. Press 2 for Show the details of Books:

3. Press 3 for Update Book Details:

4. Press 4 for Delete Book Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 2

(101, 'CS CRACKER', 350.0)

(102, 'CS with Python', 500.0)


Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new book:

2. Press 2 for Show the details of Books:

3. Press 3 for Update Book Details:

4. Press 4 for Delete Book Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 3

Enter the book id for update: 102

Enter Book New Name: IP with Python

Enter Book New Price: 450

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new book:

2. Press 2 for Show the details of Books:

3. Press 3 for Update Book Details:

4. Press 4 for Delete Book Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 2

(101, 'CS CRACKER', 350.0)

(102, 'IP with Python', 450.0)

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new book:

2. Press 2 for Show the details of Books:

3. Press 3 for Update Book Details:

4. Press 4 for Delete Book Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 4

Enter the book id for delete: 102

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new book:

2. Press 2 for Show the details of Books:

3. Press 3 for Update Book Details:

4. Press 4 for Delete Book Details:


5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 2

(101, 'CS CRACKER', 350.0)

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new book:

2. Press 2 for Show the details of Books:

3. Press 3 for Update Book Details:

4. Press 4 for Delete Book Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 5

Program No - 3: Write a menu driven program to


demonstrate add, display, update, delete and exit.
Performed on a table Product containing (pid, pname,
price) through python-MySql connectivity.

import mysql.connector

con=mysql.connector.connect(host="localhost",username="root",passwd="root")

mycursor=con.cursor()

mycursor.execute("create database if not exists spsharmag")

mycursor.execute("use spsharmag")

mycursor.execute("create table if not exists product (pid int primary key,pname


varchar(20),pprice float(8,2))")

c="y"

while(c=="y" or c=="Y"):

print("1. Press 1 for add new product: ")

print("2. Press 2 for Show the details of product: ")

print("3. Press 3 for Update product Details: ")


print("4. Press 4 for Delete product Details: ")

print("5. Press 5 for Exit: ")

choice=int(input("Enter Your Choice 1 or 2 or 3 or 4 or 5: "))

if(choice==1):

pid=int(input("Enter product Id: "))

pname=input("Enter product Name: ")

pprice=float(input("Enter product Price: "))

mycursor.execute("insert into product values(%s,%s,%s)",(pid,pname,pprice))

con.commit()

elif(choice==2):

mycursor.execute("select * from product")

myproducts=mycursor.fetchall()

for x in myproducts:

print(x)

elif(choice==3):

pid=int(input("Enter the product id for update: "))

pname=input("Enter product New Name: ")

pprice=float(input("Enter product New Price: "))

mycursor.execute("update product set pname=%s,pprice=%s where pid=


%s",(pname,pprice,pid))

con.commit()

elif(choice==4):

pid=int(input("Enter the product id for delete: "))

mycursor.execute("delete from product where pid=%s",(pid,))

con.commit()

elif(choice==5):

break

else:

print("Wrong Choice")

c=input("Press 'y' for continue and 'n' for exit: ")

OUTPUT:
Press 1 for add new product:

2. Press 2 for Show the details of product:

3. Press 3 for Update product Details:

4. Press 4 for Delete product Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 1

Enter product Id: 101

Enter product Name: Keyboard

Enter product Price: 800

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new product:

2. Press 2 for Show the details of product:

3. Press 3 for Update product Details:

4. Press 4 for Delete product Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 1

Enter product Id: 102

Enter product Name: Mouse

Enter product Price: 600 Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new product:

2. Press 2 for Show the details of product:

3. Press 3 for Update product Details:

4. Press 4 for Delete product Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 2

(101, 'Keyboard', 800.0)

(102, 'Mouse', 600.0)

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new product:

2. Press 2 for Show the details of product:

3. Press 3 for Update product Details:


4. Press 4 for Delete product Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 3

Enter the product id for update: 102

Enter product New Name: Mouse

Enter product New Price: 900

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new product:

2. Press 2 for Show the details of product:

3. Press 3 for Update product Details:

4. Press 4 for Delete product Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 2

(101, 'Keyboard', 800.0)

(102, 'Mouse', 900.0)

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new product:

2. Press 2 for Show the details of product:

3. Press 3 for Update product Details:

4. Press 4 for Delete product Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 4

Enter the product id for delete: 102

Press 'y' for continue and 'n' for exit: y

1. Press 1 for add new product:

2. Press 2 for Show the details of product:

3. Press 3 for Update product Details:

4. Press 4 for Delete product Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 2

(101, 'Keyboard', 800.0)

Press 'y' for continue and 'n' for exit: y


1. Press 1 for add new product:

2. Press 2 for Show the details of product:

3. Press 3 for Update product Details:

4. Press 4 for Delete product Details:

5. Press 5 for Exit:

Enter Your Choice 1 or 2 or 3 or 4 or 5: 5

Program No.4: Create a sql table using python and


accept 10 names and age .sort in descending order of
age and display

import sqlite3

connection = sqlite3.connect("info.db")

cursor = connection.cursor()

#cursor.execute("DROP Table student")

cursor.execute("create table student(name, age)")

print("Enter 10 students names and their ages respectively:")

for i in range(10):

who =[input("Enter Name:")]

age =[int(input("Enter Age:"))]

n =len(who)

for i in range(n):

cursor.execute("insert into student values (?, ?)", (who[i],age[i]))

cursor.execute("select * from student order by age desc")

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 Name:Aashik Mathew

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)

('Aashik Mathew', 23)

('Sowmyaa', 20)
('Annamalai', 17)

You might also like