Practical File
Practical File
Practical File
COMPUTER SCIENCE
CERTIFICATE
DELHI PRIVATE SCHOOL, DUBAI
CERTIFICATE
Code:
def transferFS(All, Even, Odd):
Even = All[0::2]
Odd = All[1::2]
return Even, Odd
Output:
2. Write a function FixSalary(Salary,N) to do the following
Code:
def FixSalary(Salary):
if Salary >= 200000:
Salary += (Salary * 0.35)
elif 100000 <= Salary < 200000:
Salary += (Salary * 0.30)
else:
Salary += (Salary * 0.20)
return Salary
Output:
3. Write a function AddEnd2() to find and display the sum of all values
ending with 2.
Code:
def AddEnd2(Sum, List):
for i in List:
if (i % 10) == 2:
Sum += i
return Sum
Output:
4. Write a program to perform sum of the following series: x-(x^2/3!) +
(x^3/5!) -(x^4/7!) +(x^5/9!)-…. upto N terms
Code:
def Series(X, N):
output, a, b, c, d = 1, 1, 1, 1, 1
for i in range(1, N):
b = b * d * (d + 1)
c = c * X * X
term = (-1) * a
d += 2
output = output + (a * c) / b
return output
Output:
5. Write a function to find the sum of series. (1) +(1+2) +(1+2+3)
+(1+2+3+4) …........up to N terms
Code:
def Series1(N):
Sum = 0
for i in range(1, N + 1):
for j in range(1, i + 1):
Sum += j
return Sum
Output:
INDEX
USER DEFINED FUNCTIONS
return Sum
Output:
2. Write a program that inputs a series of integers and passes them on one
at a time to function called even(), which uses the modulus operator to
determine if an integer is even. The function should take an integer
argument and return true if an integer is even, otherwise false
Code:
def Even(number):
val = False
if number >= 0:
if number % 2 == 0:
val = True
else:
if (-number) % 2 == 0:
val = True
return val
Output:
3. A large chemical company pays its salespeople on a common basis. The
salespeople receive Rs. 200 per week plus 9% of their gross sales for
that week. For example, a salesperson who sells Rs. 5000 worth of
chemicals in a week receives Rs.200 plus 9% of 5000, or a total of
Rs.650. Develop a program that will input each salespersons gross sales
for last week and calculate and display the salespersons earnings.
Code:
def Sale(sales):
salary = 200 + (sales * 0.09)
return salary
Output:
4. Write a function to find the sum of the series: x+ x^2/2! + x^4/4! +
x^6/6! + x^8/8! + ……. + x^n/n!
Code:
def Series3(N, X):
SUM = 0
for i in range(1, N + 1):
fact = 1
for j in range(1, i + 1):
fact *= j
SUM = SUM + ((X ** i) / fact)
return SUM
Output:
5. Write a function to find the sum of the series:1 + 1/3^2 + 1/5^2 + 1/7^2
+ 1/9^2 + ….. + 1/N^2
Code:
def Series4(N):
Sum = 0
for i in range(1, N + 1):
Sum = Sum + (1 / ((i + 2) ** 2))
return Sum
Output:
INDEX
FILE HANDLING
Code:
with open('Q1.txt','w') as f:
n='My name is Sahil Saxena\n Welcome to the jungle\n This is Python'
f.write(n)
f.close()
with open('Q1.txt','r') as f:
with open('N1.txt','w') as n:
r=f.readlines()
for i in r:
for j in i:
if 'a' or 'A' not in j:
n.write(j)
Output:
2. Write a program to read the content of file and display the total number
of consonants, uppercase, vowels and lower-case characters.
CODE:
with open('Prac.txt','r') as f:
n = f.read()
t_vow=0
t_con=0
up_c=0
low_c=0
for i in n:
if i in 'aeiouAEIOU':
t_vow+=1
if i not in 'aeiouAEIOU':
t_con+=1
if i.isupper()==True:
up_c+=1
if i.islower()==True:
low_c+=1
print("No of Vowels: ", t_vow)
print("No of Consonants: ", t_con)
print("No of Uppercase : ", up_c)
print("No of Lowercase: ", low_c)
Output:
3. Write a program to Program to create binary file to store Rollno,Name
and Marks and update marks of entered Rollno.
CODE:
import pickle
def insert():
record=[]
while True:
roll=int(input("Enter Student Roll No "))
name=input("Enter Student name: ")
d=[roll,name]
record.append(d)
val=input("Do you want to add more data (Y/N): ")
if val.lower()=='n':
break
with open('Prac','wb') as f:
pickle.dump(record,f)
def search():
with open('Prac', 'rb') as f:
record1=pickle.load(f)
found=0
rno=int(input("Enter Roll No to be searched: "))
for i in record1:
if i[0]==rno:
print(i[1],"Found!")
found=1
break
if found==0:
print("Record not found!")
while True:
print ('Choose one of the following options:')
print ('1. insert')
print ('2. Search')
print ('3. Exit')
val = int(input('Enter one of the above options:'))
if val == 1:
insert()
elif val == 2:
search()
else:
break
OUTPUT:
4. Write a program to create binary file to store Rollno and Name,
Search any Rollno and display name if Rollno found otherwise “Rollno
not found”.
CODE:
import pickle
with open("employee.dat", "ab") as n:
rec = 1
print("Please Enter Records!")
print()
while True:
print("Record No.", rec)
e_no = int(input("Roll No: "))
e_name = input("Name: ")
data = [e_no, e_name]
pickle.dump(data, n)
ans = input("Do you wish to enter more records (y/n)? ")
rec = rec + 1
if ans.lower() == 'n':
print("Record entry OVER ")
print()
break
OUTPUT:
5. Write a program to create CSV file and store empno, name, salary and
search for all names starting with ‘A’ and display name, salary and if
not found display appropriate message.
CODE:
import csv
with open('data1.csv','w',newline='') as f:
r=csv.writer(f)
r.writerow(['Emp_no','Name','Salary'])
list1=[]
while True:
emp_no=int(input("Enter Employee No: "))
emp_name=input("Enter Employee Name: ")
sal=int(input("Enter Salary of Employee: "))
list2=[emp_no,emp_name,sal]
list1.append(list2)
ch = input("Do you want to continue(Y/N): ")
if ch == 'N' or ch == 'n':
break
for i in list1:
r.writerow(i)
with open('data1.csv','r') as file:
read=csv.reader(file)
for i in read:
if i[1][0]=='A'or i[1][0]=='a':
print('name:',i[1])
print('salary:',i[2])
else:
print ("Not found")
OUTPUT:
6. Write a program to create CSV file and store empno, name, salary and
search for all names starting with ‘S’ with max 20 characters and
display name, salary and if not found display appropriate message.
CODE:
import csv
with open('Data2.csv','w',newline='') as file1:
r=csv.writer(file1)
r.writerow(['Emp_no','Name','Salary'])
list1=[]
while True:
emp_no = int(input("Enter Employee No: "))
emp_name = input("Enter Employee Name: ")
sal = int(input("Enter Salary of Employee: "))
list2 = [emp_no, emp_name, sal]
list1.append(list2)
ch=input("Do you want to continue(Y/N): ")
if ch=='N' or ch=='n':
break
for i in list1:
r.writerow(i)
else:
print ("Not found")
OUTPUT:
INDEX
DATA STRUCTURE (STACKS)
S. PROGRAMS TEACHER’
No. S
SIGNATUR
E
1. Write a program to create a stack called Student containing
data values roll number and name. Create functions push() for
pushing data values, pop() for removing data values and show()
for displaying data values.
2. Write a menu driven program stack using list containing
Employee data:Employee Id(Eid) and Employee Name(Ename).
Push()
Pop()
Display()
3. Write a menu driven program implementing stack using list
containing book record:Book Number (Bno) and Book
Name(Bname) .
Push()
Pop()
Display()
4. Write two functions push() to add and pop() to remove elements
in Directory using dictionary with fields pin code and name of
city using dictionary.
OUTPUT
def POP(Stack):
if len(Stack) == 0:
print("UNDERFLOW")
else:
val = Stack.pop()
print(val)
if len(Stack) == 0:
top_val = None
else:
top_val = len(Stack) - 1
print("Top VAL: ", top_val)
def Display(Stack):
if len(Stack) == 0:
print("UNDERFLOW")
else:
for i in Stack:
print(i[0], ":", i[1])
Stack = []
while True:
print("Choose following options:")
print('''1. Push 2. Pop 3. Display 4. Exit''')
val = int(input("Enter the chosen value : "))
if val == 1:
PUSH(Stack)
elif val == 2:
POP(Stack)
elif val == 3:
Display(Stack)
else:
break
OUTPUT:
2. Write a menu driven program stack using list containing Employee
data:Employee Id(Eid) and Employee Name(Ename). Push() Pop()
Display()
Code:
def PUSH(EMPDATA):
n = int(input("Enter the Number of inputs: "))
for i in range(n):
Eid = int(input("Enter your Employee ID: "))
Name = input("Enter your name: ")
record = (Eid, Name)
EMPDATA.append(record)
def POP(EMPDATA):
if len(EMPDATA) == 0:
print("UNDERFLOW")
else:
val = EMPDATA.pop()
print(val)
if len(EMPDATA) == 0:
top_val = None
else:
top_val = len(EMPDATA) - 1
print("Top VAL: ", top_val)
def Display(EMPDATA):
if len(EMPDATA) == 0:
print("UNDERFLOW")
else:
for i in EMPDATA:
print(i[0], ":", i[1])
EMPDATA = []
while True:
print("Choose following options:")
print('''1. Push 2. Pop 3. Display 4. Exit''')
val = int(input("Enter the chosen value : "))
if val == 1:
PUSH(EMPDATA)
elif val == 2:
POP(EMPDATA)
elif val == 3:
Display(EMPDATA)
else:
break
OUTPUT:
3. Write a menu driven program implementing stack using list containing
book record:Book Number (Bno) and Book Name(Bname) . Push() Pop()
Display()
CODE:
def PUSH(BOOKS):
n = int(input("Enter the Number of inputs: "))
for i in range(n):
Bno = int(input("Enter your Book ID: "))
Bname = input("Enter Book name: ")
record = (Bno, Bname)
BOOKS.append(record)
def POP(BOOKS):
if len(BOOKS) == 0:
print("UNDERFLOW")
else:
val = BOOKS.pop()
print(val)
if len(BOOKS) == 0:
top_val = None
else:
top_val = len(BOOKS) - 1
print("Top VAL: ", top_val)
def Display(BOOKS):
if len(BOOKS) == 0:
print("UNDERFLOW")
else:
for i in BOOKS:
print(i[0], ":", i[1])
BOOKS = []
while True:
print("Choose following options:")
print('''1. Push 2. Pop 3. Display 4. Exit''')
val = int(input("Enter the chosen value : "))
if val == 1:
PUSH(BOOKS)
elif val == 2:
POP(BOOKS)
elif val == 3:
Display(BOOKS)
else:
break
OUTPUT:
4. Write two functions push() to add and pop() to remove elements in
Directory using dictionary with fields pin code and name of city using
dictionary.
CODE:
def PUSH(City):
n = int(input("Enter the Number of inputs: "))
for i in range(n):
Bno = int(input("Enter your pin number: "))
Bname = input("Enter City: ")
City[Bno] = Bname
def POP(City):
if len(City) == 0:
print("UNDERFLOW")
else:
val = City.popitem()
print(val)
if len(City) == 0:
top_val = None
else:
top_val = len(City) - 1
print("Top VAL: ", top_val)
def Display(City):
if len(City) == 0:
print("UNDERFLOW")
else:
for i in City:
print(i, ":", City[i])
City = {}
while True:
print("Choose following options:")
print('''1. Push 2. Pop 3. Display 4. Exit''')
val = int(input("Enter the chosen value : "))
if val == 1:
PUSH(City)
elif val == 2:
POP(City)
elif val == 3:
Display(City)
else:
break
OUTPUT:
INDEX
SQL GROUP FUNCTION
S. No. PROGRAMS TEACHER’S
SIGNATURE
1. Consider the table ITEM and CUSTOMER given below. Write SQL
commands in MySql for (1) to (4) and give output for SQL queries (5) to
(8)
TABLE: ITEM
TABLE: CUSTOMER
A. SQL Commands:
1. To display the details of those customers whose city is
Delhi
1. To display the details of those items whose Price is in the
range of 35000 to 55000.
1. To display the CustomerName and City from table
Customer and Itemname and Price from table Item, with their
corresponding matching I_Id.
1. To increase the Price of all items by 1000 in the table item
A. Output Queries:
1. SELECT DISTINCT(City) FROM Customer;
1. SELECT ItemName, MAX(Price), count(*) FROM item
GROUP BY ItemName;
1. SELECT CustomerName, Manufacturer
FROM Item,Customer WHERE Item.I_ID=Customer.I_ID;
1. SELECT ItemName, Price*100 FROM Item WHERE
Manufacturer='ABC';
2. Consider the following tables DOCTOR and SALARY and write MySql
commands for the questions (1) to (4) and give output for SQL queries (5)
to (6)
TABLE: DOCTORS
TABLE: SALARY
A. SQL Commands:
1. Display NAME of all doctors who are in MEDICINE and
having more than 10 years from the table DOCTOR
1. Display the average salary of all doctors Working in “ENT”
department using the tables DOCTORS and SALARY.BASIC
= SALARY.BASIC + SALARY.ALLOWANCE
1. Display the minimum ALLOWANCE of female doctors
1. Display the highest consultation fee for Male doctors
A. Output Queries:
1. SELECT count(*) from DOCTORS where SEX= “F”;
1. SELECT NAME, DEPT, BASIC from DOCTORS,
SALARY where DEPT = “ENT” and DOCTORS.ID=
SALARY.ID
3. Consider the following tables GARMENT and FABRIC and write MySql
commands for the questions (1) to (4) and give outputs for SQL queries (5)
to (8).
TABLE: GARMENT
TABLE: FABRIC
A. SQL Commands:
1. To display GCODE and DESCRIPTION of each
GARMENT in descending order of GCODE.
1. To display the details of all the GARMENTs, which have
READYDATE in between 08-DEC-07 and 16-JUN-08
(inclusive of both dates).
1. To display the average PRICE of all the GARMENTs,
which are made up of FABRIC with FCODE as FO3.
1. To display FABRIC wise highest and lowest price of
GARMENTs from GARMENT table. (Display FCODE of each
GARMENT along with highest and lowest Price).
A. Output Queries:
1. SELECT SUM(PRICE) FROM GARMENT WHERE
FCODE = ‘F0l’;
1. SELECT DESCRIPTION, TYPE FROM GARMENT,
FABRIC WHERE GARMENT.FCODE = FABRIC.FCODE
AND GARMENT.PRICE >= 1260;
1. SELECT MAX(FCODE) FROM FABRIC;
1. SELECT COUNT(DISTINCT PRICE) FROM
GARMENT;
4. Consider the following tables worker and paylevel and write MySql
commands for the questions (1) to (4) and give outputs for SQL queries (5)
to (8).
TABLE: worker
TABLE: paylevel
A. SQL Commands:
1. To display the details of all WORKERs in descending order
of DOB.
1. To display NAME and DESIGN of those WORKERs
whose PLEVEL is either P001 or P002.
1. To display the content of all the WORKERs table whose
DOB is in between ‘19-Jan-1984 and ’18-Jan-1987'.
1. To add a new row in WORKER table with the following
data:
19, ‘Daya Kishore’, ‘Operator’, ‘P003’, 19-Jun-2008’, ’11-Jul-
1984'
A. Output Queries:
1. SELECT COUNT (PLEVEL), PLEVEL FROM WORKER
GROUP BY PLEVEL;
1. SELECT MAX(DOB), MIN(DOJ) FROM WORKER;
1. SELECT Name, Pay FROM WORKER W, PAYLEVEL P
WHERE W.PLEVEL = P.PLEVEL AND W.ECODE<13;
1. SELECT PLEVEL, PAY+ALLOWANCE FROM PAYLEVEL
WHERE PLEVEL = 'POO3';
5. Consider the following tables cabhub and customer and write MySql
commands for the questions (1) to (4) and give outputs for SQL queries (5)
to (8).
TABLE: cabhub
TABLE: customer
A. SQL Commands:
1. To display the names of all the white colored vehicles.
1. To display name of vehicle, make and capacity of all the
vehicles in ascending order of their sitting Capacity.
1. To display the highest charges at which a vehicle can be
hired from CABHUB.
1. To display all the Customer name and the corresponding
name of the vehicle hired by them.
A. Output Queries:
1. SELECT COUNT(DISTINCT Make) From CABHUB;
1. SELECT MAX(Charges), MIN(Charges) FROM
CABHUB;
1. SELECT COUNT(*), Make FROM CABHUB;
1. SELECT Vehicle FROM CABHUB WHERE Capacity=4;
1. Consider the table ITEM and CUSTOMER given below. Write SQL
commands in MySql for (1) to (4) and give output for SQL queries (5) to (8)
2. To display the details of those items whose Price is in the range of 35000 to
55000.
3. To display the CustomerName and City from table Customer and Itemname
and Price from table Item, with their corresponding matching I_Id.
1. Display NAME of all doctors who are in MEDICINE and having more than 10
years from the table DOCTOR
2. Display the average salary of all doctors Working in “ENT” department using
the tables DOCTORS and SALARY.BASIC = SALARY.BASIC +
SALARY.ALLOWANCE
3. To display the average PRICE of all the GARMENTs, which are made up of
FABRIC with FCODE as FO3.
3. To display the content of all the WORKERs table whose DOB is in between
‘19-Jan-1984 and ’18-Jan-1987'.
4. To add a new row in WORKER table with the following data: 19, ‘Daya
Kishore’, ‘Operator’, ‘P003’, 19-Jun-2008’, ’11-Jul-1984'
2. To display name of vehicle, make and capacity of all the vehicles in ascending
order of their sitting Capacity.
3. To display the highest charges at which a vehicle can be hired from CABHUB.
4. To display all the Customer name and the corresponding name of the vehicle
hired by them.
1. Write a program to create and insert the data into the following table:
Field Name Data Type Constraints
Name Char(20)
Sclass Varchar(5)
Age Number
Gender Char(10)
Percentage Float
Name Char(20)
Sclass Varchar(5)
Age Number
Gender Char(10)
Percentage Float
Name Char(20)
Age Number
Address Char(10)
Commission Float
Name Char(20)
ESalary float
EAge Number
EGender Char(1)
Designation Char(40)
Write a menu driven program
a)to insert n records from user
b) to search all employee whose salary is above 25000
Name Char(20)
ESalary float
EAge Number
EGender Char(1)
Designation Char(40)
Name Char(20)
Sclass Varchar(5)
Age Number
Gender Char(10)
Percentage Float
Code:
Output:
Name Char(20)
Sclass Varchar(5)
Age Number
Gender Char(10)
Percentage Float
def Insert():
import mysql.connector as mys
mycon = mys.connect(host="localhost", username='root',
password='Soumil008$',database = 'PRACTICAL_FILE')
mycursor = mycon.cursor()
n = int(input("Enter the number of records: "))
for i in range(n):
Roll_no = int(input("Enter roll number: "))
name = input("Enter name: ")
Class = input("Enter your class: ")
Age = int(input("Enter your age please: "))
Gender = input("Enter your gender please (MALE/FEMAlE): ")
percentage = float(input("Enter your percentage: "))
sql = "INSERT INTO STUDENTS
(Roll_no,Name,Sclass,Age,Gender,percentage) VALUES ('%d','%s','%s','%d','%s',"
\
"'%d') " % (Roll_no,name,Class,Age,Gender,percentage)
mycursor.execute(sql)
mycon.commit()
mycursor.close()
mycon.close()
print ("Values Added")
def Delete():
import mysql.connector as mys
mycon = mys.connect(host="localhost", username='root',
password='Soumil008$', database='PRACTICAL_FILE')
mycursor = mycon.cursor()
roll = int(input("Enter roll number to delete"))
mycursor.execute(f"DElETE FROM students where Roll_no = {roll} ")
print ("Deletion done! ")
mycon.commit()
def fetch():
import mysql.connector as mys
mycon = mys.connect(host="localhost", username='root',
password='Soumil008$', database='PRACTICAL_FILE')
mycursor = mycon.cursor()
mycursor.execute("Select * from students where Date_of_joining like '%-06-
%' ")
record = mycursor.fetchall()
for i in record:
print (i[0],":",i[1],":",i[2],":",i[3],":",i[4],":",i[5],":",i[6])
def display():
import mysql.connector as mys
mycon = mys.connect(host="localhost", username='root',
password='Soumil008$', database='PRACTICAL_FILE')
mycursor = mycon.cursor()
mycursor.execute("Select * from students ")
record = mycursor.fetchall()
for i in record:
print(i[0], ":", i[1], ":", i[2], ":", i[3], ":", i[4], ":", i[5],
":", i[6])
while True:
print ("Choose one of the following options")
print ("1. Insert N records from the user")
print ("2. Delete record on the basis of Roll number")
print ("3. To fetch records of the Student whose date of Joining is in the
month of june")
print ("4. To display all the records")
print ("5. Exit")
val = int(input("Enter option: "))
if val == 1:
Insert()
elif val == 2:
Delete()
elif val == 3:
fetch()
elif val == 4:
display()
elif val == 5:
break
Output:
INDEX
3. Write a python interface programs using MySQL module
using following specifications of Employee table. Use
appropriate connection to perform SQL query connections.
Field Name Data Type Constraints
Name Char(20)
Age Number
Address Char(10)
Commission Float
def display():
import mysql.connector as mys
mycon = mys.connect(host="localhost", username='root',
password='Soumil008$', database='PRACTICAL_FILE')
mycursor = mycon.cursor()
mycursor.execute("Select * from students ")
record = mycursor.fetchall()
for i in record:
print(i[0], ":", i[1], ":", i[2], ":", i[3], ":", i[4], ":", i[5],
":", i[6])
while True:
print ("Choose one of the following options")
print ("1. Insert N records from the user")
print ("2. Delete record on the basis of Roll number")
print ("3. To fetch records of the Student whose date of Joining is in the
month of june")
print ("4. To display all the records")
print ("5. Exit")
val = int(input("Enter option: "))
if val == 1:
Insert()
elif val == 2:
Delete()
elif val == 3:
fetch()
elif val == 4:
display()
elif val == 5:
break
Output:
4. Write a python interface programs using MySQL module
using following specifications of Employee table. Use
appropriate connection to perform SQL query connections.
def Search():
print()
import mysql.connector as mys
mycon = mys.connect(host="localhost", username='root',
password='Soumil008$', database='PRACTICAL_FILE')
mycursor = mycon.cursor()
mycursor.execute("Select * from emp1 where Esalary>25000 ")
record = mycursor.fetchall()
for i in record:
print(i[0], ":", i[1], ":", i[2], ":", i[3], ":", i[4], ":", i[5],
":", i[6])
def Display():
print()
import mysql.connector as mys
mycon = mys.connect(host="localhost", username='root',
password='Soumil008$', database='PRACTICAL_FILE')
mycursor = mycon.cursor()
mycursor.execute("Select * from emp1 ")
record = mycursor.fetchall()
for i in record:
print(i[0], ":", i[1], ":", i[2], ":", i[3], ":", i[4], ":", i[5],
":", i[6])
OUTPUT:
5. Write a python interface programs using MySQL module using following
specifications of Employee table. Use approprate connection to perform SQL
query connections.
def Search():
print()
import mysql.connector as mys
mycon = mys.connect(host="localhost", username='root',
password='Soumil008$', database='PRACTICAL_FILE')
mycursor = mycon.cursor()
mycursor.execute("Select * from emp2 where Designation = 'engineer' ")
record = mycursor.fetchall()
for i in record:
print(i[0], ":", i[1], ":", i[2], ":", i[3], ":", i[4], ":", i[5],
":", i[6])
def Display():
print()
import mysql.connector as mys
mycon = mys.connect(host="localhost", username='root',
password='Soumil008$', database='PRACTICAL_FILE')
mycursor = mycon.cursor()
mycursor.execute("Select * from emp2 ")
record = mycursor.fetchall()
for i in record:
print(i[0], ":", i[1], ":", i[2], ":", i[3], ":", i[4], ":", i[5],
":", i[6])
Output: