0_Practical--2023 - 24
0_Practical--2023 - 24
Ex No: 1 List
Ex No: 2 Tuple
1. Write a python program to pass list to a function and double
the odd values and half even values of a list and display list 2. Write a Python program input n numbers in tuple and pass it
element after changing. to function to count how many even and odd numbers are
entered.
AIM: To write a program to pass list to a function and double the odd
values and half even values of a list and display list element after AIM: To write a program to input n numbers in tuple and pass it to
changing . function to count how many even and odd numbers are entered.
SOFTWARE USED: IDLE (PYTHON 3.8 64-bit) SOFTWARE USED : IDLE (PYTHON 3.8 64-bit)
PROGRAM CODE:
PROGRAM CODE:
def fun(lst):
def fun(lst): eve=0
list2=[ ] odd=0
for i in range(0, len(lst)): for i in range(0, len(lst)):
if(lst[i]%2==0): if lst[i]%2 == 0:
eve =lst[i]/2 eve+=1
list2.append(eve) else:
else: odd+=1
eve=lst[i]*2 print("Number of even numbers :", eve, "Number of odd numbers :",
list2.append(eve) odd)
print(list2)
x=eval(input("Enter a tuple :"))
a=eval(input("Enter a list :")) fun(x)
fun(a)
OUTPUT
OUTPUT: Enter a tuple :(1,2,3,4,5,6,7)
Number of even numbers : 3 Number of odd numbers : 4
Enter a list :[1,2,3,4,5]
[2, 1.0, 6, 2.0, 10] RESULT:
The above program has been executed successfully.
RESULT: -----------------------------------------------------------------------------------------------
The above program has been executed successfully. Ex No: 3 Dictionary
-----------------------------------------------------------------------------------------------
3. Write a Python program to function with key and value, and
update value at that key in dictionary entered by user.
1 | MVM
Class: 12 CS PRACTICALS: 2022-23
RESULT:
AIM:To write a program to function with key and value, and update The above program has been executed successfully.
value at that key in dictionary entered by user. -----------------------------------------------------------------------------------------------
SOFTWARE USED: IDLE (PYTHON 3.8 64-BIT) Ex No: 4 Count vowels in String
def fun(d,k): AIM:To write a program to pass a string to a function and count how
value2=eval (input("Enter the value :")) many vowels present in the string.
d[k]=value2
print ("Updated dictionary :", d) SOFTWARE USED : IDLE (PYTHON 3.8 64-BIT)
x=int (input ("Number of pairs in dictionary :"))
dic={ }
for i in range(x): PROGRAM CODE :
key = eval (input ("Enter the key :"))
value = eval(input ("Enter the value :")) def fun(lst):
dic[key]=value count = 0
print ("Original dictionary :", dic) for i in range (0, len(lst)):
a= eval(input("Enter the key whose value you want to change :")) if lst[i]=="a" or lst[i]=="A":
fun (dic, a) count+=1
if lst[i]=="e" or lst[i]=="E":
count+=1
OUTPUT if lst[i]=="i" or lst[i]=="I":
count+=1
Number of pairs in dictionary :3 if lst[i]=="o" or lst[i]=="O":
Enter the key :"name" count+=1
Enter the value :"sanjay" if lst[i]=="u" or lst[i]=="U":
Enter the key :"age" count+=1
Enter the value :"15" print("Number of vowels :", count)
Enter the key :"class" a=eval(input("Enter a String :"))
Enter the value :"10" fun(a)
Original dictionary : {'name': 'sanjay', 'age': '15', 'class': '10'}
Enter the key whose value you want to change :"age" OUTPUT
Enter the value :16
Updated dictionary : {'name': 'sanjay', 'age': 16, 'class': '10'} Enter a String :"computer Science"
Number of vowels : 6
RESULT:
2 | MVM
Class: 12 CS PRACTICALS: 2022-23
3 | MVM
Class: 12 CS PRACTICALS: 2022-23
8. Write a python program to remove all the lines that contain the
RESULT: character ‘a’ in a file and write it to another file.
The above program has been executed successfully.
----------------------------------------------------------------------------------------------- AIM:To write a program to remove all the lines that contain the
character ‘a’ in a file and write it to another file.
Ex No: 7 Display the file content line by line
SOFTWARE USED- IDLE (PYTHON 3.8 64-BIT)
7. Write a python program to read and display file content line by line
with each word separated by #. PROGRAM CODE:
AIM : To write a program to read and display file content line by line file=open(r"f:\result.txt", "r")
with each word separated by #. lines=file.readlines()
file.close()
SOFTWARE USED: IDLE (PYTHON 3.8 64-BIT) file=open(r"f:\result.txt", "w")
file1=open(r"f:\result1.txt", "w")
PROGRAM CODE: for li in lines:
if 'a' in li:
a= open(r"f:\result.txt", "r") file1.write(li)
lines=a.readlines() else:
for le in lines: file.write(li)
x=le.split() print("lines that contain 'a' character are removed from result")
for y in x: print ("Lines that contain 'a' character are added in result1 ")
print(y+ "#", end=" ") file.close()
print(" ") file1.close()
OUTPUT OUTPUT
RESULT:
The above program has been executed successfully.
-----------------------------------------------------------------------------------------------
4 | MVM
Class: 12 CS PRACTICALS: 2022-23
f1.write(c)
elif c.isupper():
f2.write(c)
else:
f3.write(c)
f1.close()
f2.close()
f3.close()
OUTPUT
RESULT:
Input "`" to stop execution.
The above program has been executed successfully.
Enter a single character :a
-----------------------------------------------------------------------------------------------
Enter a single character :b
Enter a single character :c
Ex No: 9 File Handling – Read characters and store in separate Enter a single character :d
file Enter a single character :e
Enter a single character :f
9. Write a python program to read characters from keyboard one by Enter a single character :g
one, all lower case letters gets stored inside a file “LOWER”, all Enter a single character :h
uppercase letters gets stored inside a file “UPPER”, and all other Enter a single character :i
characters get stored inside “OTHERS” 3 Enter a single character :j
Enter a single character :k
AIM: To write a program to read characters from keyboard one by one, Enter a single character :l
all lower case letters gets stored inside a file “LOWER”, all uppercase Enter a single character :M
letters gets stored inside a file “UPPER”, and all other characters get Enter a single character :N
stored inside “OTHERS” Enter a single character :O
Enter a single character :P
SOFTWARE USED- IDLE (PYTHON 3.8 64-bit) Enter a single character :Q
Enter a single character :R
INPUT Enter a single character :S
f1=open(r"f:\lower.txt",'w') Enter a single character :T
f2=open(r"f:\upper.txt",'w') Enter a single character :U
f3=open(r"f:\others.txt",'w') Enter a single character :V
print('Input "`" to stop execution. ') Enter a single character :W
while True: Enter a single character :X
c=input("Enter a single character :") Enter a single character :Y
if c=="~": Enter a single character :Z
break Enter a single character :1
elif c.islower(): Enter a single character :2
5 | MVM
Class: 12 CS PRACTICALS: 2022-23
6 | MVM
Class: 12 CS PRACTICALS: 2022-23
7 | MVM
Class: 12 CS PRACTICALS: 2022-23
OUTPUT:
{'C0001': ['Subham', 'Ahmedabad'], 'C0002': ['Bhavin', 'Anand'], Ex No: 14 CSV file with Employee details and their
'C0003': ['Chintu', 'Baroda']} Manipulations.
RESULT: 14. Write a python program to create a CSV file with empid, name and
The above program has been executed successfully. mobile no. and search empid, update the record and display the
----------------------------------------------------------------------------------------------- records.
Ex No: 13 File Handling - Create Binary file count records
AIM: To write a program to create a CSV file with empid, name and
13. Write a function to count records from the binary file marks.dat. mobile no. and search empid, update the record and display the
records.
8 | MVM
Class: 12 CS PRACTICALS: 2022-23
9 | MVM
Class: 12 CS PRACTICALS: 2022-23
10 | MVM
Class: 12 CS PRACTICALS: 2022-23
Create two tables for stationary and consumer and execute the given ii) To display the details of Stationary whose Price is in the
commands using SQL. range of 8 to 15 (Both values included).
Table: Stationary iii) To display the ConsumerName, Address from table
S_ID StationaryName Company Price Consumer and company and price from table Stationery with
their corresponding matching S_ID.
DP01 Dot Pen ABC 10 iv) To increase the price of all Stationary by 2.
PL02 Pencil XYZ 6 v) To display distinct Company from Stationary.
ER05 Eraser XYZ 7
PL01 Pencil CAM 5 PROGRAM CODE:
GP02 Gel Pen ABC 15 Create table stationary(S_ID char(5) not null primary key,
stationaryname char(25), company char(5), price int);
Table: Consumer insert into stationary values("DP01","Dot pen","ABC",10);
C_ID ConsumerName Address S_ID insert into stationary values("PL02","Pencil","XYZ",6);
insert into stationary values("ER05","Eraser","XYZ",7);
01 Good Learner Delhi PL01 insert into stationary values("PL01","Pencil","CAM",5);
06 Write Well Mumbai GP02 insert into stationary values("GP02","Gel pen","ABC",15);
12 Topper Delhi DP01
15 Write & Draw Delhi PL02 create table CONSUMER (C_ID int, ConsumerName char(25), Address
16 Motivation Bangalore PL01 char(25), S_ID char(5));
insert into CONSUMER values(01, “Good Learner”, “Delhi”, “PL01”);
i) To display the details of those Consumers whose Address is insert into CONSUMER values(06, “Write Well”, “Mumbai”, “GP02”);
Delhi. insert into CONSUMER values(12, “Topper”, “Delhi”, “DP01”);
ii) To display the details of Stationary whose Price is in the insert into CONSUMER values(15, “Write & Draw”, “Delhi”, “PL02”);
range of 8 to 15 (Both values included). insert into CONSUMER values(16, “Motivation”, “Banglore”, “PL01”);
iii) To display the ConsumerName, Address from table
OUTPUT:
Consumer and company and price from table Stationery with
i) Select * from consumer where Address=”Delhi”;
their corresponding matching S_ID.
iv) To increase the price of all Stationary by 2.
v) To display distinct Company from Stationary.
AIM: To create two tables for stationary and consumer and execute
the given commands using SQL. ii) Select * from stationary where price between 8 and 15;
i) To display the details of those Consumers whose Address is
Delhi.
11 | MVM
Class: 12 CS PRACTICALS: 2022-23
12 | MVM
Class: 12 CS PRACTICALS: 2022-23
13 | MVM
Class: 12 CS PRACTICALS: 2022-23
The above program has been executed successfully. iv) To display Doctor.Id. Name from the table Doctor and Basic,
--------------------------------------------------------------------------------------- Allowances from the table Salary with their corresponding
matching ID.
v) To display distinct department from the table Doctor.
15 | MVM
Class: 12 CS PRACTICALS: 2022-23
Ex No: 19 SQL CONNECTIVITY : FETCH RECORD 20. Write a python program (integrate python with SQL) to count
19. Write a python program (integrate python with SQL) to fetch the records from table.
records from table.
AIM: To write a python program (integrate python with SQL) to count
AIM: To write a python program (integrate python with SQL) to fetch records from table.
the records from table.
Program code:
PROGRAM CODE: import mysql.connector as sqltor
import mysql.connector as sqltor mycon=sqltor.connect(host="localhost",
mycon=sqltor.connect(host="localhost", user="root",password="1234",database="trinity")
if mycon.is_connected()==False:
user="root",password="1234",database="trinity") print("Error connecting to Mysql database")
if mycon.is_connected()==False: cursor=mycon.cursor()
print("Error connecting to Mysql database") cursor.execute("select * from student")
cursor=mycon.cursor() data=cursor.fetchone()
cursor.execute("select * from student") count=cursor.rowcount
data=cursor.fetchall() print("Total number of rows retrieved from resultset : ", count)
count=cursor.rowcount data=cursor.fetchone()
for row in data: count=cursor.rowcount
print(row)
print("Total number of rows retrieved from resultset : ", count)
mycon.close() data=cursor.fetchmany(3)
count=cursor.rowcount
OUTPUT: print("Total number of rows retrieved from resultset : ", count)
(1001, 'Arun', 23, 34, 45, 50, 44, 'Coimbatore') mycon.close()
(1002, 'Abhishek', 44, 50, 45, 35, 50, 'Coimbatore')
(1002, 'Swarna', 47, 50, 35, 39, 50, 'Coimbatore') OUTPUT:
Total number of rows retrieved from resultset : 1
RESULT: Total number of rows retrieved from resultset : 2
The above program has been executed successfully.
Total number of rows retrieved from resultset : 3
---------------------------------------------------------------------------------------
16 | MVM
Class: 12 CS PRACTICALS: 2022-23
Ex No: 21 SQL CONNECTIVITY : SEARCH RECORD Ex No: 22 SQL CONNECTIVITY : DELETE RECORD
21. Wrie a python program (integrate python with SQL) to search the 22. Write a python program (integrate python with SQL) to delete the
records from table. records from table.
AIM: To write a python program (integrate python with SQL) to search AIM : To write a python program (integrate python with SQL) to delete
the records from table. the records from table.
OUTPUT: mycon.commit()
py->sql connected mycon.close()
Enter num: 102
(102, 'abhishek', 35, 'coimbatore') OUTPUT:
py->sql connected
RESULT: Enter num: 101
17 | MVM
Class: 12 CS PRACTICALS: 2022-23
18 | MVM