COMPUTER SCIENCE
PRACTICAL PROGRAMS WITH SOLUTIONS
Practical Hand Book
General Instructions:
1. Eighteen Exercises from Python and Seven from MySQL are practiced in the
practical classes.
2. In Practical exams, the question paper will have two questions.
3. One question should be in Python another Question in MySQL.
4. Distribution of Marks as follows:
Duration of Practical: 3 Hrs Maximum Marks: 30
I. Internal Assessment: 17 Marks
Record Book 7 Marks
Viva Voice 2 Marks
Project Work 8 Marks
II. External Assessment: 12 Marks
Python Program (Writing Code + Execution) 8 Marks
MySQL Program (Writing Code + Execution) 4 Marks
Total 30 Marks
1
INDEX
Question Page
S.No Program Name
Number Number
Removing Repeated Elements
1 PG1 4
from a List
2 PG2 Dictionary Creation 6
3 PG3 Reverse & Swapping List 8
4 PG4 String Palindrome 10
Check a Value Exists in
5 PG5 12
Dictionary
6 PG6 Factorial 14
7 PG7 Arithmetic Operations 16
Copying Prime Numbers to the
8 PG8 18
List
Swapping Odd and Even Position
9 PG9 20
from List
Text File Displaying words
10 PG10 22
separated by #
No of Vowels & Consonants –
11 PG11 24
Text File
Creating Student Data – Binary
12 PG12 26
File
2
Updating Student Data – Binary
13 PG13 28
File
14 PG14 Add and Count the data - CSV File 30
Add and Searching data - CSV
15 PG15 32
File
PUSH and POP Operation to
16 PG16 34
Display Names
PUSH and POP Operation to
17 PG17 36
Display ODD Numbers
PUSH and POP Operation to
18 PG18 38
Display dictionary Values
CREATE, ALTER, UPDATE
19 PG19 40
Table Using MySQL
SELECT, ORDER BY, DELETE
20 PG20 42
Data Using MySQL
Aggregate Functions, Group By
21 PG21 44
Data Using MySQL
Displaying Data(Select) Using
22 PG22 47
Python With MySQL
Inserting Data Using Python With
23 PG23 49
MySQL
Deleting Data Using Python With
24 PG24 51
MySQL
Updating Data Using Python With
25 PG25 53
MySQL
3
PG1- Removing Repeated Elements from a List
Write a Python Program to remove repeated elements from a list.
Source Code
4
Result:
Thus the program was executed successfully and output is verified.
5
PG2- Dictionary Creation
Write a Python Program to create a dictionary containing names of competition
winner students as keys and number of their wins as values.
Source Code
6
Result:
Thus the program was executed successfully and output is verified.
7
PG3 – Reverse & Swapping List
Write a Python Program to accept 5 items into a list and reverse the list. Also
swap the first and last items in the list.
Source Code
8
Result:
Thus the program was executed successfully and output is verified.
9
PG4 – String Palindrome
Write a Python Program to check if a string is palindrome or not.
Source Code
10
Result:
Thus the program was executed successfully and output is verified.
11
PG5 – Check a Value Exists in Dictionary
Write a Python Program to check whether a value exists in dictionary.
Source Code
12
Result:
Thus the program was executed successfully and output is verified.
13
PG6 – Factorial
Write a Python Program to calculate a factorial value by given number.
Source Code
14
Result:
Thus the program was executed successfully and output is verified.
15
PG7 – Arithmetic Operations
Write a Python Program to receives two numbers in a function and returns
the results of all arithmetic operations(+,-,*,/,%) on these numbers.
Source Code
16
Result:
Thus the program was executed successfully and output is verified.
17
PG8 – Copying Prime Numbers to the List
Write the definition of a method/function Copy_Prime(lst) to copy all the prime
numbers from the list Lst to another list Lst_prime.
Source Code
18
Result:
Thus the program was executed successfully and output is verified.
19
PG9 – Swapping Odd and Even Position from List
Write a Python function SwitchOver(Val) to swap the even and odd positions of
the values in the list Val.
Note : Assuming that the list has even number of values in it.
For example :If the list Numbers contain
[25,17,19,13,12,15]
After swapping the list content should be displayed as
[17,25,13,19,15,12]
Source Code
20
Result:
Thus the program was executed successfully and output is verified.
21
PG10 – Displaying words separated by # -Text File
Write a Python Program to read a text file line by line and display each word
separated by '#'.
Source Code
22
Result:
Thus the program was executed successfully and output is verified.
23
PG11 – No of Vowels & Consonants – Text File
Write a Python Program to read a text file and display the number of vowels/
consonants/ uppercase/lowercase characters in the file.
Source Code
24
Result:
Thus the program was executed successfully and output is verified.
25
PG12 – Creating Student Data – Binary File
Write a Python Program to get student data (rollno,name,marks) from user and
write onto a binary file. The program should be able to get data from the user
and write onto the file as long as the user wants.
Source Code
26
Result:
Thus the program was executed successfully and output is verified.
27
PG13 –Updating Student Data – Binary File
Write a Python Program to create a binary file with roll number, name & marks,
input a roll number & update the marks.
Source Code
28
Result:
Thus the program was executed successfully and output is verified.
29
PG14 – Add and Count the data – CSV File
Sangeeta is a Python programmer working in a computer hardware company.
She has to maintain the records of the peripheral devices. She created a csv file
named Peripheral.csv, to store the details.
The structure of Peripheral.csv is:
[P_id, P_name, Price]
where
P_id is Peripheral device ID (integer)
P_name is Peripheral device name (String)
Price is Peripheral device price (integer)
Sangeeta wants to write the following user defined functions:
Add_Device( ) : To accept a record from the user and add it to a csv file,
Peripheral.csv
Count_Device( ) : To count and display number of peripheral devices
whose price is less than 1000.
Source Code
30
Result:
Thus the program was executed successfully and output is verified.
31
PG15 – Add and Searching data – CSV File
Write a program in Python that defines and calls the following user defined
functions:
(i) COURIER_ADD() : It takes the values from the user and adds the details to a
csv file 'courier.csv'. Each record consists of a list with field elements as cid,
s_name, Source, destination to store Courier ID, Sender name, Source and
destination address respectively.
(ii) COURIER_SEARCH() : Takes the destination as the input and displays all
the courier records going to that destination.
Source Code
32
Result:
Thus the program was executed successfully and output is verified.
33
PG16 – PUSH and POP Operation to Display Names
A list contains following record of customer:
[Customer_name, Room Type]
Write the following user defined functions to perform given operations on the
stack named ‘Hotel’:
(i) Push_Cust() – To Push customers’ names of those customers who are
staying in ‘Delux’ Room Type.
(ii) Pop_Cust() – To Pop the names of customers from the stack and
display them. Also, display “Underflow” when there are no customers
in the stack.
For example If the lists with customer details are as follows:
["Siddarth", "Delux"]
["Rahul", "Standard"]
["Jerry", "Delux"]
The stack should contain
Jerry
Siddharth
The output should be:
Jerry
Siddharth
Underflow
Source Code
34
Result:
Thus the program was executed successfully and output is verified.
35
PG17 – PUSH and POP Operation to Display ODD Numbers
Write a Python Program that has a list containing 10 integers. You need to help
him create a program with separate user defined functions to perform the
following operations based on this list.
Traverse the content of the list and push the ODD numbers into a stack.
Pop and display the content of the stack.
Source Code
36
Result:
Thus the program was executed successfully and output is verified.
37
PG18 – PUSH and POP Operation to Display dictionary Values
Write a Python Program that has created a dictionary containing top players and
their runs as key value pairs of cricket team. Write a program, with separate
user defined functions to perform the following operations:
Push the keys (name of the players) of the dictionary into a stack, where
the corresponding value (runs) is greater than 49.
Pop and display the content of the stack
Source Code
38
Result:
Thus the program was executed successfully and output is verified.
39
PG19 – CREATE, ALTER, UPDATE Table Using MySQL
Consider the Student Mark sheet as follows:
Table Name: Student
Column Name ID Name Grade PT1 PT2
Data type String of 9 letters Characters 2 characters Number Number
Constraints Primary Key Cannot be Null
CC10356 Arnav 12 65 76
CF10345 Bharathi 11 94 95
Values IM12546 Chandru 12 67 57
IL20056 Dithya 11 54 53
IK13652 Feroz 12 76 65
Write SQL Command for the following Queries with regards to the created table:
(1) Create a Table with the given constraints
(2) Insert the given values to the created table
(3) Create a Column called “Total” with Integer data type
(4) Assign values to the column Total by adding the marks of PTI and PT2
(5) Display the names of the students who got more than 140 marks in total.
(6) Display the details of the student’s grade 12 only.
(1) Creating Table with the given Constraints
Query: CREATE TABLE Student (ID CHAR(9) PRIMARY KEY, Name
VARCHAR(30) NOT NULL, Grade CHAR(2), PT1 INTEGER, PT2 INTEGER);
(2) Insert the given values to the created table
Query: INSERT INTO Student VALUES ('CC10356','Arnav','12',65,76),
('CF10345','Bharathi','11',94,95),('IM12546','Chandru','12',67,57),('IL20056','Dithya','11',
54,53),('IK13652','Feroz','12',76,65);
40
(3) Create a Column called “Total” with Integer data type
Query: ALTER TABLE Student ADD COLUMN Total INTEGER;
(4) Assign values to the column Total by adding the marks of PTI and PT2
Query: UPDATE Student SET Total = PT1+PT2;
(5) Display the names of the students who got more than 140 marks in total.
Query: SELECT Name FROM Student WHERE Total>140;
(6) Display the details of the students grade 12 only.
Query: SELECT * FROM Student WHERE Grade= ‘12’;
Result:
Thus the program was executed successfully and output is verified.
41
PG20 – SELECT, ORDER BY, DELETE Data Using MySQL
Consider the Student Mark sheet as follows:
Table Name: Employee
Column Name EmpNo EmpName Dept Salary Designation
Data type Integer Characters Characters Number Characters
Constraints Primary Key Cannot be Null
150 Arnav Marketing 45000 Executive
134 Bharathi Sales 35000 Executive
Values 167 Chandru Marketing 38500 Asst Manager
195 Dithya Null 37000 Manager
208 Feroz Sales 60000 Manager
Write SQL Command for the following Queries with regards to the created table:
1. Create a Table with the given constraints and Insert the given values to the created
table.
2. Display the details of Employees in ascender order of EmpNo.
3. Display all the department names without repetition.
4. Display the total Department’s in the table.
5. Display the number of Departments in Employee table.
6. To delete the employee record having EmpName as Dithya.
(1) Create a Table with the given constraints and Insert the given values to the created
table.
Query: CREATE TABLE Employee (EmpNo INT PRIMARY KEY, EmpName
VARCHAR(30) NOT NULL, Dept VARCHAR(30), Salary INT, Designation
VARCHAR(30));
INSERT INTO Employee VALUES (150,'Arnav','Marketing',45000,'Executive'),
(134,'Bharathi','Sales',35000,'Executive'), (167,'Chandru','Marketing',38500,'Ast
Manager'), (195,'Dithya','HR',37000,'Manager'), (208,'Feroz','Sales',60000,'Manager');
42
(2) Display the details of Employees in ascender order of EmpNo.
Query: SELECT * FROM Employee ORDER BY EmpName;
(3) Display all the department names without repetition.
Query: SELECT DISTINCT (Dept) FROM Employee;
(4) Display the number of records in the table.
Query: SELECT COUNT(*) FROM Employee;
(5) Display the number of Departments in Employee table.
Query: SELECT COUNT(Dept) FROM Employee;
(6)To delete the employee record having EmpName as Dithya.
Query: DELETE FROM Employee WHERE EmpName= ‘Dithya’;
Result:
Thus the program was executed successfully and output is verified.
43
PG21 – Aggregate Function, Group By Data Using MySQL
Consider the Student Mark sheet as follows:
Table Name: COMPUTER
Column Name PROD_ID PROD_NAME PRICE COMPANY TYPE
Data type Characters Characters Integer Characters Characters
Constraints Primary Key Canonot be Null
P001 MOUSE 200 LOGITECH INPUT
P002 LASER PRINTER 4000 CANON OUTPUT
Values P003 KEYBOARD 500 LOGITECH INPUT
P004 JOYSTICK 1000 IBALL INPUT
P005 SPEAKER 1200 CREATIVE OUTPUT
Write SQL Command for the following Queries with regards to the created table:
1. Create a Table with the given constraints and Insert the given values to the created
table.
2. Display the details of Maximum and Minimum price of products.
3. Display all the count of the product each type.
4. Display the total price of the product each company wise.
5. Display the difference of highest and lowest salary of each company having
maximum price>1000.
6. To delete the attribute Type in Computer Table.
(1) Create a Table with the given constraints and Insert the given values to the created
table.
Query: CREATE TABLE COMPUTER(PROD_ID VARCHAR(30) PRIMARY KEY,
PROD_NAME VARCHAR(30) NOT NULL, PRICE INT, COMPANY VARCHAR(20),
TYPE VARCHAR (20));
44
INSERT INTO COMPUTER VALUES ('P001','MOUSE',200,'LOGITECH','INPUT'),
('P002','LASER PRINTER',4000,'CANON','OUTPUT'),
('P003','KEYBOARD',500,'LOGITECH','INPUT'),
('P004','JOYSTICK',1000,'IBALL','INPUT'),('P005','SPEAKER',1200,'CREATIVE','OUT
PUT'),('P006','DESKJET PRINTER',4300,'CANON','OUTPUT');
(2) Display the details of Maximum and Minimum price of products.
Query: SELECT MAX(PRICE),MIN(PRICE) FROM COMPUTER;
(3) Display all the count of the product each type.
Query: SELECT COUNT(*) FROM COMPUTER GROUP BY TYPE;
(4) Display the total price of the product each company wise.
Query: SELECT SUM(PRICE),COMPANY FROM COMPUTER GROUP BY
COMPANY;
(5) Display the difference of highest and lowest salary of each company having maximum
price>1000.
Query: SELECT MAX(PRICE)-MIN(PRICE) “DIFFERENCE” FROM COMPUTER
GROUP BY COMPANY HAVING MAX(PRICE)>1000;
45
(6) To delete the attribute Type in Computer Table.
Query: ALTER TABLE COMPUTER DROP COLUMN TYPE;
Result:
Thus the program was executed successfully and output is verified.
46
PG22 – Displaying Data(SELECT) Using Python With MySQL
To write a code in Python to display all the details of the passengers from the
table flight in MySQL database, Travel. The table contains the following
attributes:
F_ code : Flight code (String)
F_name: Name of flight (String)
Source: Departure city of flight (String)
Destination: Destination city of flight (String)
Consider the following to establish connectivity between Python and MySQL:
● Username : root
● Password : airplane
● Host : localhost
Table Name: FLIGHT
F_code F_Name Source Destination
F101 Indigo Delhi Chennai
F102 Etihad Delhi Bengaluru
F103 Qatar Airways Mumbai Chennai
F104 Kingfisher Delhi Mumbai
F105 Emirates Mumbai Bengaluru
Source Code
47
F_code F_Name Source Destination
F101 Indigo Delhi Chennai
F102 Etihad Delhi Bengaluru
F103 Qatar Airways Mumbai Chennai
F104 Kingfisher Delhi Mumbai
F105 Emirates Mumbai Bengaluru
Result:
Thus the program was executed successfully and output is verified.
48
PG23 – Inserting Data Using Python With MySQL
To write a program in Python to insert the following record in the table named
Bank_Account in MySQL database, Bank:
● Accno – integer
● Cname – string
● Atype – string
● Amount – float
Note the following to establish connectivity between Python and MySQL:
● Username – admin
● Password – root
● Host – localhost
The values of fields Accno, Cname, Atype and Amount have to be accepted from
the user.
Source Code
49
Enter your Account Number : 10326584875
Enter your Name : Arunkumar
Enter your Account Type : SB- Type
Enter The Amount : 50000
A tuple has been inserted
Result:
Thus the program was executed successfully and output is verified.
50
PG24 – Deleting Data Using Python With MySQL
To write a program in Python to delete the record of a candidate “Raman” from
the table named Placement in MySQL database Agency. The table Placement in
MySQL contains the following attributes :
● CName – String
● Dept – String
● Place – String
● Salary – integer
Note the following to establish connectivity between Python and MySQL:
● Username – root
● Password – job
● Host – localhost
Source Code
51
Enter the Candidate Name: Raman
Candidate Raman, is removed from the table Placement
Result:
Thus the program was executed successfully and output is verified.
52
PG25 – Updating Data Using Python With MySQL
To write a program in Python to update the quantity to 20 of the records whose
item code is 111 in the table named shop in MySQL database named Keeper.The
table shop in MySQL contains the following attributes:
● Item_code: Item code (Integer)
● Item_narne: Name of item (String)
● Qty: Quantity of item (Integer)
● Price: Price of item (Integer)
Consider the following to establish connectivity between Python and MySQL:
Username: admin
Password : Shopping
Host: localhost
Source Code
53
Enter the Item_code: 111
Enter the Quantity : 20
Item_code 111 ,
Item of Quantity have been updated to 20
Result:
Thus the program was executed successfully and output is verified.
54