0% found this document useful (0 votes)
5 views

Computer Science 2024-25

The document is a practical file for Computer Science (083) focusing on Python programming for Class XII students. It includes a list of practical exercises such as calculating factorials, checking prime numbers, and implementing data structures like stacks and queues. Additionally, it contains sample code and outputs for various programming tasks, demonstrating the application of Python in different scenarios.

Uploaded by

Razor Iron Gamer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Computer Science 2024-25

The document is a practical file for Computer Science (083) focusing on Python programming for Class XII students. It includes a list of practical exercises such as calculating factorials, checking prime numbers, and implementing data structures like stacks and queues. Additionally, it contains sample code and outputs for various programming tasks, demonstrating the application of Python in different scenarios.

Uploaded by

Razor Iron Gamer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

PRACTICAL FILE

COMPUTER SCIENCE (083)

PYTHON

Student Name :

Grade : XII- _____________________________________________________

Roll No. :
Certificate

This is to certify that _

student of Class- XII Science has successfully completed their Computer

Science (New - 083) Practical File.

Computer Teacher External Examiner

Principal
INDEX
PRACTICAL FILE- COMPUTER SCIENCE (083)

LIST OF PRACTICALS (2024-2025) CLASS-XII

Programming Language: - Python

Sr. No Name of Practical


1 Input any number from user and calculate factorial of a number

2 Input any number from user and check it is Prime no. or not
3 Write a program to find sum of elements of List recursively
4 Write a program to calculate the nth term of Fibonacci series
5 Program to search any word in given string/sentence
Program to read and display file content line by line with each word separated
6
by “#”
Program to read the content of file and display the total number of consonants,
7
uppercase, vowels and lower case characters
Program to create binary file to store Rollno and Name, Search any Rollno and
8
display name if Rollno found otherwise “Rollno not found”
Program to create binary file to store Rollno,Name and Marks and update marks
9
of entered Rollno
Program to read the content of file line by line and write it to another file except
10
for the lines contains “a” letter in it.
Program to create CSV file and store empno,name,salary and search any empno
11
and display name,salary and if not found appropriate message.
12 Program to generate random number 1-6, simulating a dice
13 Program to implement Stack in Python using List
14 Program to implement Queue in Python using List
Program to take 10 sample phishing email, and find the most common word
15
occurring
16 Program to create 21 Stick Game so that computer always wins
Program to connect with database and store record of employee and display
17
records.
Program to connect with database and search employee number in table
18 employee and display record, if empno not found display appropriate message.

Program to connect with database and update the employee record of entered
19
empno.
Program to connect with database and delete the record of entered employee
20
number.
Date : Experiment No: 1

Program 1: Input any number from user and calculate factorial of a number

# Program to calculate factorial of entered number


num = int(input("Enter any number :"))
fact = 1
n = num # storing num in n for printing
while num>1: # loop to iterate from n to 2
fact = fact * num
num-=1

print("Factorial of ", n , " is :",fact)

OUTPUT
Enter any number :6
Factorial of 6 is : 720

Page : 1
Page : 3
Date : Experiment No: 4

Program 1: Write a program to calculate the nth term of Fibonacci series

#Program to find 'n'th term of fibonacci series


#Fibonacci series : 0,1,1,2,3,5,8,13,21,34,55,89,...
#nth term will be counted from 1 not 0

def nthfiboterm(n):
if n<=1:
return n
else:
return (nthfiboterm(n-1)+nthfiboterm(n-2))

num = int(input("Enter the 'n' term to find in fibonacci :"))


term =nthfiboterm(num)
print(num,"th term of fibonacci series is :",term)

OUTPUT
Enter the 'n' term to find in fibonacci :10
10 th term of fibonacci series is : 55

Page : 4
Page : 5
Date : Experiment No: 6

Program 1: Program to read and display file content line by line with each
word separated by „#‟

#Program to read content of file line by line


#and display each word separated by '#'

f = open("file1.txt")

for line in f:
words = line.split()
for w in words:
print(w+'#',end='')
print()
f.close()

NOTE : if the original content of file is:


India is my country
I love python
Python learning is fun

OUTPUT
India#is#my#country#
I#love#python#
Python#learning#is#fun#

Page : 6
Page : 7
Page : 8
OUTPUT
Enter Roll Number :1
Enter Name :Amit
Add More ?(Y)y

Enter Roll Number :2


Enter Name :Jasbir
Add More ?(Y)y

Enter Roll Number :3


Enter Name :Vikral
Add More ?(Y)n

Enter Roll number to search :2


## Name is : Jasbir ##
Search more ?(Y) :y

Enter Roll number to search :1


## Name is : Amit ##
Search more ?(Y) :y

Enter Roll number to search :4


####Sorry! Roll number not found ####
Search more ?(Y) :n

Page : 9
Page : 10
OUTPUT
Enter Roll Number :1
Enter Name :Amit
Enter Marks :99
Add More ?(Y)y

Enter Roll Number :2


Enter Name :Vikrant
Enter Marks :88
Add More ?(Y)y

Enter Roll Number :3


Enter Name :Nitin
Enter Marks :66
Add More ?(Y)n

Enter Roll number to update :2


## Name is : Vikrant ##
## Current Marks is : 88 ##
Enter new marks :90
## Record Updated ##
Update more ?(Y) :y

Enter Roll number to update :2


## Name is : Vikrant ##
## Current Marks is : 90 ##
Enter new marks :95
## Record Updated ##
Update more ?(Y) :n

Page : 11
Page : 12
Page : 13
Enter Employee Number 1

Enter Employee Name Amit

Enter Employee Salary :90000

## Data Saved... ##

Add More ?y

Enter Employee Number 2

Enter Employee Name Sunil

Enter Employee Salary :80000

## Data Saved... ##

Add More ?y

Enter Employee Number 3

Enter Employee Name Satya

Enter Employee Salary :75000

## Data Saved... ##

Add More ?n

Enter Employee Number to search :2

============================

NAME : Sunil

SALARY : 80000

Search More ? (Y)y

Enter Employee Number to search :3

============================

NAME : Satya

SALARY : 75000

Search More ? (Y)y

Enter Employee Number to search :4

==========================

EMPNO NOT FOUND

==========================

Search More ? (Y)n

Page : 14
Page : 15
Date : Experiment No: 13

Program 1: Program to implement Stack in Python using List

def isEmpty(S):
if len(S)==0:
return True
else:
return False

def Push(S,item):
S.append(item)
top=len(S)-1

def Pop(S):
if isEmpty(S):
return "Underflow"
else:
val = S.pop()
if len(S)==0:
top=None
else:
top=len(S)-1
return val

def Peek(S):
if isEmpty(S):
return "Underflow"
else:
top=len(S)-1
return S[top]

def Show(S):
if isEmpty(S):
print("Sorry No items in Stack ")
else:
t = len(S)-1
print("(Top)",end=' ')
while(t>=0):
print(S[t],"<==",end=' ')
t-=1
print()

Page : 16
# main begins here
S=[] #Stack
top=None
while True:
print("**** STACK DEMONSTRATION ******")
print("1. PUSH ")
print("2. POP")
print("3. PEEK")
print("4. SHOW STACK ")
print("0. EXIT")
ch = int(input("Enter your choice :"))
if ch==1:
val = int(input("Enter Item to Push :"))
Push(S,val)
elif ch==2:
val = Pop(S)
if val=="Underflow":
print("Stack is Empty")
else:
print("\nDeleted Item was :",val)
elif ch==3:

val = Peek(S)
if val=="Underflow":
print("Stack Empty")
else:
print("Top Item :",val)
elif ch==4:

Show(S)
elif ch==0:
print("Bye")
break

OUTPUT
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :1
Enter Item to Push :10

Cont…
Page : 17
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :1
Enter Item to Push :20

**** STACK DEMONSTRATION ******


1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :1
Enter Item to Push :30

**** STACK DEMONSTRATION ******


1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :4
(Top) 30 <== 20 <== 10 <==

**** STACK DEMONSTRATION ******


1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :3
Top Item : 30

**** STACK DEMONSTRATION ******


1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :2

Deleted Item was : 30


Page : 18
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :4
(Top) 20 <== 10 <==

**** STACK DEMONSTRATION ******


1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :0
Bye

Page : 19
Date : Experiment No: 14

Program 1: Program to implement Queue in Python using List

def isEmpty(Q):
if len(Q)==0:
return True
else:
return False

def Enqueue(Q,item):
Q.append(item)
if len(Q)==1:
front=rear=0
else:
rear=len(Q)-1
def Dequeue(Q):
if isEmpty(Q):
return "Underflow"
else:
val = Q.pop(0)
if len(Q)==0:
front=rear=None
return val
def Peek(Q):
if isEmpty(Q):
return "Underflow"
else:
front=0
return Q[front]
def Show(Q):
if isEmpty(Q):
print("Sorry No items in Queue ")
else:
t = len(Q)-1
print("(Front)",end=' ')
front = 0
i=front
rear = len(Q)-1
while(i<=rear):
print(Q[i],"==>",end=' ')
i+=1
print()
Page : 20 Cont…
Q=[] #Queue
front=rear=None
while True:
print("**** QUEUE DEMONSTRATION ******")
print("1. ENQUEUE ")
print("2. DEQUEUE")
print("3. PEEK")
print("4. SHOW QUEUE ")
print("0. EXIT")
ch = int(input("Enter your choice :"))
if ch==1:
val = int(input("Enter Item to Insert :"))
Enqueue(Q,val)
elif ch==2:
val = Dequeue(Q)
if val=="Underflow":
print("Queue is Empty")
else:
print("\nDeleted Item was :",val)
elif ch==3:

val = Peek(Q)
if val=="Underflow":
print("Queue Empty")
else:
print("Front Item :",val)
elif ch==4:

Show(Q)
elif ch==0:
print("Bye")
break

OUTPUT
**** QUEUE DEMONSTRATION ******
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. SHOW QUEUE
0. EXIT
Enter your choice :1
Enter Item to Insert :10
**** QUEUE DEMONSTRATION ******
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. SHOW QUEUE
0. EXIT
Enter your choice :1 Cont…
Page : 21
Enter Item to Insert :20
**** QUEUE DEMONSTRATION ******
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. SHOW QUEUE
0. EXIT
Enter your choice :1
Enter Item to Insert :30
**** QUEUE DEMONSTRATION ******
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. SHOW QUEUE
0. EXIT
Enter your choice :4
(Front) 10 ==> 20 ==> 30 ==>
**** QUEUE DEMONSTRATION ******
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. SHOW QUEUE
0. EXIT
Enter your choice :3
Front Item : 10
**** QUEUE DEMONSTRATION ******
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. SHOW QUEUE
0. EXIT
Enter your choice :2
Deleted Item was : 10
**** QUEUE DEMONSTRATION ******
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. SHOW QUEUE
0. EXIT
Enter your choice :4
(Front) 20 ==> 30 ==>
**** QUEUE DEMONSTRATION ******
1. ENQUEUE
2. DEQUEUE
3. PEEK
4. SHOW QUEUE
0. EXIT
Enter your choice :0
Bye
Page : 22
Page : 23
Page : 24
OUTPUT

==== Welcome To Stick Picking Game :: Computer Vs User =====


Rule: 1) Both User and Computer can pick sticks between 1 to 4 at a time
2) Whosoever picks the last stick will be the looser
==== Lets Begin ======
Enter Your Name :RAJ
ooooooooooooooooooooo
|||||||||||||||||||||
|||||||||||||||||||||
|||||||||||||||||||||
|||||||| |||| |||||||||
You Can Pick stick between 1 to 4
RAJ: Enter Number of Stick to Pick3
oooooooooooooooooo
||||||||||||||||||
||||||||||||||||||
||||||||||||||||||
||||||||||||||||||
************************************************************
Press any key...
Computer Picks : 2 Sticks
oooooooooooooooo
||||||||||||||||
||||||||||||||||
||||||||||||||||
||||||||||||||||
************************************************************
Press any key...
You Can Pick stick between 1 to 4
RAJ: Enter Number of Stick to Pick4
oooooooooooo
||||||||||||
||||||||||||
||||||||||||
||||||||||||
************************************************************
Press any key...
Computer Picks : 1 Sticks
ooooooooooo
|||||||||||
|||||||||||
|||||||||||
|||||||||||
************************************************************
Press any key...
You Can Pick stick between 1 to 4
RAJ: Enter Number of Stick to Pick2
ooooooooo
|||||||||
|||||||||
|||||||||
||||||||| Page : 25
************************************************************
Press any key...
Computer Picks : 3 Sticks
oooooo
||||||
||||||
||||||
||||||
************************************************************
Press any key...
You Can Pick stick between 1 to 4
RAJ: Enter Number of Stick to Pick3
ooo
|||
|||
|||
|||
************************************************************
Press any key...
Computer Picks : 2 Sticks
o
|
|
|
|
## WINNER : COMPUTER ##
************************************************************
Press any key...

Page : 26
Page : 27
OUTPUT

1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :1
Enter Employee Number :1
Enter Name :AMIT
Enter Department :SALES
Enter Salary :9000
## Data Saved ##
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :1
Enter Employee Number :2
Enter Name :NITIN
Enter Department :IT
Enter Salary :80000
## Data Saved ##
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :2
EMPNO NAME DEPARTMENT SALARY
1 AMIT SALES 9000
2 NITIN IT 80000
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :0
## Bye!! ##

Page : 28
Page : 29
Program 119 Create a student table 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 and find the min, max, sum, count and average

#Switched to a database
mysql> USE GVKCV;
Database changed

#Creating table student


mysql> create table student
-> (ROLLNO INT NOT NULL PRIMARY KEY,
-> NAME CHAR(10),
-> TELUGU CHAR(10),
-> HINDI CHAR(10),
-> MATHS CHAR(10));
Query OK, 0 rows affected (1.38 sec)

#Inserting values into table


mysql> insert into student
-> values(101,"student1",50,51,52),
-> (102,"student2",60,61,62),
-> (103,"student3",70,71,72),
-> (104,"student4",80,81,82),
-> (105,"student5",90,91,92),
-> (106,"student6",40,41,42),
-> (107,"student7",63,64,65);
Query OK, 7 rows affected (0.24 sec)
Records: 7 Duplicates: 0 Warnings: 0

#Adding new attribute computers


mysql> alter table student
-> add (computers char(10));
Query OK, 0 rows affected (1.13 sec)
Records: 0 Duplicates: 0 Warnings: 0

#Describing table
mysql> desc student;
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |
| computers | char(10) | YES | | NULL | |
+ + + + + + +
6 rows in set (0.21 sec)

#Modifying the datatype


mysql> alter table student
-> modify column computers varchar(10);
Query OK, 7 rows affected (2.38 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> desc student;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |
| computers | varchar(10) | YES | | NULL | |
+ + + + + + +
6 rows in set (0.11 sec)

#Droping a attribute
mysql> alter table student
-> drop column computers;
Query OK, 0 rows affected (0.93 sec)
Records: 0 Duplicates: 0 Warnings: 0

#Describing table
mysql> desc student;
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |
+ + + + + + +
5 rows in set (0.14 sec)

#UPDATE DATA TO MODIFY DATA


#ACTUAL DATA
mysql> select *from student;
+ + + + + +
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+ + + + + +
| 101 | student1 | 50 | 51 | 52 |
| 102 | student2 | 60 | 61 | 62 |
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
| 106 | student6 | 40 | 41 | 42 |
| 107 | student7 | 63 | 64 | 65 |
+ + + + + +
7 rows in set (0.00 sec)

#UPDATE THE MARKS FOR ATTRIBUTE TELUGU FOR THE STUDENT101


mysql> UPDATE STUDENT
-> SET TELUGU=99
-> WHERE ROLLNO=101;
Query OK, 1 row affected (0.12 sec)
Rows matched: 1 Changed: 1 Warnings: 0

#DATA IN THE TABLE AFTER UPDATING


mysql> SELECT *FROM STUDENT;
+ + + + + +
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+ + + + + +
| 101 | student1 | 99 | 51 | 52 |
| 102 | student2 | 60 | 61 | 62 |
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
| 106 | student6 | 40 | 41 | 42 |
| 107 | student7 | 63 | 64 | 65 |
+ + + + + +
7 rows in set (0.00 sec)

#ORDER BY DESCENDING ORDER


mysql> SELECT *FROM STUDENT
-> ORDER BY HINDI DESC;
+ + + + + +
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+ + + + + +
| 105 | student5 | 90 | 91 | 92 |
| 104 | student4 | 80 | 81 | 82 |
| 103 | student3 | 70 | 71 | 72 |
| 107 | student7 | 63 | 64 | 65 |
| 102 | student2 | 60 | 61 | 62 |
| 101 | student1 | 99 | 51 | 52 |
| 106 | student6 | 40 | 41 | 42 |
+ + + + + +
7 rows in set (0.05 sec)
#ORDER BY ASCENDING ORDER
mysql> SELECT *FROM STUDENT
-> ORDER BY HINDI ASC;
+ + + + + +
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+ + + + + +
| 106 | student6 | 40 | 41 | 42 |
| 101 | student1 | 99 | 51 | 52 |
| 102 | student2 | 60 | 61 | 62 |
| 107 | student7 | 63 | 64 | 65 |
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
+ + + + + +
7 rows in set (0.00 sec)

#DELETING A TUPLE FROM THE TABLE


mysql> DELETE FROM STUDENT
-> WHERE ROLLNO=101;
Query OK, 1 row affected (0.14 sec)

mysql> SELECT *FROM STUDENT;


+ + + + + +
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+ + + + + +
| 102 | student2 | 60 | 61 | 62 |
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
| 106 | student6 | 40 | 41 | 42 |
| 107 | student7 | 63 | 64 | 65 |
+ + + + + +
6 rows in set (0.06 sec)

#ORDER BY BRANCH

#ACTUAL DATA
mysql> SELECT *FROM STUDENT;
+ + + + + + +
| ROLLNO | BRANCH | NAME | TELUGU | HINDI | MATHS |
+ + + + + + +
| 102 | MPC | student2 | 60 | 61 | 62 |
| 103 | BIPC | student3 | 70 | 71 | 72 |
| 104 | BIPC | student4 | 80 | 81 | 82 |
| 105 | BIPC | student5 | 90 | 91 | 92 |
| 106 | BIPC | student6 | 40 | 41 | 42 |
| 107 | MPC | student7 | 63 | 64 | 65 |
+ + + + + + +
6 rows in set (0.00 sec)

mysql> SELECT BRANCH,COUNT(*)


-> FROM STUDENT
-> GROUP BY BRANCH;
+ + +
| BRANCH | COUNT(*) |
+ + +
| MPC | 2|
| BIPC | 4|
+ + +
2 rows in set (0.01 sec)

#e min, max, sum, count and average


mysql> SELECT MIN(TELUGU) "TELUGU MIN MARKS"
-> FROM STUDENT;
+ +
| TELUGU MIN MARKS |
+ +
| 40 |
+ +
1 row in set (0.00 sec)

mysql> SELECT MAX(TELUGU) "TELUGU MAX MARKS"


-> FROM STUDENT;
+ +
| TELUGU MAX MARKS |
+ +
| 90 |
+ +
1 row in set (0.00 sec)

mysql> SELECT SUM(TELUGU) "TELUGU TOTAL MARKS"


-> FROM STUDENT;
+ +
| TELUGU TOTAL MARKS |
+ +
| 403 |
+ +
1 row in set (0.00 sec)

mysql> SELECT COUNT(ROLLNO)


-> FROM STUDENT;
+ +
| COUNT(ROLLNO) |
+ +
| 6|
+ +
1 row in set (0.01 sec)

mysql> SELECT AVG(TELUGU) "TELUGU AVG MARKS"


-> FROM STUDENT;
+ +
| TELUGU AVG MARKS |
+ +
| 67.16666666666667 |
+ +

Program 20:Integrate SQL with Python by importing the MySQL module

import mysql.connector as sqltor


mycon=sqltor.connect(host="localhost",user="root",passwd="tiger",database="gvkcv")
if mycon.is_connected()==False:
print("error connecting to database")
cursor=mycon.cursor()
cursor.execute("select *from student10")
data=cursor.fetchall()
for i in data:
print(i)
mycon.close()

Program 21:Integrate SQL with Python by importing the pymysql module

import pymysql as pym


mycon=sqltor.connect(host="localhost",user="root",passwd="tiger",database="gvkcv")
cursor=mycon.cursor()
cursor.execute("select *from student10")
data=cursor.fetchall()
for i in data:
print(i)
mycon.close()

You might also like