Professional Documents
Culture Documents
Download
Download
Download
EX.NO: 1
DATE:
OPERATIONS
AIM:
SOURCE CODE:
print("1. Adddition")
print("2. Subtraction")
print("3. Multiplication")
print ("4. Division")
opt=int(input("Enter
your choice:"))
a=int (input("Enter the First Number:"))
b=int (input ("Enter the Second Number:")}
if opt=1:
c=a+b
elif opt==2:
c-a-b
elif opt==3:
c=a*b
elif opt==4:
Else:
if b==0:
c=a/b
print("The Division of two number is:",c)
print("Invalid Option")
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Run - 1:
1. Adddition
2.
Subtraction
3. Multiplication
4. Division
RUN- 2
1. Adddition
2.
Subtraction
3. Multiplication
4. Division
DATE:
AIM:
SOURCE CODE:
First=0
Second=1
else:
print (First)
print (Second)
for i in range (2,no):
Third First+Second
First Second
Second-Third
print (Third)
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
RUN -1:
1
1
2
3
5
8
13
RUN -2:
DATE:
AIM:
To write a menu driven Python Program to find Factorial and sum of list of numbers using function.
SOURCE CODE:
if no<0:
print ("Sorry, we cannot take Factorial for Negative number") elif no==0:
print("The Factorial of 0 is 1")
else:
#Main Program
if opt=1:
n=int(input("Enter a number to find Factorial:")) Factorial
(n)
elif opt=2:
Result:
L=[]
RUN -1:
1. To Find Factorial
RUN - 2:
1. To Find Factorial
25
32
45
12
DATE:
AIM:
SOURCE CODE:
import math
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
BETWEEN 1 TO 6
AIM:
SOURCE CODE:
import random
while True:
break
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
********
EX.NO: 6
DATE:
AIM:
To write a Python Program to Read a text file "Story.txt" line by line and display each word
separated by '#'.
SOURCE CODE:
words=line.split() for i in
words:
f.close()
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Story.txt:
Story b
C/Users/APPS/AppData/Local/Programs/Pyt
DATE:
CREATING A PYTHON PROGRAM TO READ A TEXT FILE AND DISPLAY THE NUMBER OF
VOWELS/CONSONANTS/LOWER CASE/ UPPER CASE CHARACTERS.
AIM:
To write a Python Program to read a text file "Story.txt" and displays the number of Vowels/
Consonants/ Lowercase / Uppercase/characters in the file.
SOURCE CODE:
f=open("Story.txt", 'r')
Contents f.read()
Vowels=0
Consonants=0
Lower case=0
Upper case=0
for ch in Contents:
if ch in 'aeiouAEIOU':
Vowels-Vowels+1
if ch in 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ":
Consonants-Consonants+1
if ch.islower():
Lower_case-Lower_case+1
if ch.isupper():
Upper_case=Upper_case+1
f.close()
print("The total numbers of vowels in the file:", Vowels) print ("The total
numbers of consonants in the file:", Consonants) print("The total numbers
of uppercase in the file:", Upper_case) print("The total numbers of
lowercase in the file:", Lower_case)
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Story.txt:
| “Story: bxt -
C/Users/APPS/AppData/Local/Programs/Python/Python36/Story.txt
(367)
File Edit Format Run Options Window Help
DATE:
AIM:
To write a python program to read lines from a text file "Sample.txt" and copy those lines into another file
which are starting with an alphabet 'a' or 'A'.
SOURCE CODE:
F1-open ("Sample.txt",
'r')
F2=open ("New.txt", 'w')
while True:
line F1.readline ()
if line';
break
F2.write(line)
print("All lines which are starting with character 'a' or 'A' has been copied
successfully into New.txt") F1.close()
F2.close()
Result:
Thus the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Sample.txt:
RESTART:
C:\Users\APPS\AppData\Local\Programs\Python\Python36\PR_COPY_TEXT
_FILE.py
-
All lines which are starting with character 'a' or 'A' has been copied
successfully into New.txt
New.txt:
DATE:
BINARY FILE
AIM:
To write a Python Program to Create a binary file with roll number and name. Search for a given roll number
and display the name, if not found display appropriate message.
SOURCE CODE:
import pickle
def Create():
def Search():
F=open("Students.dat", 'rb')
no=int(input("Enter Roll. No of student to search: "))
found=0
try:
while True:
except:
S pickle.load (F)
if s[0]==no:
print("The searched Roll. No is found and Details are:",S) found=1
break
F.close()
if found=0:
#Main Program
Create ()
Search ()
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUPUT:
DATE:
CREATING A PYTHON PROGRAM TO CREATE AND UPDATE/MODIFY RECORDS IN
BINARY FILE
AIM:
To write a Python Program to Create a binary file with roll number, name, mark and update/modify the mark for
a given roll number.
SOURCE CODE:
import pickle
def Create():
while opt=='y':
Roll No=int(input("Enter roll number:'))
Name=input("Enter Name:")
Mark=int(input("Enter Marks: "))
L= [Roll No, Name, Mark]
pickle.dump (L,F)
opt=input("Do you want to add another student detail (y/n) :")
F.close()
def Update():
F=open ("Marks.dat", 'rb+')
no=int(input("Enter Student Roll. No to modify marks: "))
found=0
try:
while True:
except:
Pos=F.tell()
S-pickle.load (F)
if s[0]==no:
F.close()
if found==0:
print("The searched Roll. No is found and Details are:",S)
S[2]=int(input("Enter New Mark to be update:"))
F.seek(Pos)
pickle.dump (S, F)
found=1
F. seek (Pos) #moving the file pointer again beginning of the current record. print("Mark updated
Successfully and Details are:",S)
break
#Main Program
Create()
Update ()
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
***
EX.NO: 11
DATE:
IN CSV FILE.
AIM:
To write a Python program Create a CSV file to store Empno, Name, Salary and search any Empno and
display Name, Salary and if not found display appropriate
message.
SOURCE CODE:
import csv
def Create():
F=open ("Emp.csv", 'a', newline='')
W=csv.writer (F)
opt='y'
while opt=='y':
No=int(input("Enter Employee Number:"))
Name=input("Enter Employee Name:")
Sal float (input ("Enter Employee Salary:")) L= [No, Name, Sal]
W.writerow (L)
opt-input ("Do you want to continue (y/n) ?:")
F.close()
def Search():
IOW=CSV.reader (F)
for data in row:
if data[0]==str(no):
print("\nEmployee Deatils are:")
print("▬▬▬▬
print("Name:", data [1])
print ("Salary: ", data[2])
print("s
print (":
found=1
break
if found==0:
#Main Program
Create ()
Search ()
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Name: Balu
Salary: 27000.0
EX.NO: 12
DATE:
AIM:
CREATING A PYTHON PROGRAM TO IMPLEMENT STACK OPERATIONS
SOURCE CODE:
def PUSH():
def POP():
if Stack=[]:
print("Stack is Empty/Underflow")
else:
def PEEK():
top-len (Stack) -1
print("The top most element of Stack is:", Stack[top])
def Disp():
top-len (Stack) -1
print ("The Stack elements are:")
for i in range (top,-1,-1):
print (Stack[i])
#Main Program
Stack=[]
opt='y'
while opt 'y' or opt== 'Y':
Result:
print("Stack Operations")
print("*****
print("1. PUSH")
print ("2. POP")
print("3. PEEK")
print("4. Display")
print("*****************)
opt-int (input ("Enter your choice:"))
if opt=1:
PUSH ()
elif opt 2:
POP ()
elif opt=-3:
PEEK ()
elif opt==4:
Disp ()
else:
print("Invalid Input")
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Stack Operations
1. PUSH
2. POP
3. PEEK
4. Display
1. PUSH
2. POP
3. PEEK
4. Display
1. PUSH
2. POP
3. PEEK
4. Display
1. PUSH
2. POP
3. PEEK
4. Display
20
10
Stack Operations
1. PUSH
2. POP
3. PEEK
4. Display
1. PUSH
2. POP
3. PEEK
4. Display
1. PUSH
2. POP
3. PEEK
4. Display
10
**
EX.NO: 13
DATE:
CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL WITH PYTHON
AIM:
To write a Python Program to integrate MYSQL with Python by inserting records to Emp table and
display the records.
SOURCE CODE:
import mysql.connector
con=mysql.connector.connect (host='localhost', username='root', password='root',
database='employees') if con.is_connected():
cur=con.cursor ()
opt='y'
while opt=='y':
No-int (input("Enter Employee Number: "))
Name=input("Enter Employee Name:")
Gender input("Enter Employee Gender (M/F) :")
Salary-int (input("Enter Employee Salary:"))
Query="INSERT INTO EMP VALUES ({}, '(',')', {})".format (No, Name, Gender, Salary)
cur.execute (Query)
con.commit()
print ("Record Stored Successfully")
opt=input ("Do you want to add another employee details (y/n) :")
print(i)
con.close()
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
SOL OUTPUT:
| EMPID | NAME
GENDER SALARY |
+
1❘ Arun
| M
2 Bala
| M
3 | Bavya
4
Saravanan
MMF M
20000 |
25000 |
F
27000 |
M
29000 |
+
+
+
DATE:
AIM:
To write a Python Program to integrate MYSQL with Python to search an Employee using EMPID and
display the record if present in already existing table EMP, if not display the appropriate message.
SOURCE CODE:
import mysql.connector
con-mysql.connector.connect (host='localhost', username='root',password='root',
database='employees') if con.is connected():
cur=con.cursor()
print("
print("Welcome to Employee Search Screen")
print("
*****")
else:
print (data)
print("Record not Found!!!")
con.close()
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
RUN-1:
Run - 2:
SOL OUTPUT:
OUTPUT -1:
2 | Bala | M
1 row in set (0.00 sec)
OUTPUT -2:
25000
DATE:
(UPDATING RECORDS)
AIM:
To write a Python Program to integrate MYSQL with Python to search an Employee using EMPID and update
the Salary of an employee if present in already existing table EMP, if not display the appropriate
message.
SOURCE CODE:
import mysql.connector
con=mysql.connector.connect (host='localhost', username='root',password='root', database='employees') if
con.is connected():
cur=con.cursor()
print("
print("Welcome to Employee detail update Screen") print("**
****"]
cur.execute (Q2)
data=cur.fetchall ()
for i in data:
print (i)
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
RUN -1:
Do you want to update the Salary of the above employee (y/n)?: y Enter the
New Salary of an Employee: 30000
Employee Salary Updated Successfully
(1, 'Arun', 'M', 20000)
(2,
'Bala' 'M' 25000)
1
Run -2:
SOL OUTPUT:
3 | BAVYA | F
+
27000 |
EMPID❘ NAME
GENDER | SALARY |
+
1 | Arun
M
20000
2❘ Bala
| M
25000
3
BAVYA
F
30000
4
Saravanan | M
29000
Ex.No: 16
DATE:
AIM:
SQL COMMANDS EXERCISE - 1
To write Queries for the following Questions based on the given table:
COMPUTER 1997-01-10
Fees
120
2
Ankit
M
21
HISTORY
1998-03-24
200
3
Anu
F
20
HINDI
1996-12-12
300
4
Bala
M
19
NULL
1999-07-01
400
5
Charan
M
18
HINDI
1997-09-05
250
6
Deepa
F
19
HISTORY
1997-06-27
300
7
Dinesh
M
22
COMPUTER 1997-02-25
210
8
Usha
F
23
NULL
1997-07-31
200
Sol:
Sol:
mysql> SHOW DATABASES;
Output:
| Database
employees
hospital
hospitals
information_schema
ip_practicals
mysql
performance_schema
school
students
sys
(e) Write a Query to List all the tables that exists in the current database.
Sol:
Output:
| Tables_in_students
||stu
(f) Write a Query to insert all the rows of above table into Info table.
Sol:
Sol:
Output:
Rollno❘ Name | Gender ❘ Age Dept ❘
DOA
Fees
1 Arun 2❘ Ankit
M
24❘ COMPUTER
21❘ HISTORY
3
Anu
F
20❘ HINDI
1997-01-10 129 1998-03-24 | 200
1996-12-12
300
4 Bala
M
19❘ NULL
1999-07-01 | 480
5
Charan H
18 HINDI
1997-06-27 | 250
6 | Deepa
F
7 Dinesh H B❘ Usha
19❘ HISTORY 22
COMPUTER
1997-06-27 | 388
1997-02-25
210
F
23
NULL
| 1997-07-31
200
(h) Write a query to Rollno, Name and Department of the students from STU table.
Sol:
1 | Arun
2 Ankit
3
Anu
COMPUTER
HISTORY HINDI
NULL
4 | Bala
S❘ Charan | HINDI
6❘ Deepa
HISTORY
7 Dinesh | COMPUTER
8❘ Usha | NULL
Ex.No: 17 DATE:
AIM:
SQL COMMANDS EXERCISE - 2
To write Queries for the following Questions based on the given table:
1997-01-10
Fees
120
2
Ankit
M
21
HISTORY
1998-03-24
200
3
Anu
F
20
HINDI
1996-12-12
300
4
Bala
M
19
NULL
1999-07-01
400
5
Charan
M
18
HINDI
1997-09-05
250
6
Deepa
F
19
HISTORY
1997-06-27
300
7
Dinesh
M
22
COMPUTER 1997-02-25
210
8
Usha
F
23
NULL
1997-07-31
200
Output:
DEPT
COMPUTER
HISTORY
HINDI NULL
Output:
2❘ Ankit M
6 | Deepa
21❘ HISTORY 1998-03-24 | 19 |
HISTORY | 1997-06-27 |
200
300
(c) Write a Query to list name of female students in Hindi Department.
Sol:
Output:
| NAME
| Anu
(d) Write a Query to list name of the students whose ages are between 18 to 20.
Sol:
mysql> SELECT NAME FROM STU WHERE AGE BETWEEN 18 AND 20;
Output:
NAME
Anu
BalaCharan
| Deepa
(e) Write a Query to display the name of the students whose name is starting with 'A'.
Sol:
Output:
NAME
Arun
Ankit
Anu
(f) Write a query to list the names of those students whose name have second alphabet 'n' in their
names.
Sol:
| NAME
+
Ankit Anu
+
Ex.No: 18 DATE:
AIM:
SQL COMMANDS EXERCISE - 3
To write Queries for the following Questions based on the given table:
1997-01-10
Fees
120
2
Ankit
M
21
HISTORY
1998-03-24
200
3
Anu
F
20
HINDI
1996-12-12
300
4
Bala
M
19
NULL
1999-07-01
400
5
Charan
M
18
HINDI
1997-09-05
250
6
Deepa
F
19
HISTORY
1997-06-27
300
7
Dinesh
M
22
COMPUTER 1997-02-25
210
8
Usha
F
23
NULL
1997-07-31
200
Rollno Name
Gender | Age | Dept
DOA
Fees
1 Arun
M
24 | COMPUTER
1997-01-18 | 128
2 Ankit
21
3
Anu
F
20
HISTORY
HINDI
1998-03-24
200
1996-12-12
300
4
Bala
19
NULL
1999-07-01 | 400❘
5
Charan M
18
HINDI
1997-86-27
250
6 | Deepa
F
19
HISTORY
1997-86-27
388
7 | Dinesh | M
22 COMPUTER
1997-82-25
210
(b) Write a Query to change the fess of Student to 170 whose Roll number is 1, if the existing fess
is less than 130.
Sol:
Output(After Update):
1 | Arun
M
24❘ COMPUTER
| Fees |
1997-01-10 | 170
(c) Write a Query to add a new column Area of type varchar in table STU.
Sol:
Output:
ROLLNO
NAME
GENDER
AGE
DEPT
DOA
| FEES
AREA
1
Arun
M
24
COMPUTER
2
Ankit
M
21
HISTORY
1997-01-10
1998-03-24
120 NULL
200 NULL
3
Anu
F
20
HINDI
1996-12-12
300 NULL
Bala
M
19
NULL
1999-07-01
400 NULL
5
charan M
18
HINDI
1997-09-05
250 NULL
9
Deepa
F
19
HISTORY
1997-06-27
300 NULL
7
B
Dinesh M Usha
22
COMPUTER
1997-02-25
210 NULL
23
NULL
1997-07-31
200 NULL
(d) Write a Query to Display Name of all students whose Area Contains NULL.
Sol:
Output:
NAME
Arun
Ankit
Anu
Bala
Charan
Deepa
Dinesh
Usha
(e) Write a Query to delete Area Column from the table STU.
Sol:
Sol:
mysql> DROP TABLE STU;
Output:
AIM:
SOL COMMANDS EXERCISE - 4
To write Queries for the following Questions based on the given table:
TABLE: STOCK
Pno
Pname
Dcode
Qty
UnitPrice
StockDate
5005
Ball point pen
102
100
10
2021-03-31
5003
Gel pen premium
102
150
15
2021-01-01
5002
Pencil
101
125
4
2021-02-18
5006
Scale
101
200
6
2020-01-01
5001
Eraser
102 210
3
2020-03-19
5004
Sharpner
102 60
5
2020-12-09
5009
Gel pen classic
103 160
8
2022-01-19
TABLE: DEALERS
Dcode
Dname
101
Sakthi Stationeries
103
Classic Stationeries
102
Indian Book House
(a) To display the total Unit price of all the products whose Dcode as 102.
Sol:
Output:
SUM(UNITPRICE)
33
(b) To display details of all products in the stock table in descending order of Stock date.
Sol:
mysql> SELECT * FROM STOCK ORDER BY STOCKDATE DESC;
Output:
PNO
PNAME
| DCODE | QTY | UNITPRICE
5001❘ Eraser
102
210
3
5006 Scale
101 | 200
STOCKDATE
2022-01-19 |
2021-03-31 |
4 2021-02-18 | 2021-01-01 |
5 2028-12-09
2020-03-19
6 | 2020-01-01 |
(c) To display maximum unit price of products for each dealer individually as per dcode
from the table Stock.
Sol:
Output:
DCODE | MAX(UNITPRICE) |
102
101
103
15 | 6 |
8
(d) To display the Pname and Dname from table stock and dealers.
Sol:
Output:
PNAME
Eraser Pencil