Notre Dame of Holy Cross School (CBSE)
Notre Dame of Holy Cross School (CBSE)
Notre Dame of Holy Cross School (CBSE)
(CBSE)
DATE: 23.04.24
Ex.No.1. Program to Calculate Factorial of a number using function.
Aim:
To write a Python program to calculate factorial of a number using function.
Algorithm:
1. Start.
2. Get input number from the user.
3. Call user defined function by passing the number as an argument.
4. Calculate factorial of the
5. passed argument and return the value.
6. Print the result.
7. Stop.
Sample Output:
Input a number to compute the factorial: 5
Factorial of 5 is: 120
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
1
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 23.04.24
Ex.No.2. Program to find the number of upper case and lower case letters in
a string.
Aim:
To write a Python program to find the number of upper case and lowercase
letters in a string which is passed as an argument to the function.
Algorithm:
1. Start.
2. Read string input from the user.
3. Call the user defined function by passing string as an argument.
4. Find the number of upper case letters and lower case letters using
built in functions.
5. Print the result
6. Stop.
Sample Output:
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
3
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 25.04.24
Ex.No.3. Program to check if a Number is a Prime or not using function.
Aim:
To write a Python program to check if a Number is a Prime or not using
function.
Algorithm:
1. Start
2. Read the number from the user and pass it to the function.
3. Call the function to check whether the entered number is prime or not.
4. If the number is prime function returns True otherwise False.
5. Print the result.
6. Stop.
if test_prime(n):
print("Entered Number is Prime")
else:
print("Entered Number is not prime")
5
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Sample Output:
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
6
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 25.04.24
Aim:
To write a Python program to find the occurrence of any word in a
string which is passed as an argument to function.
Algorithm:
1. Start
2. Read input string from the user.
3. Read the word to be searched in the above string.
4. Call user defined function by passing string and the word as an argument.
5. Split the passed string and count number of words.
6. Return the count value.
7. Print the result.
8. Stop.
Sample Output:
Enter any sentence: Meena likes cake and Tina likes chocolate.
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
8
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 29.04.24
Ex.No.5. Program to generate random number between 1 – 6 to simulate the
dice.
Aim:
To write a Python program to simulate the dice using random module.
Algorithm:
1. Start.
2. Repeatedly generate random number using randint(1,6) and
display the number with equal time interval.
3. Press CTRL+C to stop generating random number.
4. Print the number.
5. Stop.
import random
import time
print("Press CTRL+C to stop the dice!!!") play='y'
while play=='y':
try:
while True:
for i in range(10):
print()
n = random.randint(1,6)
print(n)
time.sleep(0.5)
except KeyboardInterrupt:
print("\nYour Number is :",n)
ans=input("Play More? (Y) :")
if ans.lower()!='y':
play='n'
9
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
break
Sample Output:
Press CTRL+C to stop the
dice!!! 5
3
6
2
2
3
3
4
5
3
Your Number is: 3
Play More? (Y): n
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
10
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 29.04.24
Ex.No.6. Program to print Floyd's triangle.
Aim:
To write a Python Program to print Floyd’s Triangle.
Algorithm:
1. Start.
2. Using Nested For Loop we print the Triangle.
3. Stop.
#Floyd's triangle
n=int(input("Enter the number :"))
for i in range(n,0,-1):
for j in range(n,i-1,-1):
print (j,end=' ')
print('\n')
11
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
12
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 14.06.24
Ex.No.7. Program to count number of lines and words in a text file.
Aim:
To write a Python program to count the number of lines and words in a text
file.
Algorithm:
1. Start.
2. Create text file using notepad and save it.
3. Open the created text file in read mode.
4. Apply readlines() to read all the lines and count the number of lines.
5. Now set the file pointer to the beginning of file and read the entire file
and count the number of words using split().
6. Print the result
7. Stop.
Sample Output:
Number of Lines in a text file: 4
Number of Words in a text file: 23
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
13
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
14
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 21.06.24
Ex.No.8. Program to count number of vowels and consonants in a text file.
Aim:
To write a Python program to count number of vowels and consonants in a
text file.
Algorithm:
1. Start.
2. Create text file using notepad and save it.
3. Open the created text file in read mode.
4. Read each character from the text file and check whether it is vowel
or consonant.
5. Update the counter variable.
6. Print the result
7. Stop.
myfile=open("F:\\TextFile\\Poem.txt","r")
vowels="aeiouAEIOU"
count1=0 count2=0
data=myfile.read()
for i in data:
if i in vowels:
count1=count1+1
else:
count2=count2+1
print("Number of Vowels:",count1)
print("Number of Consonants:",count2)
15
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Sample Output:
Number of Vowels: 38 Number
of Consonants: 78
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
16
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 12.07.24
Ex.No.9. Program to copy the content of text file.
Aim:
To write a Python program to copy the content of text file which are starting
with ‘I’ to another file.
Algorithm:
1. Start.
2. Create Source text file using notepad and save it.
3. Open source file in read mode and target file in write mode.
4. Read each line from the source text file and check whether it is
starting with ‘I’.
4. Write the line into target file.
5. Print the content of target file.
6. Stop.
f=open("F:\\textfile\\poem.txt",'r')
f1=open("CopyPoem.txt","w")
while True:
txt=f.readline()
if len(txt)==0:
break
if txt[0]=='I':
f1.write(txt)
print("File Copied!")
print("Contents of new file:")
f.close()
17
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Sample Output:
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
18
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
19
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 22.07.24
Ex.No.10. Program to read a text file line by line and display each word
separated by '#'.
AIM:
#Program
f=open('India.txt', 'r')
contents=f.readlines()
for line in contents:
words=line.split()
for i in words:
print(i+'#', end='')
f.close()
Sample Output:
india#is#my#country#all#indians#are#my#brothers#and#sisters#i#love#my#coun
try#
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
20
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 05.08.24
Ex.No.11. Program to create binary file.
Aim:
To write a Python program to create binary file and store the details of
a student.
Algorithm:
1. Start.
2. Import pickle module and open binary file in write mode.
3. Get student details from the user and store it in dictionary variable.
4. Write the record into the file using dump().
5. Open the same file in read mode and read its content using load()
6. Display the content of file.
7. Stop.
stu={}
fin=open('student.dat','rb')
try:
print("File Contents:")
while True:
stu=pickle.load(fin)
print(stu)
except EOFError:
fin.close()
Sample Output:
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
22
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
23
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 19.08.24
Aim:
To write a Python program to update the content of binary file.
Algorithm:
1. Start.
2. Import pickle module and open binary file in read/write mode.
3. Get the roll number of a student to identify the record.
4. Read the file using load() and identify the record to be updated.
5. Update the mark of a student and write the updated record back into the
file using dump().
6. Now display the content of updated file.
7. Stop.
import pickle
stu={}
found=False
myfile=open('student.dat','rb+')
n=int(input("Enter Roll number of a student to update his mark:"))
try:
while True:
pos=myfile.tell()
stu=pickle.load(myfile)
if stu['RollNo']==n:
stu['Marks']+=10
myfile.seek(pos)
pickle.dump(stu,myfile)
found=True
24
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
except EOFError:
if found==False:
print("No matching record found!")
else:
print("Record Successfully Updated!")
try:
myfile.seek(0)
print("File Content after updation:")
while True:
stu=pickle.load(myfile)
print(stu)
except EOFError:
myfile.close()
myfile.close()
Sample Output:
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
25
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
26
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 09.09.24
Aim:
Algorithm:
1. Start.
2. Import csv module and open file in write mode.
3. Create writer object and write the product details into the file using
writerow().
4. Open the above created file in read mode using with statement.
5. Read the file using reader object.
6. Display the contents of file.
7. Stop.
27
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
print(“ ”)
myfile.close()
with open("Product.csv","r",newline='\n') as fh:
prod_reader=csv.reader(fh)
for rec in prod_reader:
print(rec)
Sample Output:
Product 1
Enter Product code: 104
Enter Product Name: Pen
Enter Price:200
Product 2
Enter Product code: 405 Enter
Product Name: Marker Enter
Price: 50
File created
successfully! File
Contents:
-----------------------
['CODE', 'NAME', 'PRICE']
['104', 'Pen', '200.0']
['405', 'Marker', '50.0']
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
28
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 14.09.24
Aim:
To write a Python program to create Stack using an element.
Algorithm:
1. Start
2. To push an item in the stack, use the list function append list.append(item)
3. To pop an item in the stack, use the list function pop list.pop()
4. To get the top most item in the stack, write list[-1]
5. Display the content of the Stack.
6. Stop.
# Program to create a Stack using Student details.
def check_stack_isEmpty(stk):
if stk==[]:
return True
else:
return False
def push(stk,e):
stk.append(e)
top = len(stk)-1
def display(stk):
if check_stack_isEmpty(stk):
print("Stack is Empty")
else:
top = len(stk)-1
print(stk[top],"-Top")
29
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
for i in range(top-1,-1,-1):
print(stk[i])
def pop_stack(stk):
if check_stack_isEmpty(stk):
return "UnderFlow"
else:
e = stk.pop()
if len(stk)==0:
top = None
else:
top = len(stk)-1
return e
def peek(stk):
if check_stack_isEmpty(stk):
return "UnderFlow"
else:
top = len(stk)-1
return stk[top]
s=[] # An empty list to store stack elements, initially its empty top = None
# This is top pointer for push and pop operation.
def main_menu():
while True:
print("Stack Implementation")
print("1 - Push")
print("2 - Pop")
print("3 - Peek")
print("4 - Display")
print("5 - Exit")
ch = int(input("Enter the your choice:"))
30
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
if ch==1:
el = int(input("Enter the value to push an element:"))
push(s,el)
elif ch==2:
e=pop_stack(s)
if e=="UnderFlow":
print("Stack is underflow!")
else:
print("Element popped:",e)
elif ch==3:
e=peek(s)
if e=="UnderFlow":
print("Stack is underflow!")
else:
print("The element on top is:",e)
elif ch==4:
display(s)
elif ch==5:
break
else:
print("Sorry, You have entered invalid option")
main_menu()
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
31
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
32
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Output
33
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 18.09.24
Algorithm:
1. Start
2. To push an item in the stack, use the list function append list.append(item)
3. To pop an item in the stack, use the list function pop list.pop()
4. To get the top most item in the stack, write list[-1]
5. Display the content of the Stack.
6. Stop.
34
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
def display():
global stk
global top
if top==-1:
isEmpty()
else:
top=len(stk)-1
print(stk[top],"<-top")
for i in range(top-1,-1,-1):
print(stk[i])
def pop_ele():
global stk
global top
if top==-1:
isEmpty()
else:
stk.pop()
top=top-1
def main():
while True:
line()
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Exit")
ch=int(input("Enter your choice:"))
if ch==1:
push()
print("Element Pushed")
elif ch==2:
pop_ele()
elif ch==3:
display()
else:
print("Invalid Choice")
35
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
break
main()
36
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Output
Result:
Thus, the above Python program has been executed and the output is
verified successfully.
37
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
38
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
PART B
DATE: 30.09.24
EX.No.16
5 sets of SQL queries using one/two tables.
In this section Practical file computer science class 12, 5 sets of SQL queries are
required. These queries can be performed either on one or two tables. So here
we go!
Productio Busines
Movie Movie Release
Type n s Cost
ID Name Date
Cost
The Kashmir
M001 Files Action 2022/01/26 1245000 1300000
M002 Attack Action 2023/01/28 1120000 1250000
M003 Looop Lapeta Thriller 2023/02/01 250000 300000
M004 Badhai Do Drama 2023/02/04 720000 68000
Shabaas h Biograph
M005 Mithu 2023/02/04 1000000 800000
y
M006 Gehraiyaan Romance 2023/02/11 150000 120000
ANSWERS
Output:
Type
Action
Thriller
Drama
Biography
Romance
40
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
41
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Moviename
Attack
Gehriyaan
Moviename
Loop Lapeta
Bdhai Do
Shabaash Mothu
Gehriyaan
42
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Queries
Set 2 (Based on Functions)
Answers:
mysql>Select pow(5,3);
Pow(5,3)
125
43
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
mysql>Select round(563.854741,-2);
Round(563.854741,-2)
600
mysql>Select mid(“Computer”,4,3);
Mid(“Computer”,4,3)
put
[4] select
concat(day(now()),concat(‘.’,month(now()),concat(‘.’,year(now()))))
“Date”;
Date
9.1.2022
mysql>Select right(“Media”,3);
right(“Media”,3)
dia
concat(moviename,concat(‘-‘,type))
The Kashmir Files - Action
Attack - Action
Loop Lapeta – Thriller
Bdhai Do – Drama
Shabaash Mothu - Biography
Gehriyaan - Romance
left(productioncost,4)
45
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
1245
1120
2500
7200
1000
1500
6 rows in set(0.00 sec)
right(businesscost,4)
0000
0000
0000
0000
0000
0000
6 rows in set(0.00 sec)
weekday(releasedate)
2
4
1
4
4
4
6 rows in set(0.00 sec)
46
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
dayname(releasedate)
Wednesday
Friday
Tuesday
Friday
Friday
Friday
6 rows in set(0.00 sec)
Queries
Set 3 (DDL Commands)
Suppose your school management has decided to conduct cricket matches
between students of Class XI and Class XII. Students of each class are asked
to join any one of the four teams – Team Titan, Team Rockers, Team Magnet
and Team Hurricane. During summer vacations, various matches will be
conducted between these teams. Help your sports teacher to do the following:
1. Create a database “Sports”.
2. Create a table “TEAM” with following considerations:
● It should have a column TeamID for storing an integer value between 1
to 9, which refers to unique identification of a team.
● Each TeamID should have its associated name (TeamName), which
should be a string of length not less than 10 characters.
● Using table level constraint, make TeamID as the primary key.
● Show the structure of the table TEAM using a SQL statement.
● As per the preferences of the students four teams were formed as
given below. Insert these four rows in TEAM table:
● Row 1: (1, Tehlka)
● Row 2: (2, Toofan)
● Row 3: (3, Aandhi)
● Row 3: (4, Shailab)
Show the contents of the table TEAM using a DML statement.
3. Now create another table MATCH_DETAILS and insert data as shown
below. Choose appropriate data types and constraints for each attribute.
MatchID Match First Second First Second
47
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Team Team
Date TeamID TeamID
Score Score
M1 2021/12/20 1 2 107 93
M2 2021/12/21 3 4 156 158
M3 2021/12/22 1 3 86 81
M4 2021/12/23 2 4 65 67
M5 2021/12/24 1 4 52 88
M6 2021/12/25 2 3 97 68
ANSWERS
[1] Create database sports.
mysql>create database sports;
Query Ok, 1 row affected(0.01sec)
mysql>desc team;
Inserting data:
49
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Teamid Teamname
1 Tehlka
2 Toofan
3 Aandhi
4 Shailab
50
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
M3 2021-12-22 1 3 86 81
M4 2021-12-23 2 4 65 67
M5 2021-12-24 1 4 52 88
M6 2021-12-25 2 3 97 68
Queries
Set 4 (Based on Two Tables)
Answers:
d
M1 2021-12-20 1 2 107 93
M3 2021-12-22 1 3 86 81
M6 2021-12-25 2 3 97 68
Teamname
Tehlka
Aandhi
Shailab
Toofan
4 rows in set(0.03 sec)
52
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
(‘Aandhi’,’Shailab’);
Matchid Matchdate
M2 2021-12-21
M4 2021-12-23
2 rows in set(0.00 sec)
M1 Tehlka 1 2 2021-12-20
M2 Aandhi 3 4 2021-12-21
M3 Tehlka 1 3 2021-12-22
M4 Toofan 2 4 2021-12-23
M5 Tehlka 1 4 2021-12-25
M6 Toofan 2 3 2021-12-25
Queries
Set 5 (Group by, Order By)
Dcode Max(unitprice)
103 5
55
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
102 35
101 15
3 rows in set (0.01 sec)
Dcode Avg(unitprice)
102 13.7500
101 9.0000
56
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Dcode Sum(qty)
103 210
102 445
101 350
57
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 15.11.24
Ex.No.17. Program to fetch all the records from Student Table. Aim:
To write a Python program to fetch all the records from the Student Table.
Algorithm:
1. Start.
2. Import mysql.connector module.
3. Establish connection using connect().
4. Create Cursor instance using cursor().
5. Fetch all the records from the table using fetchall().
6. Traverse the result set using for loop and print the records.
7. Close the connection using close().
8. Stop.
Program:
58
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Sample Output:
Result:
Thus, the above query has been executed and the output is
verified successfully.
59
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 21.11.24
Aim:
Algorithm:
1. Start.
2. Import mysql.connector module.
3. Establish connection using connect().
4. Create Cursor instance using cursor().
5. Execute SQL query with string formatting.
6. Fetch all the records from the table using fetchall().
7. Traverse the result set using for loop and print the records.
8. Close the connection using close().
9. Stop.
Program:
60
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Sample Output:
Result:
Thus, the above query has been executed and the output is
verified successfully.
61
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 03.12.24
Aim:
Algorithm:
1. Start.
2. Import mysql.connector module.
3. Establish connection using connect().
4. Create Cursor instance using cursor().
5. Execute insert command with string formatting and apply commit() to
save the changes permanently.
6. Execute select query and fetch all the records using fetchall().
7. Traverse the result set using for loop and print the records.
8. Close the connection using close().
9. Stop.
Program:
# Program to insert new record into table.
62
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
cur=con.cursor()
Sample Output:
Result:
Thus, the above query has been executed and the output is
verified successfully.
63
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
64
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
DATE: 03.12.24
Aim:
Algorithm:
1. Start.
2. Import mysql.connector module.
3. Establish connection using connect().
4. Create Cursor instance using cursor().
5. Execute insert command with string formatting and apply commit() to
save the changes permanently.
6. Execute select query and fetch all the records using fetchall().
7. Traverse the result set using for loop and print the records.
8. Close the connection using close().
9. Stop.
Program:
65
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
Sample Output:
Result:
Thus, the above query has been executed and the output is
verified successfully.
66
NOTRE DAME OF HOLY CROSS SCHOOL
(CBSE)
67