Namma Kalvi 12 Cs em Atp Practical Hand Book 215961
Namma Kalvi 12 Cs em Atp Practical Hand Book 215961
INDEX
Instructions:
Question
1. Eight exercises from Python and Two from MySQL are practiced in the Sl. No. Program Name
Number
practical classes a) CALCULATE FACTORIAL
1 PY1
2. In Practical exams, the question paper will have two questions with internal b) SUM OF SERIES
a) ODD OR EVEN
choice. 2 PY2
b) REVERSE THE STRING
3. One question should be chosen from the list of internal choice
3 PY3 GENERATE VALUES AND REMOVE ODD NUMBERS
4. Distribution of Marks as follows
Duration of Practical: 3 Hrs Maximum Marks: 20 4 PY4 GENERATE PRIME NUMBERS AND SET OPERATIONS
I. Internal Assessment: 5 Marks
5 PY5 DISPLAY A STRING ELEMENTS – USING CLASS
Record Book 5 Marks
II. External Assessment: 15 Marks 6 DB6 MYSQL – EMPLOYEE TABLE
Prepared by,
Parkunan T
PG Asst. in Computer Science,
MHSS, Chengam,
Tiruvannamalai Dt
Cell : 9655 966 906
Mail :[email protected]
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
QUESTION: 1 a) CALCULATE FACTORIAL
Write a program to calculate the factorial of the given number using for
loop. AIM:
To write a program for calculating the factorial of the given number using
for loop.
CODING:
num = int(input("Enter a number:"))
if(num==0):
fact=1
fact=1
for i in range(1,num+1):
fact=fact*i
print("Factorial of" , num," is ",fact)
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
SAMPLE OUTPUT:
Enter a number:5
Factorial of 5 is 120
RESULT:
Thus, the program for calculating factorial of given number using for loop
has been created and executed successfully.
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
QUESTION: 1 b) SUM OF SERIES
2 3 n
Write a program to sum the series:1/1 + 2 /2 + 3 /3 + … + n /n.
AIM:
To write a program to sum the series: 1/1 + 22/2 + 33/3 + … + nn/n.
CODING:
s=0.0
for i in range(1,n+1):
a=float(i**i)/i
s=s+a
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
SAMPLE OUTPUT:
Enter a value of n: 4
The sum of the series is 76.0
RESULT:
Thus, the program for sum of series has been created and executed
successfully.
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
QUESTION: 2 a) ODD OR EVEN
Write a program to using functions to check whether a number is odd or AIM:
even. To write a program using functions for checking whether a number is odd
or even.
CODING:
def oddeven(a):
if(a%2==0):
return 1
else:
return 0
num = int (input("Enter a number: "))
if(oddeven(num)==1):
print("The given number is Even")
elif(oddeven(num)==0):
print("The given number is Odd")
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
SAMPLE OUTPUT 1:
Enter a number: 5
The given number is Odd
SAMPLE OUTPUT 2:
Enter a number: 6
The given number is Even
RESULT:
Thus, the program using functions for checking whether a number is odd
or even has been created and executed successfully.
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
QUESTION: 2 b) REVERSE THE STRING
CODING:
def rev(str1):
str2=''
i=len(str1) - 1
while i>=0:
str2+=str1[i]
i-=1
return str2
word=input("\n Enter a String: ")
print("\n The mirror image of the given string is:" , rev(word))
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
SAMPLE OUTPUT:
RESULT:
Thus, the program Reverse the string has been created and executed
successfully.
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
QUESTION:
3. GENERATE VALUES AND REMOVE ODD NUMBERS
Write a program to generate values from 1 to 10 and then remove
all the odd numbers from the list. AIM:
To write a program for generating values from 1 to 10 and then
removing all the odd numbers from the list.
CODING:
num=[]
for i in range(1,11):
num.append(i)
print("Numbers from 1 to 10...\n ", num)
for j,i in enumerate(num):
if(i%2==1):
del num[j]
print("The values after removed odd numbers...\n", num)
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
SAMPLE OUTPUT:
RESULT:
Thus, the program for generating values from 1 to 10 and then removing
all odd numbers from the list has been created and executed successfully.
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
QUESTION: 4. GENERATE PRIME NUMBERS AND SET OPERATIONS
Write a Program that generates a set of prime numbers and another AIM:
set of odd numbers. Display the result of union, intersection, difference and To write a program for generating a set of prime numbers and another set
symmetric difference operations. of odd numbers and displaying the result of set operations.
CODING:
odd=set([x*2+1 for x in range(0,5)])
primes=set()
for i in range(2,10):
for num in range(0,11):
if num>1:
for i in range(2,num):
if(num % i) == 0:
break
else:
primes.add(num)
print("Odd: ",odd)
print("Prime: ",primes)
print("Union: ",odd.union(primes))
print("Intersection: ",odd.intersection(primes))
print("Difference: ",odd.difference(primes))
print("Symmetric Difference: ",odd.symmetric_difference(primes))
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
SAMPLE OUTPUT :
Odd: {1, 3, 5, 7, 9}
Prime: {2, 3, 5, 7}
Union: {1, 2, 3, 5, 7, 9}
Intersection: {3, 5, 7}
Difference: {1, 9}
Symmetric Difference: {1, 2, 9}
RESULT:
Thus, the program for generating a set of prime numbers and odd
numbers and displaying the result of set operations has been created and
executed successfully.
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
QUESTION: 5. DISPLAY STRING ELEMENTS – USING CLASS
Write a program to accept a string and print the number of
uppercase, lowercase, vowels, consonants and spaces in the given string AIM:
using Class. To write a program for accepting a string and printing the number of
uppercase, lowercase, vowels, consonants and spaces in the given string
using Class.
CODING:
class String:
def __init__(self):
self.uppercase=0
self.lowercase=0
self.vowels=0
self.consonents=0
self.spaces=0
self.string=""
def getstr(self):
self.string=str(input("Enter a String: "))
def count_upper(self):
for ch in self.string:
if(ch.isupper()):
self.uppercase+=1
def count_lower(self):
for ch in self.string:
if(ch.islower()):
self.lowercase+=1
def count_vowels(self):
for ch in self.string:
if(ch in ('A','a','e','E','I','i','O','o','u','U')):
self.vowels+=1
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
SAMPLE OUTPUT: def count_consonents(self):
Enter a String: Computer Science for ch in self.string:
The given String contains... if(ch not in ('A','a','e','E','I','i','O','o','u','U'," ")):
2 Uppercase self.consonents+=1
13 Lowercase def count_spaces(self):
6 vowels for ch in self.string:
9 consonants if(ch==" "):
1 Spaces self.spaces+=1
def execute(self):
self.count_upper()
self.count_lower()
self.count_vowels()
self.count_consonents()
self.count_spaces()
def display(self):
print("The given String contains...")
print("%d Uppercase"%self.uppercase)
print("%d Lowercase"%self.lowercase)
print("%d vowels"%self.vowels)
print("%d consonants"%self.consonents)
print("%d Spaces"%self.spaces)
S=String()
S.getstr()
S.execute()
S.display()
RESULT:
Thus, the program Display string elements using class has been created
and executed successfully.
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
QUESTION: 6. MYSQL – EMPLOYEE TABLE
Create an Employee Table with the fields Empno, Empname, Desig, AIM:
Dept, Age and Place. Enter five records into the table To create SQL queries for creating a table and perform adding records,
• Add two more records to the table. modifying table structure, checking null values and selecting records using where
clause.
• Modify the table structure by adding one more field namely date of SQL QUERIES AND OUTPUT:
joining. i) Creating Table Employee:
mysql> create table emp(Empno integer(4) primary key, Empname varchar(20),
• Check for Null value in doj of any record.
Desig Varchar(15), Dept varchar(15), Age integer(2), Place varchar(15));
• List the employees who joined after 2018/01/01.
ii) Viewing Table Structure:
mysql> desc emp;
+---------------+----------------+------+-------+---------+----------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------+------+-------+---------+----------+
| Empno | int(4) | NO | PRI | NULL | |
| Empname | varchar(20) | YES | | NULL | |
| Desig | varchar(15) | YES | | NULL | |
| Dept | varchar(15) | YES | | NULL | |
| Age | int(2) | YES | | NULL | |
| Place | varchar(15) | YES | | NULL | |
+--------------+-----------------+------+-------+---------+----------+
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
iv) Select all the records:
mysql> select * from emp;
+-----------+----------------+-------------+-------------+-------+---------------+
| Empno | Empname | Desig | Dept | Age | Place |
+-----------+----------------+-------------+-------------+-------+---------------+
| 101 | Aalayam | Officer | Accounts | 45 | Salem |
| 102 | Annamalai | Manager | Admin | 32 | Erode |
| 103 | Kumar | Clerk | Accounts | 33 | Ambathur |
| 104 | Madhesh | Manager | Admin | 28 | Anna Nagar |
| 105 | Basha | Officer | Accounts | 31 | Anna Nagar |
+---------+-----------------+-------------+--------------+------+-----------------+
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
vi) Adding one more field in Table:
mysql> Alter table emp add (doj date);
mysql> desc emp;
+---------------+----------------+------+-------+-----------+----------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------+------+-------+-----------+----------+
| Empno | int(4) | NO | PRI | NULL | |
| Empname | varchar(20) | YES | | NULL | |
| Desig | varchar(15) | YES | | NULL | |
| Dept | varchar(15) | YES | | NULL | |
| Age | int(2) | YES | | NULL | |
| Place | varchar(15) | YES | | NULL | |
| doj | date | YES | | NULL | |
+--------------+-----------------+------+-------+-----------+-----------+
vii) Inserting date of joining to each employee:
mysql> update emp set doj='2015-01-01' where empno=101;
mysql> update emp set doj='2015-06-01' where empno=102;
mysql> update emp set doj='2016-01-01' where empno=103;
mysql> update emp set doj='2016-06-01' where empno=104;
mysql> update emp set doj='2018-01-01' where empno=105;
mysql> update emp set doj='2018-06-01' where empno=106;
mysql> update emp set doj='2019-06-01' where empno=107;
mysql> select * from emp;
+-----------+----------------+-------------+-------------+-------+---------------+----------------+
| Empno | Empname | Desig | Dept | Age | Place | doj |
+-----------+----------------+-------------+-------------+-------+---------------+----------------+
| 101 | Aalayam | Officer | Accounts | 45 | Salem |2015-01-01 |
| 102 | Annamalai | Manager | Admin | 32 | Erode |2015-06-01 |
| 103 | Kumar | Clerk | Accounts | 33 | Ambathur |2016-01-01 |
| 104 | Madhesh | Manager | Admin | 28 | Anna Nagar |2016-06-01 |
| 105 | Basha | Officer | Accounts | 31 | Anna Nagar |2018-01-01 |
| 106 | Ashok | Manager | Accounts | 42 | Erode |2018-06-01 |
| 107 | Suresh | Officer | Admin | 34 | Salem |2019-01-01 |
+---------+-----------------+-------------+--------------+------+-----------------+---------------+
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
viii) Checking null value in doj:
Empty set
+-----------+----------------+-------------+-------------+-------+---------------+----------------+
| Empno | Empname | Desig | Dept | Age | Place | doj |
+-----------+----------------+-------------+-------------+-------+---------------+----------------+
| 106 | Ashok | Manager | Accounts | 42 | Erode |2018-06-01 |
| 107 | Suresh | Officer | Admin | 34 | Salem |2019-01-01 |
+---------+-----------------+-------------+--------------+------+-----------------+----------------+
RESULT:
Thus, the Queries for Employee Table using MySQL has been created and
executed successfully.
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
QUESTION: 7. MYSQL - STUDENT TABLE
Create Student table with following fields and enter data as given in
the table below AIM:
Field Type Size To create SQL queries for given student table and perform some queries.
Reg_No char 5
Sname varchar 15 SQL QUERIES AND OUTPUT:
Age int 2
1) Creating Table – Student:
Dept varchar 10
mysql> create table student(Reg_No char(5), Sname varchar(15), Age integer(2),
Class char 3
Dept varchar(10), Class char(3));
Data to be entered
View Table Structure:
Reg_No Sname Age Dept Class mysql> desc student;
M1001 Harish 19 ME ME1 +-----------+-----------------+------+-------+-----------+--------+
M1002 Akash 20 ME ME2 | Field | Type | Null | Key | Default | Extra |
C1001 Sneha 20 CSE CS1 +-----------+-----------------+------+-------+-----------+--------+
C1002 Lithya 19 CSE CS2 | Reg_No | char(5) | YES | | NULL | |
E1001 Ravi 20 ECE EC1 | Sname | varchar(15) | YES | | NULL | |
E1002 Leena 21 EEE EE1 | Age | int(2) | YES | | NULL | |
E1003 Rose 20 ECE EC2 | Dept | varchar(10) | YES | | NULL | |
| Class | char(3) | YES | | NULL | |
+-----------+-----------------+------+-------+----------+---------+
Then, Query the followings: 2) Inserting Data into table:
i) List the students whose department is “CSE”. mysql> insert into student values('M1001','Harish',19,'ME','ME1');
ii) List all the students of age 20 and more in ME department. mysql> insert into student values('M1002','Akash',20,'ME','ME2');
iii) List the students department wise. mysql> insert into student values('C1001','Sneha',20,'CSE','CS1');
iv) Modify the class ME2 to ME1. mysql> insert into student values('C1002','Lithya',19,'CSE','CS2');
v) Check for the uniqueness of Register no. 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');
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
View all records:
mysql> select * from student;
+-----------+-----------+------+--------+-------+
| 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 |
+-----------+----------+-------+-------+---------+
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 |
+-----------+----------+-------+-------+---------+
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 |
+-----------+----------+-------+-------+---------+
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
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 | EEE | EE1 |
| E1001 | Ravi | 20 | ECE | EC1 |
| C1001 | Sneha | 20 | CSE | CS1 |
+-----------+----------+-------+-------+---------+
iv) Modify the class ME2 to ME1:
mysql> update student set class='ME1' where Class='ME2';
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 |
+-----------+----------+-------+-------+---------+
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
v) Check for the uniqueness of Register no.:
mysql> select distinct Reg_No from student;
+------------+
| Reg_No |
+------------+
| M1001 |
| M1002 |
| C1001 |
| C1002 |
| E1001 |
| E1002 |
| E1003 |
+-----------+
RESULT:
Thus, the queries for student table using MySQL has been created and
executed successfully.
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
QUESTION: 8. PYTHON WITH CSV
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 AIM:
to display the name and the score. If the player name is not found give an To write a program using python for getting 10 players name and their
appropriate message. score, writing the input in a csv file, accepting a player name using python,
reading the csv file to display the name and the score, If the player name is not
found giving an appropriate message.
CODING:
import csv
with open('c:\\pyprg\\player.csv','w') as f:
w = csv.writer(f)
n=1
while(n<=10):
score=int(input("Score: "))
w.writerow([name,score])
n+=1
f.close()
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
SAMPLE OUTPUT: searchname=input("Enter the name to be searched: ")
RESULT:
Thus, the program Python with CSV has been created and executed
successfully.
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
QUESTION: 9. PYTHON WITH SQL
Create a SQL table using python and accept 10 names and age. sort in
descending order of age and display. AIM:
To create a SQL table using python and accepting 10 names, age and
sorting in descending order of age and displaying the result.
CODING:
import sqlite3
connection =sqlite3.connect("info.db")
cursor=connection.cursor()
for i in range(10):
n=len(who)
for i in range(n):
print("Displaying All the Records From student Table in Descending order of age:")
print(*cursor.fetchall(),sep='\n')
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
SAMPLE OUTPUT:
CODING:
import matplotlib.pyplot as plt
marks=[]
i=0
subjects=["Tamil","English","Maths","Physics","Computer"]
while i<5:
i+=1
for j in range(len(marks)):
plt.pie(marks,labels=subjects,autopct="%.2f")
plt.axes().set_aspect("equal")
plt.show()
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]
SAMPLE OUTPUT:
Enter Mark = 70
Enter Mark = 80
Enter Mark = 90
Enter Mark = 95
Enter Mark = 100
1.Tamil Mark =70
2.English Mark =80
3.Maths Mark =90
4.Physics Mark =95
5.Computer Mark =100
RESULT:
Thus, the program Python Graphics with Pip has been created and
executed successfully.
Prepared by Parkunan T, PG Asst. in Computer Science, MHSS, Chengam, Tiruvannamalai Cell: 9655 966 906 Mail: [email protected]