0% found this document useful (0 votes)
32 views

Python Manual-1-5 Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Python Manual-1-5 Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

BANGALORE INSTITUTE OF TECHNOLOGY

K. R. ROAD, V.V. PURAM, BANGALORE-560 004

Department of Artificial Intelligence & Machine Learning

BPLCK105B/205B

Introduction to Python Programming


Manual

II-SEMESTER

Prepared By:

Prof. Sahana M
Introduction to Python Programming
(Effective from the academic year 2022 -2023)
SEMESTER – II

Course Code BPLCK205B IA Marks 20


Number of Lecture Hours/Week 2:0:2:0 Exam Marks 50
Total Number of Lecture Hours 28 Exam Hours 03

CREDITS – 03

COURSE LEARNING OBJECTIVES (CLOs):


This course enables students to get practical experience in design and development of:
CLO1: Learn the syntax and semantics of the Python programming language.
CLO2: Illustrate the process of structuring the data using lists, tuples
CLO3: Appraise the need for working with various documents like Excel, PDF, Word and Others.
CLO4: Demonstrate the use of built-in functions to navigate the file system.
CLO5: Implement the Object-Oriented Programming concepts in Python.

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.")

Department of Artificial Intelligence & Machine Learning, BIT 1


(Introduction to Python Programming) BPLCK205B

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: ")

year_of_birth = int(input("Enter year of birth: "))


# Calculate age
current_year = 2023 # Replace with current year
age = current_year - year_of_birth
# Determine if person is a senior citizen
is_senior_citizen = (age >= 60)
# Display results
print("Name:", name)
print("Age:", age)
if is_senior_citizen:
print("This person is a senior citizen.")
else:
print("This person is not a senior citizen.")
output:

Department of Artificial Intelligence & Machine Learning, BIT 2


(Introduction to Python Programming) BPLCK205B

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:

2 b. Write a function to calculate factorial of a number. Develop a program to compute binomial


coefficient
# Define function to calculate factorial
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
# Get input from user
n = int(input("Enter the value of n: "))
k = int(input("Enter the value of k: "))
# Calculate binomial coefficient
binomial_coefficient = factorial(n) // (factorial(k) * factorial(n-k))
# Display result
print("The binomial coefficient of", n, "choose", k, "is:", binomial_coefficient)
output:

Department of Artificial Intelligence & Machine Learning, BIT 3


(Introduction to Python Programming) BPLCK205B

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:

Department of Artificial Intelligence & Machine Learning, BIT 4


(Introduction to Python Programming) BPLCK205B

4. Read a multi-digit number (as chars) from the console. Develop a program to print the
frequency of each digit with suitable message.

# Get input from user


number = input("Enter a multi-digit number: ")
# Create dictionary to store frequencies
frequency = {}
for digit in number:
if digit in frequency:
frequency[digit] += 1
else:
frequency[digit] = 1
# Display frequencies
print("Digit frequencies:")
for digit in frequency:
print(digit + ":", frequency[digit])

output:

Department of Artificial Intelligence & Machine Learning, BIT 5


(Introduction to Python Programming) BPLCK205B

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:

Department of Artificial Intelligence & Machine Learning, BIT 6

You might also like