PYTHON PROGRAMMING
LAB MANUAL
DAV Public School
DAV Public School
Basanti Colony, Rourkela-12
Contents
#WAP to find the largest and smallest number in a list/tuple. ..................................................................... 3
#WAP with a user-defined function with string as a parameter which replaces all vowels in the string with
‘*’. .................................................................................................................................................................. 3
#WAF to accept string and display the vowels of the string present in it also display the index of it. ......... 4
#WAF to accept a string and display the number of Capital Alphabets, Small Alphabets and Digits
separately with suitable heading. ................................................................................................................. 5
#WAF to calculate number of fruits in the list with '@' symbol in between. ............................................... 6
#WAF CHANGE to modify the vowels present in a string to @ and display both the strings. ..................... 6
#WAP which will work like a random number generator that generates random numbers between 1 and
6 like a dice.................................................................................................................................................... 7
#WAF to create a dictionary with roll number & name. ............................................................................... 8
#WAP to store the names of 5 students in a file(.txt). .................................................................................. 9
#WAP to read a file. ...................................................................................................................................... 9
#WAP to display the words whose any of the even positions is a vowel from the file Students.txt. ......... 10
#WAF code to find the size of the file in bytes, the number of lines, number of words and number of
characters. ................................................................................................................................................... 11
#WAP to store the names of 5 students with their roll number and marks in a file (CSV). ........................ 12
#WAP to show the names of 5 students with their roll number and marks in a file(CSV). ........................ 14
#WAP to search the record of a particular student from CSV file on the basis of inputted name. ............ 15
PYTHON
#WAP to find the largest and smallest number in a list/tuple.
Code:
# creating empty list
list1 = []
num = int(input("Enter number of elements in list: "))
# iterating till num to append elements in list
for i in range(1, num + 1):
ele= int(input("Enter elements: "))
list1.append(ele)
print("Largest element is:", max(list1))
print("Smallest element is:", min(list1))
Output:
===== RESTART: C:/Users/HP/AppData/Local/Programs/Python/Python313/ex-1.py =====
Enter number of elements in list: 5
Enter elements: 34
Enter elements: 45
Enter elements: 76
Enter elements: 77
Enter elements: 76
Largest element is: 77
Smallest element is: 34
#WAP with a user-defined function with string as a parameter which
replaces all vowels in the string with ‘*’.
Code:
def strep(str):
s=list(str)
for i in range(len(s)):
if s[i] in 'aeiouAEIOU':
s[i]='*'
n= "".join(s)
return n
line = input("Enter string: ")
print("Orginal String")
print(line)
print("After replacing Vowels with '*'")
print(strep(line))
Output:
= RESTART: C:/Program Files/Python312/Python/Function/replce.py
Enter string: Spiderman
Orginal String
Spiderman
After replacing Vowels with '*'
Sp*d*rm*n
#WAF to accept string and display the vowels of the string present in it
also display the index of it.
Code:
def name(s):
for i in range(0,len(s)):
if s[i] in 'AEIOUaeiou':
print("Index:",i)
print("Vowels:",s[i])
x=input("Enter a string:")
name(x)
Output:
========= RESTART: C:/Program Files/Python312/Python/Function/vowel.py =========
Enter a string:Biswajeet
Index: 1
Vowels: i
Index: 4
Vowels: a
Index: 6
Vowels: e
Index: 7
Vowels: e
#WAF to accept a string and display the number of Capital Alphabets,
Small Alphabets and Digits separately with suitable heading.
Code:
def sen(s):
a=b=c=0
for i in range(0,len(s)):
if(s[i].isupper()):
a+=1
elif(s[i].islower()):
b+=1
else:
c+=1
print("Capital Letters:",a)
print("Small Letters:",b)
print("No of Digits:",c)
x=input("Enter a String:")
sen(x)
Output:
= RESTART: C:/Program Files/Python312/Python/Function/no of letters.py
Enter a String:BiswajeetMuNda19114
Capital Letters: 3
Small Letters: 11
No of Digits: 5
#WAF to calculate number of fruits in the list with '@' symbol in
between.
Code:
def frut(f):
fruits=dict()
for i in f:
index=i.lower()
if i in fruits:
fruits[index]+=1
else:
fruits[index]=1
m=max(fruits.values())
for i,j in fruits.items():
print(i,j,sep='@')
ft=['Apples','Grapes','Orange','apples','tomato','grapes']
frut(ft)
Output:
= RESTART: C:/Program Files/Python312/Python/Function/Fruits.py
apples@2
grapes@2
orange@1
tomato@1
#WAF CHANGE to modify the vowels present in a string to @ and display
both the strings.
Code:
def name(x):
p=''
v="aeiouAEIOU"
for i in range(0,len(x)):
if x[i] in v:
p=p+'@'
else:
p=p+x[i]
print(p)
a=input("Enter a strng:")
name(a)
Output:
= RESTART: C:/Program Files/Python312/Python/Function/[email protected]
Enter a strng:Raju
R@j@
#WAP which will work like a random number generator that generates
random numbers between 1 and 6 like a dice.
Code:
import random
def roll_dice():
print (random.randint(1,6))
print("""Welcome to my python random dice program!
To start press enter! Whenever you are over, type quit.""")
flag = True
while flag:
user_prompt = input(">")
if user_prompt.lower()=="quit":
flag = False
else:
print ("Rolling dice...\nYour number is:")
roll_dice()
Output:
= RESTART: C:/Program Files/Python312/Python/Function/dice.py
Welcome to my python random dice program!
To start press, enter! Whenever you are over, type quit.
>
Rolling dice...
Your number is:
>
Rolling dice...
Your number is:
>quit
#WAF to create a dictionary with roll number & name.
Code:
def student(n):
result={}
for i in range(n):
rollno=int(input("Enter the roll number:"))
name=input("Enter the name:")
result[rollno]=[name]
print(result)
a=int(input("Enter the number of students:"))
student(a)
Output:
= RESTART: C:/Program Files/Python312/Python/Function/Student list.py
Enter the number of students:3
Enter the roll number:08
Enter the name:Biswajeet
Enter the roll number:19
Enter the name:Om
Enter the roll number:06
Enter the name:Bhavin
{8: ['Biswajeet'], 19: ['Om'], 6: ['Bhavin']}
#WAP to store the names of 5 students in a file(.txt).
Code:
f=open("Students.txt","w")
for i in range(5):
name=input("Enter the name:")
f.write(name+"\n")
f.close()
Output:
======= RESTART: C:\Program Files\Python312\Python\k.py ======
Enter the name:Kunal
Enter the name:Raj
Enter the name:Om
Enter the name:Biswajeet
Enter the name: Rahul
Text file display:-
#WAP to read a file.
Code:
f=open("Students.txt","r")
s=f.read()
print(s)
Output:
= RESTART: C:/Program Files/Python312/Python/File Handeling/Readfile.py
Kunal
Raj
Om
Biswajeet
Rahul
#WAP to display the words whose any of the even positions is a vowel
from the file Students.txt.
Code:
f=open("Students.txt","r")
c=f.read().split()
v="aeiouAEIOU"
s=''
for i in c:
s=i
for j in range(0,len(s),2):
if s[j] in v:
print(s)
f.close()
Output:
= RESTART: C:/Program Files/Python312/Python/File Handeling/vowwel.py
Om
Biswajeet
Biswajeet
#WAF code to find the size of the file in bytes, the number of lines,
number of words and number of characters.
Code:
import os
filesize = 0
lines = 0
words = 0
letters = 0
for line in open("Students.txt"):
lines += 1
letters += len(line)
#get the size of file
filesize = os.path.getsize("Students.txt")
# A flag that signals the location outside the word.
pos = 'out'
for letter in line:
if letter != ' ' and pos == 'out':
words += 1
pos = 'in'
elif letter == ' ':
pos = 'out'
print("Size of File is",filesize,'bytes')
print("No of Lines:", lines)
print("No of Words:", words)
print("No of Letters:", letters)
Output:
= RESTART: C:/Program Files/Python312/Python/File Handeling/Filesize.py
Size of File is 34 bytes
No of Lines: 5
No of Words: 5
No of Letters: 29
# Remove all the lines that contain the character `a' in a file and write it
to another file.
Code:
f1 = open("Students.txt")
f2 = open("Students 2.txt","w")
for line in f1:
if 'a' not in line:
f2.write(line)
print('## File Copied Successfully! ##')
f1.close()
f2.close()
f2 = open("Students 2.txt","r")
print(f2.read())
Output:
= RESTART: C:/Program Files/Python312/Python/File Handeling/transfer.py
## File Copied Successfully! ##
Om
#WAP to store the names of 5 students with their roll number and marks
in a file (CSV).
Code:
import csv
f=open("5 Students.csv","w", newline='')
w=csv.writer(f,delimiter=',')
for i in range(5):
r=int(input("Enter the roll no:"))
n=input("Enter the name:")
m=int(input("Enter the marks:"))
rec=[r,n,m]
w.writerow(rec)
f.close()
Output:
= RESTART: C:\Program Files\Python312\Python\File Handeling\5 students.py
Enter the roll no:1
Enter the name:Abhisekh
Enter the marks:61
Enter the roll no:8
Enter the name:Biswajeet
Enter the marks:58
Enter the roll no:11
Enter the name:Harsh
Enter the marks:50
Enter the roll no:19
Enter the name:Om
Enter the marks:60
Enter the roll no:25
Enter the name:Rahul
Enter the marks:45
#WAP to show the names of 5 students with their roll number and marks
in a file(CSV).
Code:
import csv
with open("5 Students.csv")as f:
r=csv.reader(f)
for x in r:
print(x)
Output:
= RESTART: C:/Program Files/Python312/Python/File Handeling/5 studentsread.py
['Rollno', 'Name', 'Marks']
['1', 'Abhisekh', '61']
['8', 'Biswajeet', '58']
['11', 'Harsh', '50']
['19', 'Om', '60']
['25', 'Rahul', '45']
#WAP to search the record of a particular student from CSV file on the
basis of inputted name.
Code:
import csv
number = input('Enter the student roll number:')
with open('5 Students.csv') as f:
csvfile = csv.reader(f, delimiter=',')
#loop through csv list
for row in csvfile:
#if current rows index value (here 0) is equal to input, print that row
if number==row[0]:
print(row)
print('Entry found')
else:
pass
Output:
========= RESTART: C:\Users\KUNAL\Documents\marks.py =========
Enter the student roll number:8
['8', 'Biswajeet', '58']
Entry found
# Write a python program to implement a stack using a list data-
structure.
Code:
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 "underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def peek(stk):
if isempty(stk):
return "underflow"
else:
top=len(stk)-1
return stk[top]
def display(stk):
if isempty(stk):
print('stack is empty')
else:
top=len(stk)-1
print(stk[top],'<-top')
for i in range(top-1,-1,-1):
print(stk[i])
#Driver Code
def main():
stk=[]
top=None
while True:
print('''stack operation
1.push
2.pop
3.peek4.display
5.exit''')
choice=int (input('enter choice:'))
if choice==1:
item=int(input('enter item:'))
push(stk,item)
elif choice==2:
item=pop(stk)
if item=="underflow":
print('stack is underflow')
else:
print('poped')
elif choice==3:
item=peek(stk)
if item=="underflow":
print('stack is underflow')
else:
print('top most item is:',item)
elif choice==4:
display(stk)
elif choice==5:
break
else:
print('invalid')
exit()
main(
Output:
===== RESTART: C:\Users\HP\AppData\Local\Programs\Python\Python313\ex-1.py =====
stack operation
1.push
2.pop
3.peek
4.display
5.exit
enter choice:1
enter item:34
stack operation
1.push
2.pop
3.peek4.display
5.exit
enter choice:1
enter item:67
stack operation
1.push
2.pop
3.peek
4.display
5.exit
enter choice:2
poped
stack operation
1.push
2.pop
3.peek
4.display
5.exit
enter choice:3
top most item is: 34
stack operation
1.push
2.pop
3.peek
4.display
5.exit
enter choice:4
34 <-top
stack operation
1.push
2.pop3.peek
4.display
5.exit
enter choice:5
# Write a program to implement a queue using a list data structure.
Code:
# Function to check Queue is empty or not
def isEmpty(qLst):
if len(qLst)==0:
return 1
else:
return 0
# Function to add elements in Queue
def Enqueue(qLst,val):
qLst.append(val)
if len(qLst)==1:
front=rear=0
else:
rear=len(qLst)-1
# Function to Delete elements in Queue
def Dqueue(qLst):
if isEmpty(qLst):
return "UnderFlow"
else:
val = qLst.pop(0)
if len(qLst)==0:
front=rear=None
return val
# Function to Display top element of Queue
def Peek(qLst):
if isEmpty(qLst):
return "UnderFlow"
else:
front=0
return qLst[front]
# Function to Display elements of Queue
def Display(qLst):
if isEmpty(qLst):
print("No Item to Dispay in Queue....")
else:
tp = len(qLst)-1
print("[FRONT]",end=' ')
front = 0
i = front
rear = len(qLst)-1
while(i<=rear):
print(qLst[i],'<-',end=' ')
i += 1
print()
# Driver function
def main():
qList = []
front = rear = 0
while True:
print()
print("##### QUEUE OPERATION #####")
print("1. ENQUEUE ")
print("2. DEQUEUE ")
print("3. PEEK ")
print("4. DISPLAY ")
print("0. EXIT ")
choice = int(input("Enter Your Choice: "))
if choice == 1:
ele = int(input("Enter element to insert"))
Enqueue(qList,ele)
elif choice == 2:
val = Dqueue(qList)
if val == "UnderFlow":
print("Queue is Empty")
else:
print("\n Deleted Element was : ",val)
elif choice==3:
val = Peek(qList)
if val == "UnderFlow":
print("Queue is Empty")
else:
print("Item at Front: ",val)
elif choice==4:
Display(qList)
elif choice==0:
print("Good Luck......")
break
main()
Output:
===== RESTART: C:\Users\HP\AppData\Local\Programs\Python\Python313\ex-1.py =====##### QUEUE
OPERATION #####
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. DISPLAY
0. EXIT
Enter Your Choice: 1
Enter element to insert23
##### QUEUE OPERATION #####
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. DISPLAY
0. EXIT
Enter Your Choice: 1
Enter element to insert34
##### QUEUE OPERATION #####
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. DISPLAY
0. EXIT
Enter Your Choice: 2
Deleted Element was : 23
##### QUEUE OPERATION #####
1. ENQUEUE2. DEQUEUE
3. PEEK
4. DISPLAY
0. EXIT
Enter Your Choice: 3
Item at Front: 34
##### QUEUE OPERATION #####
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. DISPLAY
0. EXIT
Enter Your Choice: 4
[FRONT] 34 <-
##### QUEUE OPERATION #####
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. DISPLAY
0. EXIT
Enter Your Choice: 0
Good Luck......
SQL
To display the all information of Sales department.
;
To display all information about the employees whose name starts
with 'K'.
To list the name of female employees who are in Finance department.
;
To display name and sex of all the employees whose age is in the range
of 40 to 50 in ascending order of their name.
To count the number of female employees with age greater than 20
and who are in Accounts department.
To display the name of all Games with their GCodes.
To display details of those games which are having Prize Money more
than 7000.
To display the content of the GAMES table in ascending order of
ScheduleDate.
*
To display sum of PrizeMoney for each of the Numberof participation
groupings ( as shown in column number 2 or 4).
To display the sum of prize money of all games.
Display the sum of all Loan Amount whose Interest rate is greater than
10.
Display the Maximum Interest from Loans table.
Display the count of all loan holders whose name ends with ‘SHARMA’.
( )
;
Display the count of all loan holders whose Interest is NULL.
( )
Display the Interest-wise details of Loan Account Holders.
*
Display the Interest-wise details of Loan Account Holders with at least
10 installments remaining
Display the Interest-wise count of all loan holders whose Installment
due is more than 5 in each group.
5;
Add one more column name ‘Address’ to the LOANS table.
Reduce Interest rate by 1 of all loan holders whose Interest is not NULL.
Delete the record of customer whose account number is 105.