Python Programs
Python Programs
INDEX
SL. QUESTION PAGE
NO. NUMBER
PROGRAM NAME NUMBER
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
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
n=int(input("Enter n:"))
oddeven(n)
OUPUT:
Enter n : 7
7 is Odd
Enter n : 6
6 is Even
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
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]
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.
AIM:
To create Student Table with following fields and enter data as given in the table
below
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
Reg_No
M1001
M1002
C1001
C1002
E1001
E1002
E1003
7 rows in set (0.02 sec)
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
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)
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