0 - Practical File - 0
0 - Practical File - 0
PUSHP VIHAR
S. No. Practical
1 Write a python program to search an element in a list and display the
frequency of elements present in the list and their location using
Linear search by using a user defined function.
2 Write a program to count frequency of words “his” or “her” in a text
file.
3 Write a python program to pass a list to a function and double the
odd values and half even values of a list and display list elements
after changing.
4 Write a Python program input n numbers in tuple and pass it to
function to count how many even and odd numbers are entered.
5 Write a Python program to function with key and value, and update
value at that key in the dictionary entered by the user.
10 Find the total number of students of each gender in the using group
by
11 Find the total number of girls who scored more than 75 marks
OUTPUT 2:
with open('file.txt','r') as f:
s=f.read()
l=s.split()
c1=0
c2=0
for i in l:
if i.lower()=='his':
c1+=1
elif i.lower()=='her':
c2+=1
print('Frequency of his in the file:',c1)
print('Frequency of her in the file:',c2)
OUTPUT :
L=[2,11,16,19,8,3]
l=[]
def func(L):
for i in L:
if i%2==0:
i=i/2
l.append(i)
else:
i=i*2
l.append(i)
return l
print("List before changing: ",L)
print("List after changing: ",func(L))
OUTPUT :
t=()
n=int(input("Enter no. of elements in tuple: "))
for i in range(n):
a=int(input("Enter number in tuple: "))
t+=(a,)
e=0
o=0
for i in t:
if i%2==0:
e+=1
else:
o+=1
print("Tuple: ",t)
print("No. of odd numbers in tuple: ",o)
print("No. of even numbers in tuple: ",e)
OUTPUT :
dic={}
while True:
k=input("Enter key: ")
v=input("Enter value: ")
dic[k]=v
u=input("Do you want to add more? (y/n) :")
if u.lower()!='y':
break
print()
print("Your dictionary: ",dic)
print()
def update(K,V):
dic[K]=V
return dic
else:
print()
print('No key named',key,'is present in dictionary')
OUTPUT 1:
Enter key: apple
Enter value: red
Do you want to add more? (y/n) :y
Enter key: grapes
Enter value: black
Do you want to add more? (y/n) :n
OUTPUT 2:
Enter key: apple
Enter value: red
Do you want to add more? (y/n) :n
def countvowel(s):
c=0
for i in s:
if i.lower() in ['a','e','i','o','u']:
c+=1
return c
S=input("Enter a sting: ")
v=countvowel(S)
print('No. of vowels in " ',S,' " : ',v)
OUTPUT :
def dice():
import random
a=random.randint(1,6)
return a
while True:
print("You rolled: ",dice())
u=input("Do you want to roll again? (y/n) :")
if u.lower()!='y':
break
OUTPUT :
You rolled: 4
Do you want to roll again? (y/n) :y
You rolled: 1
Do you want to roll again? (y/n) :y
You rolled: 3
Do you want to roll again? (y/n) :n
8) Write a menu driven python program to implement 10 python
mathematical functions.
print("----------------MENU----------------")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Square")
print("6. Square root")
print("7. Raising an exponent")
print("8. Log with a custom base")
print("9. Smallest integer")
print("10. Factorial")
print("11. Exit")
print("="*50)
import math
while True:
u=int(input("Enter your choice: "))
if u==1:
a=int(input("Enter a number: "))
b=int(input("Enter a number: "))
print(a+b)
elif u==2:
a=int(input("Enter a number: "))
b=int(input("Enter a number: "))
print(a-b)
elif u==3:
a=int(input("Enter a number: "))
b=int(input("Enter a number: "))
print(a*b)
elif u==4:
a=int(input("Enter a number: "))
b=int(input("Enter a number: "))
print(a/b)
elif u==5:
a=int(input("Enter a number: "))
print(a**2)
elif u==6:
a=int(input("Enter a number: "))
print(math.sqrt(a))
elif u==7:
a=int(input("Enter base: "))
b=int(input("Enter power: "))
print(math.pow(a,b))
elif u==8:
a=int(input("Enter a number: "))
b=int(input("Enter base: "))
print(math.log(a,b))
elif u==9:
a=float(input("Enter a number: "))
print(math.ceil(a))
elif u==10:
a=int(input("Enter a number: "))
print(math.factorial(a))
elif u==11:
print("Exiting program")
print("="*50)
break
else:
print("Oops! Incorrect choice")
print("="*50)
OUTUPUT:
----------------MENU----------------
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Square
6. Square root
7. Raising an exponent
8. Log with a custom base
9. Smallest integer
10. Factorial
11. Exit
==================================================
Enter your choice: 1
Enter a number: 2
Enter a number: 3
5
==================================================
Enter your choice: 8
Enter a number: 64
Enter base: 4
3.0
==================================================
Enter your choice: 9
Enter a number: 3.8
4
==================================================
Enter your choice: 11
Exiting program
==================================================
9) Write a menu driven program in python to delete the name of a
student from the dictionary and to search phone no of a student by
student name. Create menu as below:
MENU
1. Delete from Dictionary
2. Search Phone number using name from Dictionary
3. Exit
OUTPUT :
------------------MENU------------------
1. Show dictionary
2. Delete from Dictionary
3. Search Phone number using name from Dictionary
4. Exit
=========================================================
Enter your choice: 1
{'Atul': '9967884589', 'Abhinay': '8938572946', 'Vinay': '1289452967',
'Anshi': '1234567890', 'Preeti': '4589240087'}
=========================================================
Enter your choice: 2
Enter name: atul
=========================================================
Enter your choice: 3
Enter name: vinay
Phone number of Vinay : 1289452967
=========================================================
Enter your choice: 1
{'Abhinay': '8938572946', 'Vinay': '1289452967', 'Anshi': '1234567890',
'Preeti': '4589240087'}
=========================================================
Enter your choice: 4
Exiting program
=========================================================
10) Write a python program to read and display file content line by
line with each word separated by #.
OUTPUT :
Hi#I#am#Vivaan#Sinha#
I#am#17#years#old#
This#is#my#computer#science#practical#
11) Write a python program Read a text file and display the number
of vowels, consonants, upper case, lower case characters in the file.
with open('file.txt','r') as f:
s=f.read()
v,c,u,l=0,0,0,0
for i in s:
if i.isupper():
u+=1
if i.lower() in 'aeiou' :
v+=1
elif i.lower() not in 'aeiou' :
c+=1
elif i.islower():
l+=1
if i.lower() in 'aeiou' :
v+=1
elif i.lower() not in 'aeiou' :
c+=1
print(s)
print("No. of consonants : ",c)
print("No. of vowels : ",v)
print("No. of upper case letters : ",u)
print("No. of lower case letters : ",l)
OUTPUT :
Hi I am Vivaan Sinha
I am 17 years old
This is my computer science practical
No. of consonants : 35
No. of vowels : 24
No. of upper case letters : 6
No. of lower case letters : 53
12) Write a Menu driven program in python to count spaces,
digits, words and lines from text file TOY.txt
f=open('Toy.txt')
S=f.read()
print("-----------------------MENU-----------------------")
print("1. Show File")
print("2. Count Spaces")
print("3. Count Digits")
print("4. Count words")
print("5. Count Lines")
print("6. Exit")
print('='*50)
s=d=w=l=0
for i in S:
if i.isspace():
s+=1
elif i.isdigit():
d+=1
elif i.isalpha():
w+=1
while True:
u=int(input("Enter your choice: "))
if u==1:
print(S)
elif u==2:
print("No. of spaces:",s)
elif u==3:
print("No. of digits:",d)
elif u==4:
print("No. of words:",w)
elif u==5:
f.close()
f=open('Toy.txt','r')
L=f.readlines()
print("No. of lines:",len(L))
elif u==6:
print("Exiting program")
print('='*50)
break
else:
print("Oops! Incorrect Choice")
print('='*50)
OUTPUT :
-----------------------MENU-----------------------
1. Show File
2. Count Spaces
3. Count Digits
4. Count words
5. Count Lines
6. Exit
==================================================
Enter your choice: 1
Hi I am Vivaan Sinha
I am 17 years old
This is my computer science practical
==================================================
Enter your choice: 2
No. of spaces: 15
==================================================
Enter your choice: 3
No. of digits: 2
==================================================
Enter your choice: 4
No. of words: 59
==================================================
Enter your choice: 5
No. of lines: 3
==================================================
Enter your choice: 6
Exiting program
==================================================
13) Write a python program to remove all the lines that contain
the character “a‟ from file “sample.txt” and write it to another
file “newfile.txt”.
f=open('sample.txt','r')
l=f.readlines()
print(l)
L=[]
for i in l:
if 'a' in i :
L.append(i)
l.remove(i)
f.close()
f=open('sample.txt','w')
f.writelines(l)
f.close()
f=open('newfile.txt','w')
f.writelines(L)
f.close()
print('='*50)
f=open('sample.txt','r')
print(f.readlines())
f.close()
print('='*50)
f=open('newfile.txt','r')
print(f.readlines())
f.close()
print('='*50)
OUTPUT :
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number : "))
name= input("Enter Name : ")
student.append([roll,name])
ans=input("Add More ? (y/n): ")
print('='*50)
pickle.dump(student,f)
f.close()
l=[]
f=open('student.dat','rb')
try:
while True:
l=pickle.load(f)
except EOFError:
pass
while True:
found=False
r=int(input("Enter roll no. to search: "))
for s in l:
if s[0]==r:
print("Name of student is: ",s[1])
found=True
if found==False:
print("Roll no. not found")
ans=input("Want to search more? (y/n): ")
print('='*50)
if ans.lower()!='y':
print("Exiting program")
print('='*50)
break
f.close()
OUTPUT :
Enter Roll Number : 1
Enter Name : abhi
Add More ? (y/n): y
==================================================
Enter Roll Number : 2
Enter Name : preeti
Add More ? (y/n): y
==================================================
Enter Roll Number : 18
Enter Name : vinay
Add More ? (y/n): n
==================================================
Enter roll no. to search: 1
Name of student is: abhi
Want to search more? (y/n): y
==================================================
Enter roll no. to search: 15
Roll no. not found
Want to search more? (y/n): n
==================================================
Exiting program
==================================================
15) Write a python program using list having roll number, name
and marks. Accept 5 records from the user and write them into a
binary file.
import pickle
f=open('students.dat','wb')
l=[]
for i in range (5):
r=int(input("Enter roll no.: "))
n=input("Enter name: ")
m=int(input("Enter marks: "))
print('='*50)
l.append([r,n,m])
pickle.dump(l,f)
f.close()
f=open('students.dat','rb')
try:
while True:
s=pickle.load(f)
except EOFError:
pass
print("%10s"%'Roll no.','%20s'%'Name','%30s'%'Marks')
print('='*50)
for i in s:
print("%10s"%i[0],'%20s'%i[1],'%30s'%i[2])
print('='*50)
OUTPUT :
Enter roll no.: 1
Enter name: abhi
Enter marks: 95
==================================================
Enter roll no.: 2
Enter name: preeti
Enter marks: 99
==================================================
Enter roll no.: 3
Enter name: vinay
Enter marks: 92
==================================================
Enter roll no.: 4
Enter name: riya
Enter marks: 65
==================================================
Enter roll no.: 5
Enter name: ajay
Enter marks: 57
==================================================
Roll no. Name Marks
==================================================
1 abhi 95
2 preeti 99
3 vinay 92
4 riya 65
5 ajay 57
==================================================
16) Write a python program to create a CSV file by entering user-id
and password, read and search the password for given user-id.
import csv
with open("record.csv","a") as f:
fw=csv.writer(f)
while True:
uid = input("Enter id: ")
password = input("Enter password: ")
fw.writerow([uid,password])
ans=input("Want to continue? (y/n) :")
print('='*50)
if ans.lower()!='y':
break
with open("record.csv","r") as f:
fr=csv.reader(f)
u=input("Enter use it to search: ")
for i in fr:
if i!=[]:
if i[0] == u:
print("Password: ",i[1])
break
OUTPUT :
import csv
print('-----------------------MENU-----------------------')
print('1: create csv file')
print('2: Search as per dept no.')
print('3: Exit')
print('='*50)
while True:
ch=int(input('Enter choice: '))
if ch==1:
f=open('dept.csv','a')
fw=csv.writer(f)
ans='y'
print('-'*50)
while ans.lower()=='y':
dno=int(input('Enter dept no:'))
name=input('Enter dept name:')
city=input('Enter city:')
fw.writerow([dno,name,city])
ans=input('Add More? (y/n): ')
print('-'*50)
print('='*50)
f.close()
elif ch==2:
f=open('dept.csv','r')
r=input('Enter DEPT no you want to search :')
fr=csv.reader(f)
for rec in fr:
if rec!=[]:
if rec[0]==r:
print("Dept no =",rec[0])
print("Dept Name =",rec[1])
print("City =",rec[2])
print('='*50)
f.close()
elif ch==3:
print("Exiting program")
print("="*50)
break
else:
print("Oops! Incorrect option")
OUTPUT :
-----------------------MENU-----------------------
1: create csv file
2: Search as per dept no.
3: Exit
==================================================
Enter choice: 1
--------------------------------------------------
Enter dept no:12
Enter dept name:Maths
Enter city:Indore
Add More? (y/n): y
--------------------------------------------------
Enter dept no:23
Enter dept name:Hindi
Enter city:Pune
Add More? (y/n): n
--------------------------------------------------
==================================================
Enter choice: 2
Enter DEPT no you want to search :23
Dept no = 23
Dept Name = Hindi
City = Pune
==================================================
Enter choice: 3
Exiting program
==================================================
18) Write a Python program to implement a stack using list
(PUSH & POP Operations on Stack).
print(“***********STACK DEMONSTRATION************”)
print("1. TRAVERSE ")
print("2. PUSH")
print("3. POP")
print("4. Exit")
print("="*50)
ans='y'
S=[]
while ans.lower()=='y':
ch = int(input("Enter your choice :"))
if ch==1:
if S==[]:
print("Stack is empty")
else:
n=len(S)
for i in range (-1,-n-1,-1):
print(S[i])
print("="*50)
elif ch==2:
a=input("Enter element to push: ")
S.append(a)
print("="*50)
elif ch==3:
if S==[]:
print("Stack is empty")
else:
p=S.pop()
print("Popped",p,"from stack")
print('='*50)
elif ch==4:
print("Exiting program")
print('='*50)
break
else:
print("Oops! Incorrect choice")
*************** STACK DEMONSTRATION***************
1. TRAVERSE
2. PUSH
3. POP
4. Exit
==================================================
Enter your choice :2
Enter element to push: Hello
==================================================
Enter your choice :2
Enter element to push: World
==================================================
Enter your choice :2
Enter element to push: Hi
==================================================
Enter your choice :1
Hi
World
Hello
==================================================
Enter your choice :3
Popped Hi from stack
==================================================
Enter your choice :1
World
Hello
==================================================
Enter your choice :4
Exiting program
==================================================
19) Write a python program using the function PUSH(Arr), where Arr
is a list of numbers. From this list push all numbers divisible by 5
into a stack implemented by using a list. Display the stack if it has at
least one element, otherwise display appropriate error messages.
def PUSH(Arr):
stack=[]
for i in Arr:
if i%5==0:
stack.append(i)
if len(stack)==0:
print("No element is divisible by 0")
else:
print("Stack:",stack)
#Example :-
Arr = [1,3,5,15,34,95]
PUSH(Arr)
OUTPUT :
def POP(Arr):
if len(Arr)==0:
print("Stack is empty")
else:
p=Arr.pop()
print("Popped",p,"from stack")
#Example :-
Arr = [1,3,5,15,34,95]
print("Stack:", Arr)
POP(Arr)
OUTPUT :
5) Find the min, max, sum, and average marks in a student table.
6) Add a new Column stream in the student table whose datatype is
varchar and size is 30.
11) Find the total number of girls who scored more than 75 marks
12) Write command to show all names starting with “r”
14) Create a school Database and then create a student table in the
school database and insert data. Implement the following SQL
commands on the student table:
ALTER table to add new attributes / modify data type / drop attribute
UPDATE table to modify data
ORDER By to display data in ascending / descending order
DELETE to remove tuple(s)
GROUP BY
Find the min, max, sum, count, count(*) and average
Creating Database:
Creating Table:
Updating data:
Deleting a tuple:
Table after deleting a tuple:
U: