Python Lab Manual Dbatu Vivek
Python Lab Manual Dbatu Vivek
PYTHON PROGRAMMING
LABORATORY MANUAL
NAME OF STUDENT
CLASS/ SEM./BATCH
PYTHON PROGRAMMING
LABORATORY MANUAL
PRN. NO.
EXAM. NO.
LABORATORY CODE
1. Students should report to the concerned laboratory as per the time table.
2. Wash your hands before entering the computer lab.
3. Keep your bags in rack.
4. Remove your shoes and keep it in shoe stand.
5. Turn computer monitors off when asked by your teacher.
6. Do not go on banned websites.
7. No food or drinks near the keyboard.
8. Only use your assigned computer and workstation.
9. Do not change the settings on the computer.
10. Ask permission to download.
11. Ask permission to print documents
12. Save your work often.
13. If you are the last class of the day, please POWER DOWN all computers
and monitors.
SHRIRAM INSTITUTE OF ENGINEERING AND
TECHNOLOGY (POLY), PANIV
Department of Computer Engineering
CERTIFICATE
Miss/Mr.
PROGRAMING
Date:
INDEX
Ex. Pg. Given Submission
Title of Experiment Marks Sign.
No. No. Date Date
Program to calculate area of
1 triangle, rectangle, circle 24 24
Fundamental Knowledge
Identify/Formulate/Evaluate/Solution
of Problem
Oral/Written/Graphical Form
Accurate Documentation
Regular Attendance
GIVEN DATE
SUBMISSION DATE
SIGN. OF STUDENT
TOTAL
ATTENDANCE PRESENTATION UNDERSTANDING
MARKS
02 02 06 10
SIGN. OF FACULTY
REMARKS
EXPERIMENT NO. 01
1.1 AIM: -
1.2 THEORY:-
We will create a python program to calculate the areas of different shapes. We will
take user input for different parameters of different shapes to calculate shapes. We
will be using if-elif-else loop for the purpose of calculating. In this program, We will
ask the user to input the shape’s name. If it exists in our program then we will
proceed to find the entered shape’s area according to their respective formulas. If
that shape doesn’t exist then we will print “Sorry! We cannot find this shape.”
message on the screen.
def calculate_area(name):
else:
print("Sorry! This shape is not available")
# driver code
if __name__ == "__main__" :
# function calling
calculate_area(shape_name)
1.4 CONCLUSION:-
QUESTIONS:-
Q1) What is Python? List some popular applications of Python in the world of
technology.
GIVEN DATE
SUBMISSION DATE
SIGN. OF STUDENT
TOTAL
ATTENDANCE PRESENTATION UNDERSTANDING
MARKS
02 02 06 10
SIGN. OF FACULTY
REMARKS
EXPERIMENT NO. 02
2.1 AIM: -
2.2 PRE-REQUISITE:-
1. Knowledge of Union.
2. Knowledge of Lists in Python.
2.3 THEORY:-
Lists are used to store multiple items in a single variable. Lists are one of 4
built-in data types in Python used to store collections of data. List items are
indexed and you can access them by referring to the index number. Union of a
list means, we must take all the elements from list A and list B (there can be
more than two lists) and put them inside a single new list. There are various
orders in which we can combine the lists. For e.g., we can maintain the
repetition and order or remove the repeated elements in the final list and so
on. To get rid of all the repetitive elements from the initial list, we use the set()
function on both the lists, individually.
Then we add them using the “+” operator and pass as a new list.
2.4 Program:-
# Driver Code
lst1 = [23, 15, 2, 14, 14, 16, 20 ,52]
lst2 = [2, 48, 15, 12, 26, 32, 47, 54]
print(Union(lst1, lst2))
# Driver Code
lst1 = [23, 15, 2, 14, 14, 16, 20 ,52]
lst2 = [2, 48, 15, 12, 26, 32, 47, 54]
print(Union(lst1, lst2))
2.5 CONCLUSION:-
QUESTION:
GIVEN DATE
SUBMISSION DATE
SIGN. OF STUDENT
TOTAL
ATTENDANCE PRESENTATION UNDERSTANDING
MARKS
02 02 06 10
SIGN. OF FACULTY
REMARKS
EXPERIMENT NO. 03
3.1 AIM:
3.2 Theory:-
3.1 Program:-
Take user input for the following program:
Input :
lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9]
lst2 = [9, 4, 5, 36, 47, 26, 10, 45, 87]
Output :
[9, 10, 4, 5]
Input :
lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69]
lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26]
Output :
[9, 11, 26, 28]
# Driver Code
lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9]
lst2 = [9, 4, 5, 36, 47, 26, 10, 45, 87]
print(intersection(lst1, lst2))
3.2 CONCLUSION:-
QUESTIONS:-
1. What is intersection?
GIVEN DATE
SUBMISSION DATE
SIGN. OF STUDENT
TOTAL
ATTENDANCE PRESENTATION UNDERSTANDING
MARKS
02 02 06 10
SIGN. OF FACULTY
REMARKS
EXPERIMENT NO. 04
4.1 AIM: -
Program to count the occurrences of each word in a given string sentence.
4.2 PRE-REQUISITE:-
1. Knowledge of String.
4.3 THEORY:-
# Split the input string 'str' into a list of words using spaces as separators store it in the
'words' list.
words = str.split()
# Call the word_count function with an input sentence and print the results.
print( word_count('the quick brown fox jumps over the lazy dog.'))
4.5 CONCLUSION:-
QUESTIONS:-
GIVEN DATE
SUBMISSION DATE
SIGN. OF STUDENT
TOTAL
ATTENDANCE PRESENTATION UNDERSTANDING
MARKS
02 02 06 10
SIGN. OF FACULTY
REMARKS
EXPERIMENT NO. 05
5.1 AIM: -
5.2 THEORY:-
The program uses a dictionary to efficiently count the frequency of each word.
In a dictionary, words serve as keys, and their frequencies serve as values.on
whitespace by default. It facilitates the processing of individual words in the
input string. The for loop iterates through each word in the list. The split()
method is crucial for breaking the input sentence into individual words.
Without splitting, the program would treat the entire sentence as a single
entity. The for loop iterates through each word in the list, allowing the program
to process words one by one and update their frequencies in the dictionary.
The if-else statement checks whether a word is already in the dictionary. If it is,
the count is incremented; if not, the word is added to the dictionary with a
count of 1. The final output is a dictionary containing words as keys and their
respective frequencies as values, providing a clear representation of the word
distribution in the input sentence.
5.3 PROGRAM CODE:-
def word_frequency_count(sentence):
# Initialize an empty dictionary to store word frequencies.
word_count = {}
return word_count
# Example usage:
input_sentence = "the quick brown fox jumps over the lazy dog"
result = word_frequency_count(input_sentence)
print(result)
5.4 CONCLUSION:-
QUESTIONS:-
Q3) What does the conditional if-else statement inside the loop check?
Q4) What would happen if you didn't split the sentence into words?.
SHRIRAM INSTITUTE OF ENGINEERING AND
TECHNOLOGY (POLY), PANIV
Department of Computer Engineering
GIVEN DATE
SUBMISSION DATE
SIGN. OF STUDENT
TOTAL
ATTENDANCE PRESENTATION UNDERSTANDING
MARKS
02 02 06 10
SIGN. OF FACULTY
REMARKS
EXPERIMENT NO. 06
6.1 AIM: -
6.2 THEORY:-
def find_length_recursive(lst):
# Base case: If the list is empty, return 0.
if not lst:
return 0
# Recursive case: Return 1 plus the length of the rest of the list.
else:
return 1 + find_length_recursive(lst[1:])
# Example usage:
my_list = [1, 2, 3, 4, 5]
result = find_length_recursive(my_list)
print(result)
6.2 CONCLUSION:
QUESTIONS:-
Q1) How does the function reduce the problem in each recursive call?
GIVEN DATE
SUBMISSION DATE
SIGN. OF STUDENT
TOTAL
ATTENDANCE PRESENTATION UNDERSTANDING
MARKS
02 02 06 10
SIGN. OF FACULTY
REMARKS
EXPERIMENT NO. 07
7.1 AIM: -
7.2 THEORY:-
A sphere is a perfectly round geometrical object in three-dimensional space that is
the surface of a completely round ball. he radius of a sphere is the distance from
the center of the sphere to any point on its surface. It is constant for a given sphere.
The diameter of a sphere is the distance straight through the center from one point
on the surface to another point on the opposite side. It is twice the radius. The
circumference of a sphere is the distance around its outer edge. The volume of a
sphere is the amount of space enclosed by the sphere. We prompt the user to input
the radius of the sphere. We create an instance of the Sphere class with the
provided radius. We print out the computed diameter, circumference, and volume
of the sphere using the methods of the Sphere class.
Mathematical formulas:
Diameter: D=2r
Circumference: C=2πr
Volume: V=4/3 πr^3
7.3 PROGRAM CODE:-
import math
class Sphere:
def __init__(self, radius):
self.radius = radius
def diameter(self):
return 2 * self.radius
def circumference(self):
return 2 * math.pi * self.radius
def volume(self):
return (4/3) * math.pi * (self.radius ** 3)
# Example usage:
radius = float(input("Enter the radius of the sphere: "))
sphere = Sphere(radius)
print("Diameter of the sphere:", sphere.diameter())
print("Circumference of the sphere:", sphere.circumference())
print("Volume of the sphere:", sphere.volume())
7.4 CONCLUSION:-
QUESTIONS:-
Q3) How would you handle invalid input (e.g., negative radius) in this code?
SHRIRAM INSTITUTE OF ENGINEERING AND
TECHNOLOGY (POLY), PANIV
Department of Computer Engineering
EXPERIMENT NO. 08 Program to read a file and capitalize the first letter of
every word in the file
GIVEN DATE
SUBMISSION DATE
SIGN. OF STUDENT
TOTAL
ATTENDANCE PRESENTATION UNDERSTANDING
MARKS
02 02 06 10
SIGN. OF FACULTY
REMARKS
EXPERIMENT NO. 08
8.1 AIM:-
Program to read a file and capitalize the first letter of every word in the file
8.2 THEORY:-
In Python, the open() function is used to open files. It takes two arguments: the
file path and the mode in which to open the file (e.g., 'r' for reading, 'w' for
writing, 'a' for appending). Reading from a file is done using the read() method,
and writing to a file is done using the write() method. Python provides various
methods for string manipulation. In this code, we use the split() method to split
the content of the file into words, and the capitalize() method to capitalize the
first letter of each word. Generator expressions are similar to list
comprehensions but use parentheses instead of square brackets. They generate
values on-the-fly instead of constructing a full list in memory, which can be more
memory-efficient, especially for large datasets.
def capitalize_first_letter(file_path):
with open(file_path, 'r') as file:
content = file.read()
# Example usage:
file_path = input("Enter the file path: ")
capitalize_first_letter(file_path)
print("First letter of every word capitalized in the file.")
8.4 CONCLUSION:-
QUESTIONS:-
Q1) How does the open() function work in Python? What are its parameters?
Q2) Explain the difference between reading mode ('r'), writing mode ('w'), and
appending mode ('a') in file handling.
Q3) Describe the difference between a list comprehension and a generator expression .
Q4) How does the capitalize() method work in Python strings? What does it do?
SHRIRAM INSTITUTE OF ENGINEERING AND
TECHNOLOGY (POLY), PANIV
Department of Computer Engineering
GIVEN DATE
SUBMISSION DATE
SIGN. OF STUDENT
TOTAL
ATTENDANCE PRESENTATION UNDERSTANDING
MARKS
02 02 06 10
SIGN. OF FACULTY
REMARKS
EXPERIMENT NO.9
9.1 AIM:-
Program to remove the “i” th occurrence of given word in a list where words repeat.
9.2 THEORY:-
if count == i:
word_list.pop(index - 1)
# Example usage:
words = ['apple', 'banana', 'apple', 'orange', 'apple', 'grape']
word_to_remove = 'apple'
occurrence_to_remove = 2
QUESTIONS:-
Q1) Why do we use a while loop instead of a for loop in this scenario??
Q2) What happens if the given word does not exist in the list or if the occurrence specified
exceeds the actual occurrences in the list?.
Q3) What are the advantages and disadvantages of using the pop() method to remove
EXPERIMENT NO. 10 Program to create a dictionary with key as first character and
value as words starting with that character
GIVEN DATE
SUBMISSION DATE
SIGN. OF STUDENT
TOTAL
ATTENDANCE PRESENTATION UNDERSTANDING
MARKS
02 02 06 10
SIGN. OF FACULTY
REMARKS
EXPERIMENT NO. 10
10.1 AIM:-
Program to create a dictionary with key as first character and value as words
starting with that Character
10.2 THEORY:-
def create_word_dictionary(words):
word_dict = {}
for word in words:
if word[0] in word_dict:
word_dict[word[0]].append(word)
else:
word_dict[word[0]] = [word]
return word_dict
# Example usage:
word_list = ['apple', 'banana', 'cat', 'dog', 'elephant', 'fish', 'gorilla']
result_dict = create_word_dictionary(word_list)
print("Dictionary with words starting with first character as key:")
print(result_dict)
10.4 CONCLUSION:-
QUESTIONS:-
4. How would you handle cases where words with different cases (e.g.,
uppercase and lowercase) have the same first character?