Practical Cs PDF
Practical Cs PDF
2
PY1(a) - Calculate Factorial
1(a) Write a program to calculate the factorial of the given number using for loop
C o ding
Enter a Number: 12
Factorial of 12 is 479001600
PY1(b) - Sum of Series
1(b) Write a program to sum the series:1/1 + 22/2 + 33/3 + ……. nn/n
C o ding
3
PY2(a) - Odd or Even
2(a) Write a program using functions to check whether a number is even or odd
C o ding
defoddeven(a):
if (a0==2%):
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")
Output:
Enter a number: 7
The given number is Odd
Enter a number: 6
The given number is Even
C o ding
def rev(str1):
str2=''
i=len(str1-(1
while i>=0:
str+2=str1[i]
i-=1
return str2
word = input("\n Enter a String: ")
print("\n The Mirror image of the given string is: ", rev(word))
Output:
4
PY3 – Generate values and remove odd numbers
Write a program to generate values from 1 to 10 and then remove all the
3(a) odd numbers from the list
C o ding
num1=[]
for i in range(1,11):
num1.append(i)
print("Numbers from 1 to 10.....\n",num1)
for j, i in enumerate(num1):
if(i1==2%):
del num1[j]
print("The values after removed odd numbers.....\n",num1)
Output:
5
PY4 – Generate Prime numbers and Set Operations
Write a Program that generate a set of prime numbers and another set of odd
4. numbers. Display the result of union, intersection, difference and
symmetric difference operations
C o ding
odd=set([x*1+2 for x in range(0,5)])
primes=set()
for i in range(2,10):
j=2
f=0
while j<i/2:
ifi%j==0:
f=1
j+=1
if f==0:
primes.add(i)
print("Odd Numbers: ", odd)
print("Prime Numbers: ", primes)
print("Union: ", odd.union(primes))
print("Intersection: ", odd.intersection(primes))
print("Difference: ", odd.difference(primes))
print("Symmetric Difference: ", odd.symmetric_difference(primes))
Output:
6
PY5 – Display sting elements – Using Class
class String:
def __init__(self):
self.uppercase=0
self.lowercase=0
self.vowels=0
self.consonants=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', 'l', 'L')):
self.vowels+=1
def count_consonants(self):
for ch in self.string:
if (ch not in ('A', 'a', 'e', 'E', 'i', 'I', 'o', 'O', 'l', 'L')):
self.consonants+=1
def count_space(self):
for ch in self.string:
if (ch==""):
7
self.spaces+=1
def execute(self):
self.count_upper()
self.count_lower()
self.count_vowels()
self.count_consonants()
self.count_space()
def display(self):
print("The given string contains...")
print("%d Uppercase letters"%self.uppercase)
print("%d Lowercase letters"%self.lowercase)
print("%d Vowels"%self.vowels)
print("%d Consonants"%self.consonants)
print("%d Spaces"%self.spaces)
S = String()
S.getstr()
S.execute()
S.display()
Output:
8
DB6 – MySQL Employee Table
Create an Employee Table with the fields Empno, Empname, Desig, Dept,
6.
• Add two more records to the table.
• Modify the table structure by adding one more field namely date of joining.
9
(iv) Select all the record:
mysql> select * from Employee;
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
10
Place varchar(10) YES NULL
doj date YES NULL
11
DB7 – MySQL Student Table
Create Student table with following fields and enter data as given in the
7 table below
Reg_No char 5
Sname varchar 15
Age int 2
Dept varchar 10
Class char 3
Data to be entered
12
SQL Queries and Output:
13
E1001 Ravi 20 ECE EC1
Sname
Reg_No Age Dept Class
Harish
M1001 19 ME ME1
Leena
E1002 21 CSE EE1
Ravi
E1001 20 ECE EC1
Sneha
C1001 20 EEE CS1
14
Sname
Reg_No Age Dept Class
Harish
M1001 19 ME ME1
Akash
M1002 20 ME ME2
Sneha
C1001 20 CSE CS1
Lithya
C1002 19 CSE CS2
Ravi
E1001 20 ECE EC1
Leena
E1002 21 EEE EE1
Rose
E1003 20 ECE EC2
Reg_No
M1001
M1002
C1001
C1002
E1001
E1002
E1003
15
PY8 – Python with CSV
C o ding
importcsv
with open('c:\\pyprg\\player.csv','w') as f:
w = csv.writer(f)
n=1
while (n<=10):
name = input("Player Name?:" )
score = int(input("Score: "))
w.writerow([name,score])
n+=1
print("Player File created")
f.close()
searchname=input("Enter the name to be searched ")
f=open('c:\\pyprg\\player.csv','r')
reader =csv.reader(f)
lst=[]
for row in reader:
lst.append(row)
q=0
for row in lst:
if searchname in row:
print(row)
q+=1
if(q==0):
print("string not found")
f.close()
16
Output:
17
PY9 – Python with SQL
Create a sql table using python and accept 10 names and age .sort in
9
C o ding
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' )
18
Output:
19
PY10 – Python Graphics with Pip
Write a program to get five marks using list and display the marksin pie
10
C o ding
importmatplotlib.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.axes().set_aspect ("equal")
plt.show()
Output:
Enter Mark = 67
Enter Mark = 31
Enter Mark = 45 English
29.18 Social
3.Maths Mark = 45
5.Social Mark = 73
20