Nalin Practical PY FINAL
Nalin Practical PY FINAL
_______________________
___________________________
___________________________
PRINCIPAL’S
SIGNATURE
ACKNOWLEDGEMENT
INDEX
S.No Page Date Signature
TITLE No.
PYTHON
1. THE AREA OF VARIOUS SHAPES
2. LIBRARY FUNCTIONS
MySQL
4. EXTRACTING DATA
5. USING FUNCTIONS
7. GROUPING DATA
8. SORTING DATA
9. INTERFACE PYTHON
PYTHON
PROGRAM-1 TO CALCULATE THE AREA OF VARIOUS SHAPES
USING FUNCTIONS
AIM: To easily calculate the area of different shapes and to reduce
the program size and calculations by using functions
def rectangle(x,y):
area=x"y
return area
def square(x):
area=x**2
return area
def circle(x):
area=3.14*x*x
return area
def triangle(x,y):
area=0.5*x*y
return area
y="yes"
while y=="yes":
print("MENU")
print("Which area do you want to find?")
print("1.Area of triangle")
print("2.Area of rectangle")
print("3.Area of square")
print("4.Area of circle")
x=int(input("enter your choice:"))
if x==1:
base=int(input("enter base of the triangle:"))
height=int(input("enter height of the triangle:"))
a=triangle(base,height)
print(a)
elif x==2:
l=int(input("enter length of the rectangle:"))
b=int(input("enter the breadth of the rectangle"))
a=rectangle(l,b)
print(a)
elif x==3:
s=int(input("enter the side of the square:"))
a=square(s)
print(a)
elif x==4:
r=int(input("enter the radius of the circle:"))
a=circle(r)
print(a)
y=input("do you want to continue? reply with yes/no")
import math
import string
y="yes"
while y=="yes":
print("MENU")
print("1.F 1.Functions in math module")
print("2.Functions in string module")
a=int(input("enter choice 1 or 2:"))
if a==1:
print("Choose the functions:")
print("1.sqrt")
print("2.exp")
print("3.pow")
print("4.sin")
print("5.cos")
print("6.radians")
b=int(input("choose a function:"))
if b==1:
x=int(input("enter the value to be calculated:"))
y=math.sqrt(x)
print(y)
elif b==2:
x=int(input("enter the value to be calculated:"))
y=math.exp(x)
print(y)
elif b==3:
x=int(input("enter value to be calculated:"))
a=int(input("enter power:"))
y=math.pow(x,a)
print(y)
elif b==4:
x=int(input("enter the value to calculated:"))
y=math.sin(x)
print(y)
elif b==5:
x=int(input("enter the value to calculated:"))
y=math.cos(x)
print(y)
elif b==6:
x=int(input("enter the value to calculated:"))
y=math.radians(x)
print(y)
elif a==2:
print("choose the functions:")
print("1.lower")
print("2.upper")
print("3.title")
print("4.find")
print("5.replace")
b=int(input("choose a function:"))
if b==1:
x=input("Enter the string:")
print(x.lower())
elif b==2:
x=input("Enter the string:")
print(x.upper())
elif b==3:
x=input("Enter the string:")
print(x.title())
elif b==4:
x=input("Enter the string:")
print(x.find(y))
elif b==5:
x=input("Enter the string:")
y=input("Enter word to be replaced:")
z=input("enter the new word:")
print(x.replace(y,z))
y=input("do you want to continue? (yes or no)")
PROGRAM-3 CREATING MODULES AND PACKAGES
AIM: To easily calculate Length conversions by importing
modules
def miletokm(x):
return x*1.609344
def kmtomile(x):
return x/1.609344
def feettoinches(x):
return x*12
def inchestofeet(x):
return x/12
#Mass Conversion
def kgtotonne(x):
return x*0.001
def tonnetokg(x):
return x/0.001
def kgtopound(x):
return x*2.20462
def poundtokg(x):
return x/2.20462
f1=open("story.txt","w")
a=int(input("enter the limit:"))
for i in range(a):
name=input("enter name:")
f1.write(name)
f1.write("\n")
f1.close()
f1=open("story.txt","r")
for i in f1.readlines():
print(i)
f1.close()
f1=open("p.txt","r")
f2=open("book.txt","w")
X=""
c=0
for x in f1.read():
if "w" not in x:
f2.write(x)
f1.close()
f2.close()
f2=open("book.txt","r")
for i in f2.readlines():
print(i)
f2.close()
OUTPUT:
enter the word: We
the output is: 6
import pickle
def read():
st={}
fin=open("bin.dat","rb")
try:
print("File bin.dat stores these records")
while True:
st=pickle.load(fin)
print(st)
except EOFError:
fin.close()
def display():
fin=open("bin.dat","rb")
found-False
a=int(input("Enter the record to be searched:"))
try:
print("Searching in file bin.dat")
while True:
st-pickle.load(fin)
if st['rno']==a:
print(st)
found=True
except EOFError:
if found==False:
print("No such records found in the file")
else:
print("search successfull")
fin.close()
ans="y"
while ans=="y":
print("MENU")
print("1.read and display the records")
print("2.search for records")
z=int(input("enter the function:"))
if z==1:
read()
if z==2:
display()
ans=input("Do you want to continue(y/n)")
PROGRAM-17 STACKS
AIM: To create a stack
def isempty(stk):
if stk==[]:
return True
else:
return False
def push(stk,item):
stk.append(item)
top=len(stk)-1
def pop stk)
if isempty(stk):
return stack is empty"
else:
element=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return element
def peek(stk):
if isempty(stk):
return'stack is empty'
else:
top-len(stk)-1
return stk[top]
def display(stk):
ifisempty(stk):
print('stack is empty")
else:
top-len(stk)-1
print(stk[top])
for i in range(top-1,-1,-1):
print(stk[i])
stack=[]
top=None
while True:
print('stack operation")
print('1 push')
print("2.pop')
print('3.peek')
print('4.display")
print('5.exit')
ch=int(input('enter your choice(1 to 5):"))
if ch==1:
item=int(input('enter the element'))
push(stack,item)
elif ch==2:
element-pop(stack)
print('popped element is,element)
elif ch==3:
t=peek(stack)
print('top most elementis,t)
elif ch==4:
display(stack)
break
else:
print('invalid choice')
MySQL
MySQL
OUTPUT
Practical - 3 : To delete records based on criteria
Source code :
OUTPUT :
Source code :
OUTPUT :
Practical 4 :Extracting data
Practical 10 : JOINS
INTERFACE PYTHON
1. Using the concept of interface python create
a menu driven program to create table, insert
records and fetch records in sql
SOURCE CODE:
import mysql.connector as sql
con=sql.connect(host="localhost",user="root",p
asswd="rootadmin",\database="prac25")
if con.is_connected():
print("success!!!")
cur=con.cursor()
def createtable():
table="CREATE TABLE STUDENT1
(REGISTER_NO INTEGER,NAME VARCHAR(40),
CLASS\
VARCHAR(30), MARKS INTEGER, STREAM
VARCHAR(30))"
cur.execute(table)
con.commit()
print("table is created")
#createtable()
def insert();
a=int(input("enter the no of records to be
entered:"))
for i in range(a):
reg=int(input("enter the register no of the
students:"))
name=input("enter the name of the
student:")
class1=input("enter the class of the
students:")
marks=float(input("enter the marks of the
students"))
stream=input("enter the stream of the
students:")
insert1="INSERT INTO STUDENT1 VALUES
(0.'0','0','0')".format(reg, name,class1, marks,
stream)
cur.execute(insert(1)
con.commit()
def fetch():
cur.execute("SELECT*FROM STUDENT1")
d=cur.fetchall()
for row in d:
print(row)
def fetch1():
cur.execute("SELECT * FROM STUDENT1")
d=cur.fetchone()
print(d)
def fetch2():
cur.execute("SELECT * FROM STUDENT1")
a=int(input("enter the no of records to be
fetched:"))
d=cur.fetchmany(a)
for row in d:
print(row)
while True:
print("----MENU----")
print("1.creating table")
print("2.inserting records")
print("3.fetching all the records")
print("4.fetching one record")
print("5.fetching many records")
print("6.exit")
ch=int(input("enter your choice:"))
if ch==1:
createtable()
elif ch==2:
insert()
elif ch==3:
fetch()
elif ch==4:
fetch1()
elif ch==5:
fetch2()
elif ch==6:
break
$$$$$$$$$$$
6.exit
enter your choice:4
(1, 'lakshmi', '12', 98, 'group v')
----MENU----
1.creating table
2.inserting records
3.fetching all the records
4.fetching one record
5.fetching many records ster
6.exit
enter your choice:5
enter the no of records to be fetched:3
(1, 'lakshmi', '12', 98, 'group v')
(101, 'lakshmi', '12', 98, 'group v')
(102, 'praniksha', '12', 97, 'group v')
----MENU----
1.creating table
2.inserting records
3.fetching all the records
4.fetching one record
5.fetching many records
6.exit
enter your choice:6
INTERFACE PY -2 REVISED
INTERFACE PYTHON
1. Using the concept of interface python create
a menu driven program to create table, insert
records and fetch records in sql
SOURCE CODE:
import mysql.connector as sql
con=sql.connect(host="localhost", user="root",
passwd="rootadmin",\
database="prac25")
if con.is_connected():
print("success!!!")
cur=con.cursor()
def createtable():
table="CREATE TABLE STUDENT1
(REGISTER_NO INTEGER,NAME VARCHAR(40),
CLASS\
VARCHAR(30), MARKS INTEGER, STREAM
VARCHAR(30))"
cur.execute(table)
con.commit()
print("table is created")
#createtable()
def insert();
a=int(input("enter the no of records to be
entered:"))
for i in range(a):
reg=int(input("enter the register no of the
students:"))
name=input("enter the name of the
student:")
class1=input("enter the class of the
students:")
marks=float(input("enter the marks of the
students"))
stream=input("enter the stream of the
students:")
insert1="INSERT INTO STUDENT1 VALUES
(0.'0','0','0')".format(reg, name,class1, marks,
stream)
cur.execute(insert(1)
con.commit()
def fetch():
cur.execute("SELECT*FROM STUDENT1")
d=cur.fetchall()
for row in d:
print(row)
def fetch1():
cur.execute("SELECT * FROM STUDENT1")
d=cur.fetchone()
print(d)
def fetch2():
cur.execute("SELECT * FROM STUDENT1")
a=int(input("enter the no of records to be
fetched:"))
d=cur.fetchmany(a)
for row in d:
print(row)
while True:
print("----MENU----")
print("1.creating table")
print("2.inserting records")
print("3.fetching all the records")
print("4.fetching one record")
print("5.fetching many records")
print("6.exit")
ch=int(input("enter your choice:"))
if ch==1:
createtable()
elif ch==2:
insert()
elif ch==3:
fetch()
elif ch==4:
fetch1()
elif ch==5:
fetch2()
elif ch==6:
break
$$$$$$$$$$$
6.exit
enter your choice:4
(1, 'lakshmi', '12', 98, 'group v')
----MENU----
1.creating table
2.inserting records
3.fetching all the records
4.fetching one record
5.fetching many records ster
6.exit
enter your choice:5
enter the no of records to be fetched:3
(1, 'lakshmi', '12', 98, 'group v')
(101, 'lakshmi', '12', 98, 'group v')
(102, 'praniksha', '12', 97, 'group v')
----MENU----
1.creating table
2.inserting records
3.fetching all the records
4.fetching one record
5.fetching many records
6.exit
enter your choice:6
2.updating
def update():
a=int(input("enter the register no of records
to be updated:"))
print("CHOOSE THE FIELD TO BE UPDATED")
print("1.NAME")
print("2.CLASS")
print("3.STREAM")
print("4.MARK")
ch=int(input("enter your choise"))
if ch==1:
name=input ("enter new student name:")
cur.execute("update student1 set
name='{}' where (register_no={})"\
.format(name,a))
con.commit()
print("name updated")
elif ch==2:
clas=input("enter new class:")
cur.execute("update student1 set
class='{}' where (register_no={})}"\
.format(clas,a))
print("class updated")
elif ch==3:
group=input("enter new group:")
cur.execute("update student1 set
stream='{}' where (register_no={})"\
.format(group,a))
con.commit()
print("group updated")
elif ch==4:
mark=float(input("enter new mark:"))
cur.execute("update student1 set
marks={} where (register_no={})"\
.format(mark,a))
con.commit()
print("mark updated")
update()
OUTPUT
success!!!
enter the register no of records to be updated:
101
CHOOSE THE FIELD TO BE UPDATED
1.NAME
2.CLASS 3.STREAM
4.MARK
enter your choise1
enter new student name:lakshmishree
name updated
enter
enter the register no of records to be
updated:102
CHOOSE THE FIELD TO BE UPDATED
1.NAME
2.CLASS
3.STREAM
4.MARK
enter your choise2
enter new class:11
class updated
enter the register no of records to be updated:
103
CHOOSE THE FIELD TO BE UPDATED
1.NAME
2.CLASS
3.STREAM
4.MARK
enter your choise3
enter new group: group i
group updated
enter the register no of records to be
updated:101
CHOOSE THE FIELD TO BE UPDATED
1.NAME
2.CLASS
3.STREAM
4.MARK
enter your choise4
enter new mark:99
mark updated