PYTHON PROGRAMMING LAB ASSIGNMENTS
PYTHON PROGRAMMING LAB ASSIGNMENTS
COURSE OBJECTIVES:
Throughout the course, students will be expected to demonstrate their understanding of Python
Programming by being able to do each of the following:
2. To understand the fundamentals different data types, operators and conditional operators
in python.
3. To understand and apply and work with string, list, tuple and dictionary.
4. To apply logical reasoning to design programs using object oriented programing and use
python modules and packages.
COURSE OUTCOMES:
CO1: Understand the importance and basic concepts of Python Programming and be able to apply
them in problem solving
CO2: To understand basic concepts of flow control statements and concept of iterators and able to
apply flow control statements to solve problems
CO3: To get familiarize and understand basic set and dictionary operations and be able to apply the
concept for solving real word problems
CO4: Understand some basic properties of object oriented programming in python, and be able to
analyze practical examples.
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur
MODULE 1:
LIST OF ASSIGNMENTS:
NO OF LABS REQUIRED:
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur
MODULE 2:
LIST OF ASSIGNMENTS:
NO OF LABS REQUIRED:
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur
LIST OF ASSIGNMENTS:
Use the given two lists for questions 1 to 9.
1. Create a program to calculate and return the length of the given two
lists.
3. Write a program to find the index of the first matching element for
'Snow leopard' in the list of world_animals.
if "Forest" in world_biomes:
print("List 'world_biomes' contains element Forest.")
else:
print("List 'world_biomes' does not contain element Forest.")
C:/Users/INDU/AppData/Local/Programs/Python/Python310/list08feb/
prgexer/Listingprg4.py
List 'world_animals' contains element Anaconda.
List 'world_biomes' does not contain element Forest.
Answer >>>
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur
6. Write a python script that concatenates the above given two lists into
one.
8. Create your own multiline string and show how to print it.
9. Create a program that accepts a string and displays each word's length
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur
Answer #Exercise1.py
def splitString (str):
# split the string by spaces
str = str.split (' ')
# iterate words in string
for words in str:
print (words,":""(",len(words),")")
# Main code
# declare string and assign value
str = "Self control will place (a man) among the Gods;"
print("Length of the string",len(str))
# call the function
splitString(str)
10. Write a program that will count the number of lowercase and
uppercase character in a user-supplied string.
(Exercise2.py)
def countUpperAndLowerCase(sentence):
upper = 0
lower = 0
for i in sentence:
if i >='A' and i <= 'Z':
upper += 1
elif i >= 'a' and i <= 'z':
lower += 1
print("Upper case: " + str(upper))
print("Lower case: " + str(lower))
Upper case: 3
Lower case: 26
11. Create a program that will count the length of the given string without
relying on an in-built function.
Given string:
“Jack and Jill went up the hill. To fetch a pail of water. “
Answer >>> str1 = "Jack and Jill went up the hill. To fetch a pail of water."
>>> print("The string is :")
The string is :
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur
>>> print(str1)
Jack and Jill went up the hill. To fetch a pail of water.
>>> counter=0
>>> for i in str1:
... counter=counter+1
...
>>> print("The length of the string is ", counter)
The length of the string is 57
12. Write a program to elicit a string from the user and then determine the
total number of characters included in a string.
(Exercise3.py)
13. Write a program to elicit a string from the user in lower or small case
letters and then Capitalize the initial character of the string.
Answer >>> str1 = input ("Please enter the required lowercase string that needs to
be capitalised. : ")
Please enter the required lowercase string that needs to be capitalised. :
print('Input string: ', str1)
>>> x = str1.capitalize()
>>> print('Capitalised input string: ', x)
Capitalised input string: Print('input string: ', str1)
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur
Answer >>> string= input("User, enter the string to check if it is all numbers : ")
User, enter the string to check if it is all numbers : 123
>>> if string.isnumeric():
... print("The string is numeric.")
... else:
... print("The string is not numeric.")
...
The string is numeric.
>>> string= input("User, enter the string to check if it is all numbers : ")
User, enter the string to check if it is all numbers : tes
>>> if string.isnumeric():
... print("The string is numeric.")
... else:
... print("The string is not numeric.")
...
The string is not numeric.
15. Write a program that obtains a user-supplied string . Use the isdigits()
to determine whether or not it contains digits.
Answer >>> string = input("User, enter the string to check if it is all digits : ")
User, enter the string to check if it is all digits : 123
>>> if string.isdigit():
... print("The string is digits.")
... else:
... print("The string is not digits.")
...
The string is digits.
>>> string = input("User, enter the string to check if it is all digits : ")
User, enter the string to check if it is all digits : test
>>> if string.isdigit():
... print("The string is digits.")
... else:
... print("The string is not digits.")
...
The string is not digits.
>>>
(Exercise4.py)
17. Given:
“string1=Pneumonoultramicroscopicsilicovolcanoconiosis.”
Write a Python program that, takes the given string, and returns a new
string with all the vowels stripped out.
18. Construct a dictionary of pulses and their calorie counts from the table
below. Write a program that accesses the matching value at the key.
Pulses Calories
Chickpeas 164
Lentils 116
Black beans 127
Pinto beans 115
Kidney beans 112
Peas 81
19. Write a Python script to create and print a dictionary (x, x*x)
containing a number (between 1 and n) in the form (x, x).
>>> d=dict()
>>> for x in range(1,n+1):
... d[x]=x*x
...
>>> print(d)
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
20. Find the highest three values of matching dictionary keys using the
Python program.
my_dict = {'Ether': 233, 'Bitcoin': 4854, 'Litecoin': 789, 'Gaslimit':
13325}
21. Make a replica of the sample dictionary with the dict() function:
sampledict = {
"brand": "HP",
"model": "Pavilion",
"year": 2022
}
>>> print(dict1)
{'brand': 'HP', 'model': 'Pavilion', 'year': 2022}
>>> dict2=dict(dict1)
>>> print("The replicated dictionary using dict():")
The replicated dictionary using dict():
>>> print(dict2)
{'brand': 'HP', 'model': 'Pavilion', 'year': 2022}
>>>
MODULE 4:
LIST OF ASSIGNMENTS:
NO OF LABS REQUIRED: