Amit Practical
Amit Practical
SCHOOL
PROJECT FILE
PROGRAM 5
AIM: WAP to operate a text file?
Code:
f=open("sample.txt","w")
s="hlo python"
f.write(s)
f.close()
print("amit")
PROGRAM 6
AIM: calculate size of file?
Code:
f=open("sample.txt","r")
s=f.read()
l=len(s)
print("total no. of bytes/size",l)
print("amit")
PROGRAM 7
AIM: WAP to display total no. of words in a file?
Code:
f=open("sample.txt","r")
s=f.read()
l=s.split()
n=len(l)
print(n)
print("amit")
PROGRAM 8
AIM: WAP to display a total no. of line in a file?
Code:
f=open("sample.txt","r")
s=f.readlines()
l=len(s)
print("total no. of lines in a file",l)
print("amit")
PROGRAM 9
AIM: WAP to create operation on binary file?
Code:
import pickle
f=open("emp.dat","wb")
d={"name":"amitji","empno.":432,"salary":39000}
d1={"name":"amrit ji","emp.":423,"salary":36000}
pickle.dump(d,f)
pickle.dump(d1,f)
f.close()
print(“amit”)
PROGRAM 10
AIM: WAP to display a reading on binary file?
Code:
import pickle
f=open("emp.dat","rb")
s=" "
try:
while True:
s=pickle.load(f)
print(s)
except:
print(";no more data to display")
f.close()
print(“amit)
PROGRAM 11
AIM: WAP to operate on csv file?
Code:
import csv
f=open("record.csv","w")
a="y"
p=csv.writer(f)
while a=="y":
name=input("entert name")
rn=int(input("enter rollno."))
marks=int(input("enter ur marks"))
l=[name,rn,marks]
p.writerow(l)
a=input("do u want to enter new record.pressy")
f.close()
PROGRAM 12
AIM: WAP to read a binary file ?
Code:
import csv
f=open("record.csv","r")
r=csv.reader(f)
for i in r:
print(r)
prin(“amit”)
PROGRAM 13
AIM: WAP to implement different types of argument?
Code:
(i) positional
def check(x,y):
if x>10:
print("x is big")
elif y>10:
print("y is big")
check(2,23)
print(“amit”)
output:-
(ii) keyword:-
def check(x,y):
if x>10:
print("x is big")
elif y>10:
print("y is big")
check(y=2,x=23)
print(“amit”)
Output:-
(iii) default:-
def check(x,y=2):
if x>10:
print("x is big")
elif y>10:
print("y is big")
check(18)
print(“amit”)
Output:-
PROGRAM 14
AIM: WAP to implement stack operation?
Code:
(1) push()
(2) pop
(1) push()
stk=[]
top=None
def push():
if len(stk)==0:
stk.append(items)
top=0
else:
stk.append(items)
top=top+1
print(top)
print(“amit”)
(2) pop()
stk=[12,34]
a=stk.pop()
print(a)
print(stk)
print("amit")
Output:-
PROGRAM 15
AIM: WAP to display global and local scope?
Code:
def add(x,y):
global a
z=x+y
a=a+10
print(z)
print(a)
a=12
b=12
add(a,b)
Local():-
def add(x,y):
z= x+y
print(a,b)
print(z)
a=12
b=19
add(a,b)
print(z)
print(“amit”)
Output:-
Display() :-
stk=[10,20,30,40]
def display(stk):
for i in stk:
print(i)
print("amit")
Output:-
Program 16
Ques16. Write a program to connect Python with MySQL using
database connectivity and perform the following operations on
data in database: Fetch, Update and delete the data.
A. CREATE A TABLE
import mysql.connector
demodb = mysql.connector.connect(host="localhost", user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor()
democursor execute("CREATE TABLE STUDENT (admn_no int primary
key, sname varchar(30), gender char(1). DOB date, stream
varchar(15), marks float(4.2))")
Q1 create DATABASE
Syntax :
use name:
create table:
Syntax:
Q2 show database
Syntax:
show tables
Syntax:
Desc
Insert table
Q3 Alter command
Delete
Add
Update
Rename
Select command
To select table
Select scalar expression
Logical relation
Descending order
Patter match
PROGRAM 17
Aggregate function:
count(): select count(dept) from employee;
average(): select avg(salary) from employee;
sum(): select sum(salary) from employee;
max(): select max(salary) from employee;
min(): select min(salary) from employee;