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

Python Programs

Pp

Uploaded by

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

Python Programs

Pp

Uploaded by

mkailasanathan6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

XII STD - COMPUTER SCIENCE

PRACTICAL PROGRAMS WITH SOLUTION

INDEX
SL. QUESTION PAGE
NO. NUMBER
PROGRAM NAME NUMBER

(A) CALCULATE FACTORIAL


1 PY1
(B) SUM OF SERIES
(A) ODD OR EVEN
2 PY2
(B) REVERSE THE STRING

3 PY3 GENERATE VALUES AND REMOVE ODD NUMBERS

GENERATE PRIME NUMBERS AND SET


4 PY4
OPERATIONS

5 PY5 DISPLAY A STRING ELEMENTS – USING CLASS

6 DB6 MYSQL – EMPLOYEE TABLE

7 DB7 MYSQL – STUDENT TABLE

8 PY8 PYTHON WITH CSV

9 PY9 PYTHON WITH SQL

10 PY10 PYTHON GRAPHICS WITH PIP


PY1:a CALCULATE FACTORIAL

AIM:
To write a program to calculate the factorial of the given number using for loop.
CODING:
n=int(input("Enter n: "))
fact=1
for i in range(1,n+1):
fact*= i
print(n,"!=",fact)

OUPUT:
Enter n: 12
12 != 479001600

PY1:b SUM OF SERIES

AIM:
To write a program to sum the series 1/1+ 22/2 + 33/3 +…….nn/n
CODING:
n=int(input("Enter n: "))
s=0
for i in range(1,n+1):
s+= ((i**i)/i)
print("Sum of the series = ",s)
OUPUT:
Enter n: 4
Sum of the series = 76.0

PY2:a ODD OR EVEN


AIM:
To write a program using functions to check whether a number is even or odd.
CODING:
def oddeven(a):
if a%2==0:
print(a," is Even")
else:
print(a,"is Odd")

n=int(input("Enter n:"))
oddeven(n)
OUPUT:
Enter n : 7
7 is Odd
Enter n : 6
6 is Even

PY2:b REVERSE THE STRING

AIM:
To Write a program to create a mirror image of the given string. For example,
“wel” = “lew”.
CODING:
w=input("Enter string : ")
n=len(w)
i=-1
r=""
while i>=(-n):
r+=w[i]
i=i-1
print(r," is the mirror image of ",w )
OUPUT:
Enter string: school
loohcs is the mirror image of school

PY3 GENERATE VALUES AND REMOVE ODD NUMBERS

AIM:
To write a program to generate values from 1 to 10 and then remove all the odd
numbers from the list.
CODING:
n=list(range(1,11))
print("The list is :",n)
for i in n:
if i%2==1:
n.remove(i)
print("List after Removing Odd Numbers:",n)
OR
CODING:
n=list(range(1,11))
print("The list is :",n)
for i in range(1,11,2):
n.remove(i)
print("List after Removing Odd Numbers:",n)
OUPUT:
The list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
List after Removing Odd Numbers: [2, 4, 6, 8, 10]

PY4 GENERATE PRIME NUMBERS AND SET OPERATIONS

AIM:
To write a program that generates a set of prime numbers and another set of odd
numbers. Display the result of union, intersection, difference and symmetric difference
operations.
CODING:
o=set(x for x in range(1,10,2))
print("Odd Numbers: ",o)
p=set( )
for i in range(2,11):
for j in range (2,i):
if i%j == 0 :
break
else:
p.add(i)
print("Prime Numbers: ", p)
print("Union: ", o.union(p))
print("Intersection: ", o.intersection(p))
print("Difference: ", o.difference(p))
print("Symmetric Difference: ", o.symmetric_difference(p))

OUPUT:
Odd Numbers: {1, 3, 5, 7, 9}
Prime Numbers: {2, 3, 5, 7}
Union: {1, 2, 3, 5, 7, 9}
Intersection: {3, 5, 7}
Difference: {1, 9}
Symmetric Difference: {1, 2, 9}
PY5 DISPLAY STRING ELEMENTS – USING CLASS

AIM:
To write a program to accept a string and print the number of uppercase,
lowercase, vowels, consonants and spaces in the given string using Class.
CODING:
class string:
def __init__(self):
self.u=0
self.l=0
self.v=0
self.c=0
self.s=0
self.st=""
def count(self):
self.st = input("Enter the string : ")
for ch in self.st:
if ch in ('a','e','i','o','u','A','E', 'I','O','U'):
self.v += 1
elif ord(ch)== 32:
self.s += 1
else:
self.c += 1
if ch.isupper( ):
self.u += 1
elif ch.islower( ):
self.l += 1
print("Given String is = ", self.st)
print("No of Uppercase = ", self.u)
print("No of Lowercase = ", self.l)
print("No of Vowels = ", self.v)
print("No of consonant = ", self.c)
print("No of Spaces = ", self.s)

obj = string( )
obj.count( )

OUPUT:
Enter the string : Welcome to Computer Science
Given String is = Welcome to Computer Science
No of Uppercase = 3
No of Lowercase = 21
No of Vowels = 10
No of consonant = 14
No of Spaces = 3
DB6 MYSQL EMPLOYEE TABLE

AIM:
To create an Employee Table with the fields Empno, Empname, Desig, Dept,
age and Place. Enter five records into the table.
 Add two more records to the table.
 Modify the table structure by adding one more field namely date of joining.
 Check for Null value in doj of any record.
 List the employees who joined after 2018/01/01.

SQL QUERIES AND OUTPUT:

(i) Creating Table Employee


mysql> Create table employee (Empno integer(4) primary key, Empname varchar(20),
Desig varchar(10), Dept varchar(10), Age integer(2), Place varchar(10));

(ii) View Table Structure:


mysql> Desc employee;
Field Type Null Key Default Extra
Empno int(4) NO PRI NULL
Empname varchar(20) YES NULL
Desig varchar(10) YES NULL
Dept varchar(10) YES NULL
Age int(2) YES NULL
Place varchar(10) YES NULL

(iii) Inserting Data into Table:


mysql> Insert into employee values(1221, 'Sidharth', 'Officer', 'Accounts', 45, 'Salem');
mysql> Insert into employee values(1222, 'Naveen', 'Manager', 'Admin', 32, 'Erode');
mysql> Insert into employee values(1223, 'Ramesh', 'Clerk', 'Accounts', 33, 'Ambathur');
mysql> Insert into employee values(1224, 'Abinaya', 'Manager', 'Admin', 28, 'Anna Nagar');
mysql> Insert into employee values(1225, 'Rahul', 'Officer', 'Accounts', 31, 'Anna Nagar');
(iv) Select all the record:
mysql> select * from employee;
Empno Empnam Desig Dept Age Place
1221 e
Sidharth Officer Accounts 45 Salem
1222 Naveen Manager Admin 32 Erode
1223 Ramesh Clerk Accounts 33 Ambathur
1224 Abinaya Manager Admin 28 Anna Nagar
1225 Rahul Officer Accounts 31 Anna Nagar

(v) Adding two more records:


mysql> Insert into employee values(3226, 'Sona', 'Manager', 'Accounts', 42, 'Erode');
mysql> Insert into employee values(3227, 'Rekha', 'Officer', 'Admin', 34, 'Salem');
mysql> select * from employee;

Empno Empname Desig Dept Age Place


1221 Sidharth Officer Accounts 45 Salem
1222 Naveen Manager Admin 32 Erode
1223 Ramesh Clerk Accounts 33 Ambathur
1224 Abinaya Manager Admin 28 Anna Nagar
1225 Rahul Officer Accounts 31 Anna Nagar
3226 Sona Manager Accounts 42 Erode
3227 Rekha Officer Admin 34 Salem
7 rows in set (0.00 sec)

(vi) Adding one more Field:

mysql> Alter table employee add(doj date);


mysql> desc employee;

Field Type Null Key Default Extra


Empno int(4) NO PRI NULL
Empname varchar(20) YES NULL
Desig varchar(10) YES NULL
Dept varchar(10) YES NULL
Age int(2) YES NULL
Place varchar(10) YES NULL
doj date YES NULL
7 rows in set (0.00 sec)
(vii) Inserting date of joining to each employee:
mysql> update employee set doj = '2010-03-21' where empno=1221;
mysql> update employee set doj = '2012-05-13' where empno=1222;
mysql> update employee set doj = '2017-10-25' where empno=1223;
mysql> update employee set doj = '2018-06-17' where empno=1224;
mysql> update employee set doj = '2018-01-02' where empno=1225;
mysql> update employee set doj = '2017-12-31' where empno=3226;
mysql> update employee set doj = '2015-08-16' where empno=3227;
mysql> select * from Employee;

Empno Empname Desig Dept Age Place doj


1221 Sidharth Officer Accounts 45 Salem 2010-03-21
1222 Naveen Manager Admin 32 Erode 2012-05-13
1223 Ramesh Clerk Accounts 33 Ambathur 2017-10-25
1224 Abinaya Manager Admin 28 Anna Nagar 2018-06-17
1225 Rahul Officer Accounts 31 Anna Nagar 2018-01-02
3226 Sona Manager Accounts 42 Erode 2017-12-31
3227 Rekha Officer Admin 34 Salem 2015-08-16
7 rows in set (0.00 sec)
(viii) Checking null value in doj
mysql> select * from employee where empno is null;
Empty set (0.00 sec)
(ix) List the employees who joined after 2018/01/01.
mysql> Select * from employee where doj > '2018-01-01';
Empno Empname Desig Dept Age Place doj
1224 Abinaya Manager Admin 28 Anna Nagar 2018-06-17
1225 Rahul Officer Accounts 31 Anna Nagar 2018-01-02

DB7 MYSQL STUDENT TABLE

AIM:
To create Student Table with following fields and enter data as given in the table
below

Field Type Size


Reg_No char 5
Sname varchar 15
Age int 2
Dept varchar 10
Class char 3

Data to be entered
Reg_No Sname Age Dept Class
M1001 Harish 19 ME ME1
M1002 Akash 20 ME ME2
C1001 Sneha 20 CSE CS1
C1002 Lithya 19 CSE CS2
E1001 Ravi 20 ECE EC1
E1002 Leena 21 EEE EE1
E1003 Rose 20 ECE EC2

Then, query the followings:


(i) List the students whose department is “CSE”.
(ii) List all the students of age 20 and more in ME department.
(iii) List the students department wise.
(iv) Modify the class M2 to M1.
(v) Check for the uniqueness of Register no.

SQL QUERIES AND OUTPUT:


(1) Creating Table - Student
mysql>Create table Student(Reg_No char(5), Sname varchar(20), Age integer(2), Dept
varchar(10), Class char(3));
Query OK, 0 rows affected (0.51 sec)
View table structure:
mysql> desc Student;

Field Type Null Key Default Extra


Reg_No char(5) YES NULL
Sname varchar(20) YES NULL
Age int(2) YES NULL
Dept varchar(10) YES NULL
Class char(3) YES NULL
5 rows in set (0.02 sec)
(2) Inserting Data into table:
mysql>Insert into Student values ('M1001', 'Harish', 19, 'ME', 'ME1');
mysql>Insert into Student values ('M1002', 'Akash', 20, 'ME', 'ME2');
mysql>Insert into Student values ('C1001', 'Sneha', 20, 'CSE', 'CS1');
mysql>Insert into Student values ('C1002', 'Lithya', 19, 'CSE', 'CS2');
mysql>Insert into Student values ('E1001', 'Ravi', 20, 'ECE', 'EC1');
mysql>Insert into Student values ('E1002', 'Leena', 21, 'EEE', 'EE1');
mysql>Insert into Student values ('E1003', 'Rose', 20, 'ECE', 'EC2');
View all records:
mysql> select * from Student;

Reg_N Sname Age Dept Class


o
M1001 Harish 19 ME ME1
M1002 Akash 20 ME ME2
C1001 Sneha 20 CSE CS1
C1002 Lithya 19 CSE CS2
E1001 Ravi 20 ECE EC1
E1002 Leena 21 EEE EE1
E1003 Rose 20 ECE EC2

(3) Other Queries:


(i) List the students whose department is “CSE”:
mysql> Select * from Student where Dept='CSE';

Reg_No Sname Age Dept Class


C1001 Sneha 20 CSE CS1
C1002 Lithya 19 CSE CS2
2 rows in set (0.03 sec)
(ii) List all the students of age 20 and more in ME department:
mysql> Select * from Student where Age >=20 and Dept='ME';

Reg_No Sname Age Dept Class


M1002 Akash 20 ME ME2
1 row in set (0.02 sec)
(iii) List the students department wise:
mysql> Select * from Student Group by Dept Order by Sname;
Reg_No Sname Age Dept Class
M1001 Harish 19 ME ME1
E1002 Leena 21 CSE EE1
E1001 Ravi 20 ECE EC1
C1001 Sneha 20 EEE CS1
4 rows in set (0.00 sec)
(iv) Modify the class M2 to M1:
mysql> Update Student set Class='ME1' where Class='ME2';
Query OK, 1 row affected (0.11 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from Student;
Reg_No Sname Age Dept Class
M1001 Harish 19 ME ME1
M1002 Akash 20 ME ME1
C1001 Sneha 20 CSE CS1
C1002 Lithya 19 CSE CS2
E1001 Ravi 20 ECE EC1
E1002 Leena 21 EEE EE1
E1003 Rose 20 ECE EC2
7 rows in set (0.00 sec)
(v) Check for the uniqueness of Register no.
mysql> Select Distinct Reg_No from Student;

Reg_No
M1001
M1002
C1001
C1002
E1001
E1002
E1003
7 rows in set (0.02 sec)

PY8 PYTHON WITH CSV

AIM:
To write a program using Python to get 10 players name and their score. Write
the input in a csv file. Accept a player name using Python. Read the csv file to display
the names and the score. If the player name is not found give an appropriate message.
CODING:
import csv
with open("player.csv",'w') as f:
w = csv.writer(f)
for i in range(1,11):
name = input ("Enter the Player Name : ")
score = int(input("Enter the Score : "))
w.writerow([name,score])
f.close( )

with open("player.csv",'r') as f:
read=csv.reader(f)
fg =0
sv = input ("Enter the player name to be searched for :")
for row in read:
if sv in row:
print(row)
fg =1
else :
if fg==0:
print ("Player not Found")
f.close( )

OUPUT:
Enter the Player Name : Rohit Sharama
Enter the Score : 200
Enter the Player Name : Sehwag
Enter the Score : 219
Enter the Player Name : Sachin
Enter the Score : 250
Enter the Player Name : Dhoni
Enter the Score : 210
Enter the Player Name : Viratkohli
Enter the Score : 148
Enter the Player Name : Ganguly
Enter the Score : 158
Enter the Player Name : Kapildev
Enter the Score : 175
Enter the Player Name : Sachin
Enter the Score : 200
Enter the Player Name : SunilGavaskar
Enter the Score : 198
Enter the Player Name : Amarnath
Enter the Score : 130

Enter the player name to be searched for :Sachin


['Sachin', '250']
['Sachin', '200']
PY9 PYTHON WITH SQL

AIM:
To create a sql table using python and accept 10 names and age. Sort in
descending order of age and display.

CODING:
import sqlite3
connection=sqlite3.connect("data.db")
c=connection.cursor( )
c.execute("Drop Table Student;")
c.execute("Create table student (name varchar(30),age int);")
print ("Enter 10 students names and their age respectively")
for i in range (10):
who = input("Enter Name: ")
age = int(input("Enter Age: "))
c.execute("insert into student values(?,?)",(who,age))
c.execute("select * from student order by age desc;")
print( "Displaying All the Records From Student Table in Descending Order of Age")
print(*c.fetchall( ),sep = "\n")
connection.commit( )
connection.close( )

OUPUT:
Enter 10 students names and their age respectively
Enter Name: KALA
Enter Age: 14
Enter Name: MANI
Enter Age: 15
Enter Name: RANI
Enter Age: 14
Enter Name: JANU
Enter Age: 13
Enter Name: VIMAL
Enter Age: 17
Enter Name: RAHUL
Enter Age: 16
Enter Name: MEENA
Enter Age: 12
Enter Name: MALA
Enter Age: 18
Enter Name: VEENA
Enter Age: 16
Enter Name: MELVIN
Enter Age: 19
Displaying All the Records From Student Table in Descending Order of Age
('MELVIN', 19)
('MALA', 18)
('VIMAL', 17)
('RAHUL', 16)
('VEENA', 16)
('MANI', 15)
('KALA', 14)
('RANI', 14)
('JANU', 13)
('MEENA', 12)

PY10 PYTHON GRAPHICS WITH PIP


AIM:
To write a program to get five marks using list and display the marks in pie
chart.
CODING:
import matplotlib.pyplot as plt
marks=[ ]
i=0
subjects = ["Tamil", "English", "Maths", "Science", "Social"]
while i<5:
marks.append(int(input("Enter Mark = ")))
i+=1
for j in range(len(marks)):
print("{}.{} Mark = {}".format(j+1, subjects[j],marks[j]))
plt.pie (marks, labels = subjects, autopct = "%.2f ")
plt.show( )

OUPUT:
Enter Mark = 67
Enter Mark = 31
Enter Mark = 45
Enter Mark = 89
Enter Mark = 73
1.Tamil Mark = 67
2.English Mark = 31
3.Maths Mark = 45
4.Science Mark = 89
5.Social Mark = 73

You might also like