Python Manual-1-5 Programs
Python Manual-1-5 Programs
BPLCK105B/205B
II-SEMESTER
Prepared By:
Prof. Sahana M
Introduction to Python Programming
(Effective from the academic year 2022 -2023)
SEMESTER – II
CREDITS – 03
Sl.
Programs
No
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.
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
2
coefficient (Given N and R).
Read N numbers from the console and create a list. Develop a program to print mean, variance
3
and standard deviation with suitable messages.
Read a multi-digit number (as chars) from the console. Develop a program to print the
4
frequency of each digit with suitable message.
Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use
5 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]
Develop a program to sort the contents of a text file and write the sorted contents into a separate
6 text file. [Hint: Use string methods strip(), len(), list methods sort(), append(), and file methods
open(), readlines(), and write()].
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.
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
8
b=0. Develop a suitable program which reads two values from the console and calls a function
DivExp.
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
9
represent the complex number. Develop a program to read N (N >=2) complex numbers and to
compute the addition of N complex numbers.
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
10 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.]
COURSE OUTCOMES (COs):
On the completion of this course, the students will be able to:
CO1: Demonstrate proficiency in handling loops and creation of functions.
CO2: Identify the methods to create and manipulate lists, tuples and dictionaries.
CO3: Develop programs for string processing and file organization
CO4: Interpret the concepts of Object-Oriented Programming as used in Python.
CIE for the practical component of the Integrated Course
• On completion of every experiment/program in the laboratory, the students shall be evaluated
and marks shall be awarded on the same day. The15 marks are for conducting the experiment
and preparation of the laboratory record, the other 05 marks shall be for the test conducted at the
end of the semester.
• The CIE marks awarded in the case of the Practical component shall be based on the continuous
evaluation of the laboratory report. Each experiment report can be evaluated for 10 marks.
Marks of all experiments' write-ups are added and scaled down to 15 marks.
• The laboratory test (duration 02/03 hours) at the end of the 14" /15" week of the semester /after
completion of all the experiments (whichever is early) shall be conducted for 50 marks and
scaled down to 05 marks.
Scaled-down marks of write-up evaluations and tests added will be CIE marks for the laboratory
component of UPCC for 20 marks.
(Introduction to Python Programming) BPLCK205B
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.
# Get student details
name = input("Enter student name: ")
usn = input("Enter student USN: ")
marks1 = float(input("Enter marks in subject 1: "))
marks2 = float(input("Enter marks in subject 2: "))
marks3 = float(input("Enter marks in subject 3: "))
# Calculate total marks and percentage
total_marks = marks1 + marks2 + marks3
percentage = total_marks / 3
# Display results
print("Student details:")
print("Name:", name)
print("USN:", usn)
print("Marks in subject 1:", marks1)
print("Marks in subject 2:", marks2)
print("Marks in subject 3:", marks3)
print("Total marks:", total_marks)
print("Percentage:", percentage, "%")
if percentage >= 60:
print("Congratulations, you have passed with first division!")
elif percentage >= 45:
print("Congratulations, you have passed with second division!")
elif percentage >= 35:
print("Congratulations, you have passed with third division!")
else:
print("Sorry, you have failed the exam.")
output:
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.
# Get person's name and year of birth
name = input("Enter person's name: ")
2 a. Develop a program to generate Fibonacci sequence of length (N). Read N from the console.
# Get N from user
N = int(input("Enter the length of the Fibonacci sequence: "))
# Initialize the first two numbers in the sequence
fibonacci = [0, 1]
# Generate the sequence
for i in range(2, N):
next_fibonacci = fibonacci[i-1] + fibonacci[i-2]
fibonacci.append(next_fibonacci)
# Display the sequence
print("The Fibonacci sequence of length", N, "is:")
for num in fibonacci:
print(num, end=" ")
output:
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
# Get input from user
N = int(input("Enter the number of values to input: "))
# Create list of numbers
numbers = []
for i in range(N):
num = float(input("Enter a number: "))
numbers.append(num)
# Calculate mean
mean = sum(numbers) / N
# Calculate variance
variance = sum((x - mean) ** 2 for x in numbers) / N
# Calculate standard deviation
std_dev = math.sqrt(variance)
# Display results
print("The mean of the numbers is:", mean)
print("The variance of the numbers is:", variance)
print("The standard deviation of the numbers is:", std_dev)
output:
4. Read a multi-digit number (as chars) from the console. Develop a program to print the
frequency of each digit with suitable message.
output:
5. Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use
dictionary 16-2-2023 3 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]
# Open file and read contents
with open("sample.txt", "r") as file:
text = file.read()
# Remove punctuation and convert to lowercase
text = text.lower()
for p in [',', '.', ';', ':', '(', ')', '?', '!']:
text = text.replace(p, '')
# Split text into words and create dictionary of frequencies
words = text.split()
freq_dict = {}
for word in words:
if word in freq_dict:
freq_dict[word] += 1
else:
freq_dict[word] = 1
# Sort dictionary by frequency in descending order
sorted_freq = sorted(freq_dict.items(), key=lambda x: x[1], reverse=True)
# Display top 10 most frequent words
print("The 10 most frequent words are:")
for word, freq in sorted_freq[:10]:
print(word + ": " + str(freq))
output: