MGM’s
Jawaharlal Nehru Engineering College
Laboratory Manual
Python Programming
For
Third Year Students
Dept: Master of Computer Application
FOREWORD
It is my great pleasure to present this laboratory manual for Third year students for the
subject of Python Programming. As a student, many of you may be wondering with some of
the questions in your mind regarding the subject and exactly what has been tried is to answer
through this manual.
As you may be aware that MGM has already been awarded with ISO 9000 certification and it
is our endure to technically equip our students taking the advantage of the procedural aspects
of ISO 9000 Certification.
Faculty members are also advised that covering these aspects in initial stage itself, will
greatly relived them in future as much of the load will be taken care by the enthusiasm
energies of the students once they are conceptually clear.
Dr. H.H.Shinde
Principal
LABORATORY MANUAL CONTENTS
This manual is intended for the Third year students of MCA in the subject of Python
Programming. This manual typically contains practical/Lab Sessions related Python
Programming covering various aspects related the subject to enhanced understanding.
Students are advised to thoroughly go through this manual rather than only topics mentioned
in the syllabus as practical aspects are the key to understanding and conceptual visualization
of theoretical aspects covered in the books.
Good Luck for your Enjoyable Laboratory Sessions
Prof. A.R.Tungar Ms. Sujata S.Magare
Head Assistant Professor
Department of MCA Department of MCA
DOs and DON’T DOs in Laboratory:
1. Do not handle any equipment before reading the instructions/Instruction manuals
2. Read carefully the power ratings of the equipment before it is switched on whether ratings
230 V/50 Hz or 115V/60 Hz. For Indian equipments, the power ratings are normally
230V/50Hz. If you have equipment with 115/60 Hz ratings, do not insert power plug, as
our normal supply is 230V/50 Hz, which will damage the equipment.
3. Observe type of sockets of equipment power to avoid mechanical damage
4. Do not forcefully place connectors to avoid the damage
5. Strictly observe the instructions given by the teacher/Lab Instructor
Instruction for Laboratory Teachers:
1. Submission related to whatever lab work has been completed should be done during the
next lab session. The immediate arrangements for printouts related to submission on the
day of practical assignments.
2. Students should be taught for taking the printouts under the observation of lab teacher.
3. The promptness of submission should be encouraged by way of marking and evaluation
patterns that will benefit the sincere students.
Mahatma Gandhi Mission’s
Jawaharlal Nehru Engineering College, Aurangabad.
Subject: Python Programming
List of Experiments
Exp No. Title
1 Program to demonstrate basic data type in python
2 Program to demonstrate operators in python
3 A cashier has currency notes of denominations 10, 50, and 100.If the amount to
be withdrawn is input through the keyboard using input() function in hundreds,
find the total number of currency notes of each denomination the cashier will
have to give to the withdrawer
4 Program to demonstrate list and tuple in python
5 Write a program in Python, A library charges a fine for every book returned late.
For first 5 days the fine is 50 paisa, for 6-10 days fine is one rupee and above 10
days fine is 5 rupees. If you return the book after 30 days your membership will
be cancelled. Write a program to accept the number of days the member is late to
return the book and display the fine or the appropriate message
6 Write a program to calculate overtime pay of 10 employees. Overtime is paid at
the rate of Rs.12.00 per hour for every hour worked above 40 hours. Assume that
employee do not work for fractional part of an hour.
7 Two numbers are entered through the keyboard; write a program to find the value
of one number raised to the power of another.
8 Write a function that receives marks received by a student in 3 subjects and
returns the average and percentage of these marks. Call this function from main()
and print the result in main
9 Write a program to read a file and display its contents
10 Write a program to demonstrate database connectivity in python
PRACTICAL: 1
Aim: Program to demonstrate basic data type in python
Program: Program to print type of given variable
a=5
print(a, "is of type", type(a))
a = 2.0
print(a, "is of type", type(a))
a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))
Output:
5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is complex number? True
Program: ii) Program to print Dictionary items
d = {1:'value','key':2}
print(type(d))
print("d[1] = ", d[1]);
print("d['key'] = ", d['key']);
Output:
<class 'dict'>
d[1] = value
d['key'] = 2
PRACTICAL: 2
Aim: Program to demonstrate operators in python
Program:
i) Program to perform addition of two numbers
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = float(num1) + float(num2)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Output:
The sum of 1.5 and 6.3 is 7.8
ii) Program to Solve Quadratic Equation
import math
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter c: '))
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
sol1 = (-b-math.sqrt(d))/(2*a)
sol2 = (-b+math.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
Output:
Enter a: 1
Enter b: 5
Enter c: 6
The solution are -3.0 and -2.0
PRACTICAL: 3
Aim: A cashier has currency notes of denominations 10, 50, and 100.If the amount to be
withdrawn is input through the keyboard using input() function in hundreds, find the total
number of currency notes of each denomination the cashier will have to give to the
withdrawer
Program:
amt=int(input('Enter amount : '))
h=amt/100
f=(amt%100)/50
t=(((amt%100)%50)/10)
print('100 notes : ',int(h))
print('50 notes : ',int(f))
print('20 notes : ',int(t))
Output:
Enter amount : 480
100 notes : 4
50 notes : 1
20 notes : 3
PRACTICAL :4
Aim: Program to demonstrate list and tuple in python
Program:
i) Program for python list
my_list = ['p','r','o','b','e']
print(my_list[0])
print(my_list[2])
print(my_list[4])
n_list = ["Happy", [2,0,1,5]] # Nested List
print(n_list[0][1]) # Nested indexing
print(n_list[1][3])
Output:
p
o
e
a
5
ii) Program for python Tuple
my_tuple = ('a','p','p','l','e',)
print(my_tuple.count('p'))
print(my_tuple.index('l'))
#Membership Test
print('a' in my_tuple)
print('b' in my_tuple)
print('g' not in my_tuple)
Output:
2
3
True
False
True
PRACTICAL: 5
Aim: Write a program in Python, A library charges a fine for every book returned late. For
first 5 days the fine is 50 paisa, for 6-10 days fine is one rupee and above 10 days fine is 5
rupees. If you return the book after 30 days your membership will be cancelled. Write a
program to accept the number of days the member is late to return the book and display the
fine or the appropriate message
Program:
d=int(input("enter Number of days : "))
fine=0
if(d<=5):
fine=d*0.50
print("Fine : ",float(fine))
elif(d>5 and d<=10):
i=d-5
fine=(i*1)+(5*0.5)
print("Fine : ",float(fine))
elif(d>10 and d<=30):
i=d-10
fine=(i*5)+(5*0.5)+(5*1)
print("Fine : ",float(fine))
else:
i=d-10
fine=(i*5)+(5*0.5)+(5*1)
print("Your Membership is calcelled")
print("Fine : ",float(fine))
Output:
enter Number of days : 14
Fine : 27.5
PRACTICAL: 6
Aim: Write a program to calculate overtime pay of 10 employees. Overtime is paid at the rate
of Rs.12.00 per hour for every hour worked above 40 hours. Assume that employee do not
work for fractional part of an hour
Program:
ovpay=0
sum=0
for i in range(1,11):
print("Enter Working Hours of Emp ",i,":")
h=int(input())
if(h>40):
extra=h-40
ovpay=extra*12
print("Over time pay of emp ",i," is ",ovpay)
sum=sum+ovpay
else:
print("No Overtime Pay")
print("Total Overtime Pay of all employees : ", sum)
Output:
Enter Working Hours of Emp 1 : 43
Over time pay of emp 1 is 36
Enter Working Hours of Emp 2 : 44
Over time pay of emp 2 is 48
Enter Working Hours of Emp 3 : 41
Over time pay of emp 3 is 12
Enter Working Hours of Emp 4 : 40
No Overtime Pay
Enter Working Hours of Emp 5 : 42
Over time pay of emp 5 is 24
Enter Working Hours of Emp 6 :44
Over time pay of emp 6 is 48
Enter Working Hours of Emp 7 : 56
Over time pay of emp 7 is 192
Enter Working Hours of Emp 8 : 43
Over time pay of emp 8 is 36
Enter Working Hours of Emp 9 : 44
Over time pay of emp 9 is 48
Enter Working Hours of Emp 10 : 40
No Overtime Pay
Total Overtime Pay of all employees : 444
PRACTICAL: 7
Aim: Two numbers are entered through the keyboard; write a program to find the value of
one number raised to the power of another.
Program:
m=int(input('Enter First value: '))
n =int(input('Enter Second value: '))
p=m**n
print('{0} Raise to {1} = {2}'.format(m,n,p))
Output:
Enter First value: 4
Enter Second value: 7
4 Raise to 7 = 16384
PRACTICAL: 8
Aim: Write a function that receives marks received by a student in 3 subjects and returns the
average and percentage of these marks. Call this function from main() and print the result in
main
Program:
def AP(m1,m2,m3):
tot=m1+m2+m3
avg=float(tot/3)
per=float((tot/150)*100)
print("Average=%0.2f Percentage=%0.2f "%(avg,per))
def main():
i=int(input("Enter sub1 marks out of 50 : "))
j=int(input("Enter sub2 marks out of 50 : "))
k=int(input("Enter sub3 marks out of 50 : "))
AP(i,j,k)
main()
Output:
Enter sub1 marks out of 50 : 45
Enter sub2 marks out of 50 : 36
Enter sub3 marks out of 50 : 47
Average=42.66 Percentage=85.33
PRACTICAL: 9
Aim: Write a program to read a file and display its contents
Program:
f=open('myfile.txt','w')
f.write('My first file')
f.close()
f=open('myfile.txt','a')
f.write('\nWelcome Every one')
f.write('\nAppend Mode')
f.close()
f=open('myfile.txt','r')
print(f.read())
f.close()
Output:
My first file
Welcome Every one
Append Mode
PRACTICAL: 10
Aim: Write a program to demonstrate database connectivity in python
Program:
import mysql.connector
conn=
mysql.connector.connect(host='localhost',user='root',passwd='',database='db1')
cursor = conn.cursor()
cursor.execute("insert into books values (101,'Python')")
print("record inserted successfully")
cursor.execute("select * from books")
result=cursor.fetchall()
print(result)
Output:
record inserted successfully
(101,’python’)