Python Lab Manual
Python Lab Manual
Version 1.0 0 0 1 1
Pre-requisites/Exposure Basic principles of programming language
Co-requisites
COURSE OUTLINE
7. Lab. Exercise 7
Functions, lambda function, recursion
8. Lab. Exercise 8
File handling and Exception Handling
9. Lab. Exercise 9
Object Oriented Programming
10 Lab Exercise 10 Numpy, Pandas and Matplotlib
1. Declare these variables (x, y and z) as integers. Assign a value of 9 to x, Assign a value of
7 to y, perform addition, multiplication, division and subtraction on these two variables
and Print out the result.
2. Write a Program where the radius is taken as input to compute the area of a circle.
3. Write a Python program to solve (x+y)*(x+y)
Test data : x = 4 , y = 3
Expected output: 49
4. Write a program to compute the length of the hypotenuse (c) of a right triangle using
Pythagoras theorem.
5. Write a program to find simple interest.
6. Write a program to find area of triangle when length of sides are given.
7. Write a program to convert given seconds into hours, minutes and remaining seconds.
8. Write a program to swap two numbers without taking additional variable.
9. Write a program to find sum of first n natural numbers.
10. Write a program to print truth table for bitwise operators( & , | and ^ operators)
11. Write a program to find left shift and right shift values of a given number.
12. Using membership operator find whether a given number is in sequence (10,20,56,78,89)
13. Using membership operator find whether a given character is in a string.
Sample Gradesheet
Name: Rohit Sharma
Roll Number: R17234512 ROLL NO: 50005673
Sem: 1 Course: B.Tech. CSE AI&ML
1. Write a program to count and display the number of capital letters in a given string.
2. Count total number of vowels in a given string.
3. Input a sentence and print words in separate lines.
4. WAP to enter a string and a substring. You have to print the number of times that the
substring occurs in the given string. String traversal will take place from left to right, not
from right to left.
Sample Input
ABCDCDC
CDC
Sample Output
2
5. Given a string containing both upper and lower case alphabets. Write a Python program to
count the number of occurrences of each alphabet (case insensitive) and display the same.
Sample Input
ABaBCbGc
Sample Output
2A
3B
2C
1G
6. Program to count number of unique words in a given sentence using sets.
7. Create 2 sets s1 and s2 of n fruits each by taking input from user and find:
a) Fruits which are in both sets s1 and s2
b) Fruits only in s1 but not in s2
c) Count of all fruits from s1 and s2
8. Take two sets and apply various set operations on them :
S1 = {Red ,yellow, orange , blue }
S2 = {violet, blue , purple}
Practical No 6: Lists, tuples, dictionary
1. Scan n values in range 0-3 and print the number of times each value has occurred.
2. Create a tuple to store n numeric values and find average of all values.
3. WAP to input a list of scores for N students in a list data type. Find the score of the runner-up
and print the output.
Sample Input
N=5
Scores= 2 3 6 6 5
Sample output
5
Note: Given list is [2, 3, 6, 6, 5]. The maximum score is 6, second maximum is 5. Hence, we
print 5 as the runner-up score.
4. Create a dictionary of n persons where key is name and value is city.
a) Display all names
b) Display all city names
c) Display student name and city of all students.
d) Count number of students in each city.
5. Store details of n movies in a dictionary by taking input from the user. Each movie must store
details like name, year, director name, production cost, collection made (earning) & perform
the following :-
a) print all movie details
b) display name of movies released before 2015
c) print movies that made a profit.
d) print movies directed by a particular director.
Practical no 7: Functions
1. Write a Python function to find the maximum and minimum numbers from a sequence of
numbers. (Note: Do not use built-in functions.)
2. Write a Python function that takes a positive integer and returns the sum of the cube of all
the positive integers smaller than the specified number.
3. Write a Python function to print 1 to n using recursion. (Note: Do not use loop)
4. Write a lambda function which gives tuple of max and min from a list.
Sample input: [10, 6, 8, 90, 12, 56]
Sample output: (90,6)
5. Write functions to explain mentioned concepts:
a. Keyword argument
b. Default argument
c. Variable length argument
Q5. Write a Pandas program to get the first 3 rows of a given DataFrame.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew',
'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Expected Output:
First three rows of the data frame:
attempts name qualify score
a 1 Anastasia yes 12.5
b 3 Dima no 9.0
c 2 Katherine yes 16.5
Q6. Write a Pandas program to find and replace the missing values in a given DataFrame which
do not have any valuable information.
Q7. Create a program to demonstrate different visual forms using Matplotlib.