Python Lab Manual
Python Lab Manual
SOCIETY’S
K. L. E. INSTITUTE OF TECHNOLOGY,
Opp. Airport, Gokul, Hubballi-580 030
Phone: 0836-2232681 Website:www.kleit.ac.in
Laboratory Plan
Semester: 1 Year: 2022-23
Course objectives
Learn the syntax and semantics of the Python programming language.
Illustrate the process of structuring the data using lists, tuples
Appraise the need for working with various documents like Excel, PDF, Word and
Others.
Demonstrate the use of built-in functions to navigate the file system.
Implement the Object Oriented Programming concepts in Python.
Programming Exercises:
1. a. Develop a program to read the student details like Name, USN, and Marks in three
subjects. Display the student details, total marks and percentage with suitable messages.
b. Develop a program to read the name and year of birth of a person. Display whether the
person is asenior citizen or not.
2. a. Develop a program to generate Fibonacci sequence of length (N). Read N from the
console.
b. Write a function to calculate factorial of a number. Develop a program to compute
binomial coefficient (Given N and R).
3. Read N numbers from the console and create a list. Develop a program to print mean,
variance and standard deviation with suitable messages.
4. Read a multi-digit number (as chars) from the console. Develop a program to print the
frequency of each digit with suitable message.
5. Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use
dictionary with distinct words and their frequency of occurrences. Sort the dictionary in
the reverse order of frequency and display dictionary slice of first 10 items]
6. Develop a program to sort the contents of a text file and write the sorted contents into a
separate text file. [Hint: Use string methods strip(), len(), list methods sort(), append(), and
file methods open(),readlines(), and write()].
8. Write a function named DivExp which takes TWO parameters a, b and returns a value c
(c=a/b). Write suitable assertion for a>0 in function DivExp and raise an exception for
when b=0. Develop a suitable program which reads two values from the console and calls
a function DivExp.
9. Define a function which takes TWO objects representing complex numbers and returns
new complex number with a addition of two complex numbers. Define a suitable class
‘Complex’ to represent the complex number. Develop a program to read N (N >=2)
complex numbers and to compute the addition of N complex numbers.
10. Develop a program that uses class Student which prompts the user to enter marks in three
subjects and calculates total marks, percentage and displays the score card details. [Hint:
Use list to store the marks in three subjects and total marks. Use __init__() method to
initialize name, USN and the lists to store marks and total, Use getMarks() method to read
marks into the list, and display() method to display the score card details.]
Continuous Internal Evaluation (CIE):
The CIE marks for the theory component of the IC shall be 30 marks and for the laboratory
component 20 Marks.
1. a. Develop a program to read the student details like Name, USN, and Marks
in three subjects. Display the student details, total marks and percentage
with suitable messages.
Input:
Student Details
Enter the Student name: Ramchandra
Enter the Student usn: 2KI22CS005
Enter the Student Marks of Three Subjects
Enter Marks1: 95
Enter Marks2: 68
Enter Marks3: 87
Output
Marks1: 95
Marks2: 68
Marks3: 87
==============================
Total Marks: 250
Percentage: 83.33
==============================
b. Develop a program to read the name and year of birth of a person. Display
whether the person is a senior citizen or not.
Input:
Enter name of person : Sita
Enter Year of Birth of Sita: 1975
Output
Sita is not a Senior Citizen.
and age of Sita is 48
Input
How many terms? 6
Output
Fibonacci sequence:
0
1
1
2
3
5
Input Output
Enter the number:5 Factorial of 5 is 120
Input Output
Enter the Value of n: 10
Enter the Value of r: 5 Binomial Coefficient nCr= 252.0
Dept. of CSE Page 4 KLEIT
Introduction to Python Programming
3. Read N numbers from the console and create a list. Develop a program to
print mean, variance and standard deviation with suitable messages.
Input
enter the list size 3
enter number at index 0
22
enter number at index 1
33
enter number at index 2
55
Output
user list is [22, 33, 55]
mean of the userlist is 36.666666666666664
variance of the userlist is 188.2222222222222
standard deviation of the userlist 13.719410418171117
Input
enter the number: 12365478456258
Output
digit frequency
1 1
2 2
3 1
4 2
5 3
6 2
7 1
8 2
Input
Sample.txt
Output
hi 1
welcomw 1
to 1
python 6
programming 6
labthis 1
is 2
labwe 1
are 1
doing 1
6th 1
program 1
3
which 1
will 1
demonstrate 1
file 1
operations 1
in 1
pythonpython 1
this 1
lab 1
The top N value pairs are
{'python': 6, 'programming': 6, '': 3, 'is': 2, 'hi': 1, 'welcomw': 1, 'to': 1, 'labthis':
1, 'labwe': 1, 'are': 1}
6. Develop a program to sort the contents of a text file and write the sorted
contents into a separate text file.
[Hint: Use string methods strip(), len(), list methods sort(), append(), and
file methods open(), readlines(), and write()].
Input
Enter the File to be sorted:
kleit.txt
Enter name of the file to write sorted contents:
cse.txt
Output
kleit.zip file will be generated in Path: C:\Users\HP\kleit.zip
Input 2
Enter value of x 2
Enter value of y 0
Output 2
Invalid Operation: denominator can't be 0
Input 3
Enter value of x 9
Enter value of y 8
Output 3
Result of x / y is 1.125
Input
Enter first complex number
Enter the Real Part: 2
Enter the Imaginary Part: 3
Enter second complex number
Enter the Real Part: 3
Enter the Imaginary Part: 3
Output
........................................................................
First Complex Number: 2+3i
Second Complex Number: 3+3i
Sum of two complex numbers is 5+6i
10. Develop a program that uses class Student which prompts the user to
enter marks in three subjects and calculates total marks, percentage and
displays the score card details.
[Hint: Use list to store the marks in three subjects and total marks. Use
__init__() method to initialize name, USN and the lists to store marks
and total, Use getMarks() method to read marks into the list, and
display() method to display the score card details.]
Input
Enter name: Neha
Enter USN: 2ki00cs001
Enter marks for subject 1: 60
Enter marks for subject 2: 50
Enter marks for subject 3: 70
Output
Name: Neha
USN: 2ki00cs001
Marks: [60, 50, 70]
Total: 180
Percentage: 60.0 %