1st Year Python Lab Manual
1st Year Python Lab Manual
PROGRAMMING
LAB MANUAL
Semester : I
Branch : CSE
Prepared by
P.A.COLLEGE OF ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(Approved by AICTE, Affiliated to VTU, NBA Accredited 2019-24,
Recognized by Govt. of Karnataka, Certified to ISO 9001 – 2008
P.A. EDUCATIONAL TRUST (REGD.)
Nadupadav, Near Mangalore University - 574153
2022 -2023
P.A.COLLEGE OF ENGINEERING
VISION OF THE INSTITUTE
PACE is envisaged as a center of evolution for excellence in Technological, Management and
Research Education. The institution aspires to spread universal knowledge through villages and
cities enabling expansion of human resources.
LIST OF EXPERIMENTS
Exp.
No DESCRIPTION
1. 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
1 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.
2 a. Develop a program to generate Fibonacci sequence of length (N). Read N from the
console
2b) Write a function to calculate factorial of a number. Develop a program to compute
binomial coefficient (Given N and R).
2.
3. 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. 4) Read multi-digit number as char from the console develop a program to print the
frequencyof each digit with suitable message
5. 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. 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. 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. 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. 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.]
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.
print("\nStudent Details:")
print("Name:", name)
print("USN:", usn)
print("Percentage: “, percentage)
1 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.
import datetime
# Calculate age
current_year = datetime.datetime.now().year
else:
2 a. Develop a program to generate Fibonacci sequence of length (N). Read N from the console
# Program to display the Fibonacci sequence up to n-th term
n1, n2 = 0, 1
count = 0
if nterms <= 0:
elif nterms == 1:
print(n1)
else:
print("Fibonacci sequence:")
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
2b) Write a function to calculate factorial of a number. Develop a program to compute binomial
coefficient (Given N and R).
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
# example usage
result = binomial_coefficient(n, 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.
import math
# Read value of N
mean = sum(numbers) / n
std_dev = math.sqrt(variance)
print("\nMean: {:.2f}".format(mean))
print("Variance: {:.2f}".format(variance))
4) Read multi-digit number as char from the console develop a program to print the
frequencyof each digit with suitable message
frequency = {}
if digit.isnumeric():
if digit in frequency:
frequency[digit] += 1
else:
frequency[digit] = 1
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]
import string
text = file.read()
words = text.split()
word_freq = Counter(words)
print(word, freq)
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()].
lines = file1.readlines()
lines.sort()
file2.write(line)
7) Develop a program to backing Up a given Folder (Folder in a current working directory) into a ZIP
File by using relevant modules and suitable methods.
import os
import shutil
def backup_folder(folder_path):
cwd = os.getcwd()
if not os.path.exists(backup_folder_path):
os.makedirs(backup_folder_path)
folder_name = os.path.basename(folder_path)
#example
backup_folder('testfolder')
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.
c=a/b
return c
try:
result = DivExp(a, b)
print("Result:", result)
except AssertionError as e:
print("Assertion Error:", e)
except ZeroDivisionError as e:
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
class Complex:
self.real = real
self.imag = imag
def main():
n = int(input("Enter the number of complex numbers: "))
total = Complex(0, 0)
for i in range(n):
z = Complex(real, imag)
total = add_complex(total, z)
print("The sum of {} complex numbers is: {} + {}i".format(n, total.real, total.imag))
main()
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.]
class Student:
self.name = name
self.usn = usn
self.marks = [0,0,0]
self.total = 0
self.percentage = 0.0
def getMarks(self):
def display(self):
self.total = sum(self.marks)
self.percentage = self.total/3
def main():
s = Student(name, usn)
s.getMarks()
s.display()
main()