Final Recordhhhhh
Final Recordhhhhh
1. Write a Python program which accepts a number from the user and pass it as
argument to the function. The function should return the sum of the digits of this
number. Display the number
as well as the sum.
def fun1(no):
s=0
while no>0: OUTPUT
dig=no%10
s+=dig Enter
number: 12345
The sum of
digits of 12345 is 15
no//=10
return s
2. Write a Python program to store ‘n’ numbers into a list and pass it to a
function. The function should count the number of even numbers in the list
divisible by 9 and print the count
as the output. (limit ‘n’ should be entered by the user)
def fun1(l):
c=0
for i in l:
if i%18==0:
c+=1
print('Number of even nos divisible by 9 are',c)
OUTPUT
fun1(l)
3. Write a Python program to accept a string from the user and pass it to a
function. The function should return the value 1 if the string starts and ends with
a vowel else return 0.
If the value returned is 1 print ‘String entered correctly’ else ‘Invalid
string’.
def fun1(s):
if s[0] in 'AEIOUaeiou' and s[-1] in 'AEIOUaeiou':
return 1
else: OUTPUT
return 0 Enter string:
Chevy
s=input("Enter string: ") Invalid string
fun1(s)
a=fun1(s)
if a==1:
print('String entered correctly')
else:
print('Invalid string')
4. Write a Python program to accept ‘n’ numbers into a tuple. Pass this tuple as
parameter to a function. Print the number of elements in the tuple which are
“Perfect”
(6 is a perfect number as 6 = sum of its factors 1+2+3).
def fun1(t):
c=0
for i in t:
s=0
a=i OUTPUT
for x in range(1,(i//2)+1): Enter no of
entries: 3
if a%x==0: Enter number: 6
s+=x Enter number: 12
if i==s: Enter number: 28
c+=1 2 perfect numbers
print(c,'perfect numbers')
fun1(t)
def fun1(d):
search=int(input("Enter book number to be searched: "))
OUTPUT
k=d.keys() Enter
no of key value pairs: 2
if search in k: Enter
book number: 10
print(d[search]) Enter
book name: heyjude
Enter
type: fiction
n=int(input("Enter no of key value pairs: ")) Enter
price: 500
d={} Enter
book number: 92
for i in range (n): Enter
book name: thebeatles
bookno=int(input("Enter book number: ")) Enter
type: non-fiction
bk_name=input("Enter book name: ") Enter
price: 1000
type=input("Enter type: ") Enter
book number to be searched: 92
price=int(input("Enter price: "))
('thebeatles', 'non-fiction', 1000)
d[bookno]=(bk_name,type,price)
fun1(d)
6.Write a menu driven program in Python which accepts a number and does the
following:
a. Pass the number to func1(). This function should print whether the number
is ‘a Palindrome’ or not
b. Pass this number to func2(), which in turn will check whether the number
is a prime number or not.
def func1(no):
s=0
num=no
while num>0:
dig=num%10
s=s*10+dig OUTPUT
num//=10 1) Check if number is a
palindrome
if no==s: 2) Check if the number
is prime
print("Palindrome") Enter choice: 1
else: Enter number: 121
print("Not a palindrome") Palindrome
Do you want to continue?
[yes/no]yes
def func2(no): 1) Check if number is a
palindrome
a=no 2) Check if the number
is prime
for x in range (2,(no//2)+1): Enter choice: 2
if a%x==0: Enter number: 14
print("Not prime") Not prime
break Do you want to continue?
[yes/no]no
else:
print("Prime number")
ans='yes'
while ans.lower()=='yes':
print("1) Check if number is a palindrome")
print("2) Check if the number is prime")
c=int(input("Enter choice: "))
no=int(input("Enter number: "))
if c==1:
func1(no)
elif c==2:
func2(no)
else:
print("Wrong choice")
ans=input("Do you want to continue?[yes/no]")
8. Write a program to find the duplicate elements in a list given by the user.
l=[]
l1=[]
l2=[]
n=int(input("Enter number of entries: "))
OUTPUT
for i in range (n): Enter
number of entries: 4
no=int(input("Enter number: ")) Enter
number: 13
l.append(no) Enter
number: 14
Enter
number: 14
for i in l: Enter
number: 15
if i not in l1:
Duplicate elements are [14]
l1.append(i)
else:
l2.append(i)
print("Duplicate elements are ",l2)
9. Write a Python program to accept a filename from the user and print the smallest
line in the text file.
10. Write a Python program to accept ‘n’ strings and store it to “original.txt”.
Copy all the lines containing the letter given by the user to another file
“copy.txt”.
f=open('original.txt','w')
n=int(input("Enter no of entries: "))
for i in range (n):
s=input("Enter string: ")
OUTPUT
f.write(s+'\n')
Enter no of entries: 3
f.close()
Enter string: time
Enter string: to
letter=input("Enter letter: ")
Enter string: sleep
Enter letter: t
f=open('original.txt')
time
data=f.readlines()
to
f1=open('copy.txt','w')
for i in data:
if letter in i:
f1.write(i)
f1.close()
f2=open('copy.txt')
a=f2.read()
print(a)
f2.close()
11. Write a Python program to accept ‘n’ names and store it to a file where
filename is given by the user. Accept a name from the user. Print whether this name
appears in the file.
Print appropriate message on the screen.
12. Write a Python program to define a dictionary with keys starting from 1 to 7.
The corresponding value will be the name of the day. Eg. 1: “Sunday” as the first
element of the
dictionary. Write this dictionary to the binary file “day.dat”. Generate a
number between 1 and 7 randomly and search for this number in the file and print
the corresponding name of
the day with its number.
import pickle
import random
d={1:'Sunday',2:'Monday',3:'Tuesday',4:'Wednesday',5:'Thursday',6:'Friday',7:'Satur
day'}
f=open('day.dat','wb')
OUTPUT
pickle.dump(d,f)
Saturday 7
f.close()
no=random.randint(1,7)
f=open('day.dat','rb')
data=pickle.load(f)
f.close()
for i in data:
if no==i:
print(data[i],i)
13. Write a Python program to accept ‘n’ elements for a dictionary. The key of the
dictionary has to be ‘fam_code’ while the value will be a list made up
of ‘name’, ‘family_name’. Write this dictionary to a binary file ‘family.dat’.
Display the content of ‘family.dat’ where family_name is a palindrome.
import pickle
d={}
n=int(input("Enter no of key-value pairs: ")) OUTPUT
for i in range(n): Enter no of
key-value pairs: 2
fam_code=int(input("Enter family code: ")) Enter family
code: 1
name=input("Enter name: ") Enter name:
ringo
family_name=input("Enter family name: ") Enter family
name: starr
d[fam_code]=[name,family_name] Enter family
code: 7
Enter name:
george
f=open("family.dat",'wb') Enter family
name: kkk
pickle.dump(d,f) 7 ['george',
'kkk']
f.close()
f1=open("family.dat",'rb')
data=pickle.load(f1)
for i in data:
if data[i][1]==data[i][1][::-1]:
print(i,data[i])
f1.close()
import csv
l=[]
heading=['slno','country','currency']
n=int(input("Enter number of lists: "))
OUTPUT
for i in range (n):
Enter number of lists: 3
country=input("Enter country: ")
Enter country: india
currency=input("Enter currency: ")
Enter currency: ruppee
l.append([i+1,country,currency])
Enter country: america
f1=open('country.csv','r')
a=csv.reader(f1)
for i in a:
print(i)
f1.close()
stack=[]
n=int(input("Enter number of entries: "))
OUTPUT
for i in range (n): Enter
number of entries: 1
country_code=int(input("Enter country code: ")) Enter
country code: 10
country_name=input("Enter country name: ") Enter
country name: india
stack.append([country_code, country_name]) 1) To
insert a list
2) To
delete a list
ans='yes' 3) To
display list
while ans.lower()=='yes': Enter
choice: 1
print("1) To insert a list") Enter
country code: 20
print("2) To delete a list") Enter
country name: usa
print("3) To display list") Do you
want to continue?[yes/no]yes
c=int(input("Enter choice: ")) 1) To
insert a list
2) To
delete a list
if c==1: 3) To
display list
a=int(input("Enter country code: ")) Enter
choice: 3
b=input("Enter country name: ") [[10,
'india'], [20, 'usa']]
stack.append([a,b]) Do you
want to continue?[yes/no]yes
1) To
insert a list
2) To
delete a list
elif c==2: 3) To
display list
if stack==[]: Enter
choice: 2
print("Empty stack") Do you
want to continue?[yes/no]yes
else: 1) To
insert a list
stack.pop() 2) To
delete a list
3) To
display list
Enter
choice: 3
elif c==3: [[10,
'india']]
print(stack) Do you
want to continue?[yes/no]no
else:
print("Wrong choice")
ans=input("Do you want to continue?[yes/no]")
16. Write a Python program to implement a stack with district_code, district of
Kerala as information stored in stack in the form of dictionary with
‘district_code’ as key and ‘district’ as value.
stack=[]
OUTPUT
for i in range (3):
Enter district code: WA
district_code=input("Enter district code: ")
Enter district: Wayanad
district=input("Enter district: ")
Enter district code: PL
stack.append({district_code:district})
Enter district: Palakkad
print(stack)
Enter district code: ER
17. Write a Python program to create a database ‘world_info’. Use this database
and create a table with the following details :
import mysql.connector as c
db=c.connect(host='localhost',user='root',passwd='heyjude')
OUTPUT
cursor=db.cursor()
Table created
cursor.execute('create database world_info')
cursor.execute('use world_info')
cursor.execute("create table country (country_no numeric Primary key,country_name
char(10) NOT NULL unique,language char(15))")
print("Table created")
18. Write a Python program to write 3 records to the above table ‘country’.
Display the contents of ‘country’ table as output.
import mysql.connector as c
db=c.connect(host='localhost',user='root',passwd='heyjude')
cursor=db.cursor()
OUTPUT
cursor.execute('use world_info')
(1, 'India', 'Hindi')
cursor.execute("Insert into country values(1,'India','Hindi')")
(2, 'Qatar', 'Arabic')
cursor.execute("Insert into country values(2,'Qatar','Arabic')")
(3, 'USA', 'English')
cursor.execute("Insert into country values(3,'USA','English')")
db.commit()
cursor.execute('select * from country')
result=cursor.fetchall()
for x in result:
print(x)
19. Write a Python program to update the ‘country’ table with a new language for a
given country_no received from the user. Print the new contents of
the ‘country’ table as output.
import mysql.connector as c
db=c.connect(host='localhost',user='root',passwd='heyjude',database='world_info')
cursor=db.cursor()
OUTPUT
no=int(input("Enter country number: "))
Enter country number: 1
lan=input("Enter language: ")
Enter language: Malayalam
data=(lan,no)
(1, 'India', 'Malayalam')
u="update country set language=%s where country_no=%s"
(2, 'Qatar', 'Arabic')
cursor.execute(u,data)
(3, 'USA', 'English')
db.commit()
cursor.execute('select * from country')
result=cursor.fetchall()
for x in result:
print(x)
20.Write a Python program to delete country details from the ‘country’ table for a
given country number given by the user. Display the table contents
after deletion as the output.
import mysql.connector as c
db=c.connect(host='localhost',user='root',passwd='heyjude',database='world_info')
OUTPUT
country_no=int(input("Enter country number: "))
Enter country number: 2
cursor=db.cursor()
(1, 'India', 'Malayalam')
cursor.execute("Delete from country where country_no=%s"%(country_no,))
(3, 'USA', 'English')
db.commit()
ANSWERS
import ____________ as mc
mysql.connector
mydb=mc.connect(host="localhost",user="root",passwd="root",database="school")
mycursor = _______________
mydb.cursor
mycursor.execute("_________") # to display the list of tables
SHOW TABLES
for x in _________:
mycursor
print(x)
MYSQL COMMANDS
+----+-------------+---------+---------------+
| No | Name | Phone | Address |
+----+-------------+---------+---------------+
| 1 | Arpit Kumar | 7045634 | Subhash Nagar |
| 6 | Rahul Verma | 7057456 | Subhash Nagar |
+----+-------------+---------+---------------+
(b) Update the database set the phone no as 7047645 where phone number is
7057456.
update Directory set phone=7047645 where phone=7057456;
+----+----------------+---------+---------------+
| No | Name | Phone | Address |
+----+----------------+---------+---------------+
| 1 | Arpit Kumar | 7045634 | Subhash Nagar |
| 2 | Ram Sharma | 5563412 | Vikas Nagar |
| 3 | Vikas Malhotra | 7865467 | Palam Nagar |
| 4 | Rohit Arora | 2235434 | Preet Vihar |
| 5 | Kisan Kaushik | 5567845 | Pashim Vihar |
| 6 | Rahul Verma | 7047645 | Subhash Nagar |
| 7 | Rakesh Gulati | 7026519 | Palam Nagar |
+----+----------------+---------+---------------+
+----+---------------+---------+---------------+
| No | Name | Phone | Address |
+----+---------------+---------+---------------+
| 1 | Arpit Kumar | 7045634 | Subhash Nagar |
| 5 | Kisan Kaushik | 5567845 | Pashim Vihar |
| 6 | Rahul Verma | 7057456 | Subhash Nagar |
+----+---------------+---------+---------------+
+----+----------------+---------+---------------+
| No | Name | Phone | Address |
+----+----------------+---------+---------------+
| 1 | Arpit Kumar | 7045634 | Subhash Nagar |
| 2 | Ram Sharma | 5563412 | Vikas Nagar |
| 3 | Vikas Malhotra | 7865467 | Palam Nagar |
| 5 | Kisan Kaushik | 5567845 | Pashim Vihar |
| 6 | Rahul Verma | 7047645 | Subhash Nagar |
| 7 | Rakesh Gulati | 7026519 | Palam Nagar |
+----+----------------+---------+---------------+
+-------------------------+
| count(Distinct Address) |
+-------------------------+
| 6 |
+-------------------------+
+---------------+
| Name |
+---------------+
| Rakesh Gulati |
+---------------+
+---------+
| Phone |
+---------+
| 2235434 |
| 5567845 |
| 7026519 |
+---------+
(iv) Select Name, Phone from Directory where Name like “R_%”;
+---------------+---------+
| Name | Phone |
+---------------+---------+
| Ram Sharma | 5563412 |
| Rohit Arora | 2235434 |
| Rahul Verma | 7057456 |
| Rakesh Gulati | 7026519 |
+---------------+---------+
2)Consider the following tables Sender and Recipient. Write SQL commands for the
statements (i) to (iv) and give outputs for SQL queries under (v)
Table: Sender
Table: Recipient
+-------+------------+-------------------+------------
+-----------------------+
| RecID | SenderName | SenderAddress | RecName | RecAddress
|
+-------+------------+-------------------+------------
+-----------------------+
| KO05 | R Jain | 2 ABC Appts | R Bajpayee | 5 Central Avenue
|
| MU19 | R Jain | 2 ABC Appts | H Singh | 2A Andheri
|
| ND08 | H Sinha | 12 Newtown | S Mahajan | 116 A Vihar
|
| MU32 | S Jha | 27/A Park Street | P K Swamy | B5 C S Terminus
|
| ND48 | T Prasad | 122-K SDA | S Tripathi | 13 B1 D Mayur Vihar
|
+-------+------------+-------------------+------------
+-----------------------+
+-------+----------+------------+-----------------------+-------------+
| RecID | SenderID | RecName | RecAddress | RecCity |
+-------+----------+------------+-----------------------+-------------+
| MU19 | ND01 | H Singh | 2A Andheri | East Mumbai |
| MU32 | MU15 | P K Swamy | B5 C S Terminus | Mumbai |
| KO05 | ND01 | R Bajpayee | 5 Central Avenue | Kolkata |
| ND08 | MU02 | S Mahajan | 116 A Vihar | New Delhi |
| ND48 | ND50 | S Tripathi | 13 B1 D Mayur Vihar | New Delhi |
+-------+----------+------------+-----------------------+-------------+
+------------+
| SenderCity |
+------------+
| New Delhi |
| Mumbai |
+------------+
+------------+-----------+
| SenderName | RecName |
+------------+-----------+
| S Jha | P K Swamy |
+------------+-----------+
(c) SELECT RecName, RecAddress FROM Recipient WHERE RecCity NOT IN (‘Mumbai’,
‘Kolkata’);
+------------+-----------------------+
| RecName | RecAddress |
+------------+-----------------------+
| S Mahajan | 116 A Vihar |
| H Singh | 2A Andheri |
| S Tripathi | 13 B1 D Mayur Vihar |
+------------+-----------------------+
+-------+------------+
| RecID | RecName |
+-------+------------+
| ND08 | S Mahajan |
| ND48 | S Tripathi |
+-------+------------+
3) Consider the following tables ACTIVITY and COACH and answer (a) and (b) parts
of this question:
Table: Activity
Acode ActivityName Stadium ParticipantsNum Prize Money Schedule
Date
1001 Relay 100x4 Star Annex 16 10000 23-Jan-
2004
1002 High jump Star Annex 10 12000 12-Dec-
2003
1003 Shot Put Super Power 12 8000 14-Feb-
2004
1005 Long Jump Star Annex 12 9000 01-Jan-
2004
1008 Discuss Throw Super Power 10 15000 19-Mar-
2004
Table: Coach
Pcode Name Acode
1 Ahmad Hussain 1001
2 Ravinder 1008
3 Janila 1001
4 Naaz 1003
(i) To display the names of all activities with their Acodes in descending order.
+---------------+
| ActivityName |
+---------------+
| Discuss Throw |
| Long Jump |
| Shot Put |
| High jump |
| Relay 100x4 |
+---------------+
(ii) To display sum of PrizeMoney for the Activities played in each of the Stadium
separately.
+-----------------+
| sum(PrizeMoney) |
+-----------------+
| 31000 |
| 23000 |
+-----------------+
(iii) To display the coach’s name and ACodes in ascending order of ACode from the
table COACH.
(iv) To display the content of the Activity table whose ScheduleDate earlier than
01/01/2005 in ascending order of ParticipantsNum.
+-------+---------------+-------------+-----------------+------------
+--------------+
| Acode | ActivityName | Stadium | ParticipantsNum | PrizeMoney |
ScheduleDate |
+-------+---------------+-------------+-----------------+------------
+--------------+
| 1002 | High jump | Star Annex | 10 | 12000 | 2003-
12-12 |
| 1008 | Discuss Throw | Super Power | 10 | 15000 | 2004-
03-19 |
| 1003 | Shot Put | Super Power | 12 | 8000 | 2004-
02-14 |
| 1005 | Long Jump | Star Annex | 12 | 9000 | 2004-
01-01 |
| 1001 | Relay 100x4 | Star Annex | 16 | 10000 | 2004-
01-23 |
+-------+---------------+-------------+-----------------+------------
+--------------+
+---------------------------------+
| COUNT(DISTINCT ParticipantsNum) |
+---------------------------------+
| 3 |
+---------------------------------+
+-------------------+-------------------+
| MAX(ScheduleDate) | MIN(ScheduleDate) |
+-------------------+-------------------+
| 2004-03-19 | 2003-12-12 |
+-------------------+-------------------+
(iii) SELECT Name, ActivityName FROM ACTIVITY A, COACH C WHERE A.Acode=C.Acode AND
A.ParticipantsNum=10;
+----------+---------------+
| Name | ActivityName |
+----------+---------------+
| Ravinder | Discuss Throw |
+----------+---------------+