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

PYTHON PROGRAMMING LAB ASSIGNMENTS

The document outlines the Python Programming Lab Assignments for the BCAAE301 course at the University of Engineering and Management for the June to December 2024 semester. It details course objectives and outcomes, emphasizing problem-solving using Python fundamentals, data types, and object-oriented programming. Additionally, it provides a list of assignments focusing on practical programming tasks involving lists, strings, and dictionaries.

Uploaded by

deepikadalvi18
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

PYTHON PROGRAMMING LAB ASSIGNMENTS

The document outlines the Python Programming Lab Assignments for the BCAAE301 course at the University of Engineering and Management for the June to December 2024 semester. It details course objectives and outcomes, emphasizing problem-solving using Python fundamentals, data types, and object-oriented programming. Additionally, it provides a list of assignments focusing on practical programming tasks involving lists, strings, and dictionaries.

Uploaded by

deepikadalvi18
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

University of Engineering and Management

Institute of Engineering & Management, Salt Lake Campus Institute of Engineering


& Management, New Town Campus University of Engineering & Management, Jaipur

PYTHON PROGRAMMING LAB ASSIGNMENTS


BCAAE301
June 2024-December 2024

IEM, BCA and M.Sc. Information Science Department


University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur

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:

1. To apply fundamental concepts of Python Programming for problem solving

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

MODULE 3: Strings, List, Dictionaries, Tuples

NO OF LABS REQUIRED: 4 HRS ; 2 WEEKS

LIST OF ASSIGNMENTS:
Use the given two lists for questions 1 to 9.

world_animals=['Anaconda', 'Platypus','Red panda', 'Beaver', 'Killer whale', 'Platypus', 'Camel', 'Polar


bear', 'King penguin', 'Snow leopard', 'Zebra', 'Plains bison']

world_biomes=['Tropical Rainforest','Temperate Forest','Taiga', 'Marine','Freshwater',


'Desert','Arctic Tundra','AntarticaTundra','Alpine Tundra', 'Tropical Grassland' ,' Temperate Grassland
']

1. Create a program to calculate and return the length of the given two
lists.

Answer >>> world_animals=['Anaconda', 'Platypus','Red panda', 'Beaver', 'Killer


whale', 'Platypus', 'Camel', 'Polar bear', 'King penguin', 'Snow leopard',
'Zebra', 'Plains bison']
>>> print("The length of the list world_animals is ",len(world_animals))
The length of the list world_animals is 12
>>>
>>>
>>> world_biomes=['Tropical Rainforest','Temperate Forest','Taiga',
'Marine','Freshwater', 'Desert','Arctic Tundra','AntarticaTundra','Alpine
Tundra', 'Tropical Grassland' ,' Temperate Grassland ']
>>>
>>> print("The length of the list world_biomes is ",len(world_biomes))
The length of the list world_biomes is 11
>>>
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur

2. Create a program to count up how many times an item appears in a


given list.
Given:
 Platypus in world_animals
 Taiga in world_biomes

Answer >>> world_animals=['Anaconda', 'Platypus','Red panda', 'Beaver', 'Killer


whale', 'Platypus', 'Camel', 'Polar bear', 'King penguin', 'Snow leopard',
'Zebra', 'Plains bison']
>>>
>>> world_biomes=['Tropical Rainforest','Temperate Forest','Taiga',
'Marine','Freshwater', 'Desert','Arctic Tundra','AntarticaTundra','Alpine
Tundra', 'Tropical Grassland' ,' Temperate Grassland ']
>>>
>>> print("Platypus occurence in the list of
world_animals",world_animals.count("Platypus"))
Platypus occurence in the list of world_animals 2

>>> print("Taiga occurence in the list of


world_biomes",world_biomes.count("Taiga"))
Taiga occurence in the list of world_biomes 1

3. Write a program to find the index of the first matching element for
 'Snow leopard' in the list of world_animals.

Answer >>> world_animals=['Anaconda', 'Platypus','Red panda', 'Beaver', 'Killer


whale', 'Platypus', 'Camel', 'Polar bear', 'King penguin', 'Snow leopard',
'Zebra', 'Plains bison']

>>> print("The animal Snow leopard is in the


index",world_animals.index("Snow leopard"))
The animal Snow leopard is in the index 9
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur

4. Write a program to examine a list for an item.


 Anaconda in world_animals
 Forest in world_biomes

Answer >>> world_animals=['Anaconda', 'Platypus','Red panda', 'Beaver', 'Killer


whale', 'Platypus', 'Camel', 'Polar bear', 'King penguin', 'Snow leopard',
'Zebra', 'Plains bison']
>>>
>>> world_biomes=['Tropical Rainforest','Temperate Forest','Taiga',
'Marine','Freshwater', 'Desert','Arctic Tundra','AntarticaTundra','Alpine
Tundra', 'Tropical Grassland' ,' Temperate Grassland ']
>>>
if "Anaconda" in world_animals:
print("List 'world_animals' contains element Anaconda.")
else:
print("List 'world_animals' does not contain element Anaconda.")

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.

5. Write a program to reverse the order of the world_biomes list in


Python

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

>>> world_biomes=['Tropical Rainforest','Temperate Forest','Taiga',


'Marine','Freshwater', 'Desert','Arctic Tundra','AntarticaTundra','Alpine
Tundra', 'Tropical Grassland' ,' Temperate Grassland ']
>>>
>>> print('\nOriginal List = ', world_biomes)
Original List = ['Tropical Rainforest', 'Temperate Forest', 'Taiga', 'Marine',
'Freshwater', 'Desert', 'Arctic Tundra', 'AntarticaTundra', 'Alpine Tundra',
'Tropical Grassland', ' Temperate Grassland ']
>>>
>>> world_biomes.reverse()
>>> print('\nReverse List = ', world_biomes)

Reverse List = [' Temperate Grassland ', 'Tropical Grassland', 'Alpine


Tundra', 'AntarticaTundra', 'Arctic Tundra', 'Desert', 'Freshwater', 'Marine',
'Taiga', 'Temperate Forest', 'Tropical Rainforest']
>>>

6. Write a python script that concatenates the above given two lists into
one.

Answer >>> world_animals=['Anaconda', 'Red panda', 'Beaver', 'Killer whale',


'Platypus', 'Camel', 'Polar bear', 'King penguin', 'Snow leopard', 'Zebra',
'Plains bison']
>>>
>>> world_biomes=['Tropical Rainforest','Temperate Forest','Taiga',
'Marine','Freshwater', 'Desert','Arctic Tundra','AntarticaTundra','Alpine
Tundra', 'Tropical Grassland' ,' Temperate Grassland ']
>>>
>>> print("Concatenating two lists")
Concatenating two lists
>>>
>>> print(world_animals+world_biomes)
['Anaconda', 'Red panda', 'Beaver', 'Killer whale', 'Platypus', 'Camel', 'Polar
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur

bear', 'King penguin', 'Snow leopard', 'Zebra', 'Plains bison', 'Tropical


Rainforest', 'Temperate Forest', 'Taiga', 'Marine', 'Freshwater', 'Desert',
'Arctic Tundra', 'AntarticaTundra', 'Alpine Tundra', 'Tropical Grassland', '
Temperate Grassland ']

7. Write a program to insert world_animals list at a given position.


Given:
Item is ,”Elephant”
Position is 3

Answer >>> world_animals=['Anaconda', 'Red panda', 'Beaver', 'Killer whale',


'Platypus', 'Camel', 'Polar bear', 'King penguin', 'Snow leopard', 'Zebra',
'Plains bison']
>>> world_animals.insert(3,"Elephant")
>>> print(world_animals)
['Anaconda', 'Red panda', 'Beaver', 'Elephant', 'Killer whale', 'Platypus',
'Camel', 'Polar bear', 'King penguin', 'Snow leopard', 'Zebra', 'Plains bison']

8. Create your own multiline string and show how to print it.

Answer >>> # Creating a multiline string


>>> multiline_str = """I'm learning Introduction to Strings in Python.
... Its quite interesting to implement all the problem excerises .
... It gives the complete knowledge to all Python programmers."""
>>> print("Multiline string: \n" + multiline_str)
Multiline string:
I'm learning Introduction to Strings in Python.
Its quite interesting to implement all the problem excerises .
It gives the complete knowledge to all Python programmers.

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

and the length of the string.


(Exercise1.py)

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)

C:\Users\User> python > Exercise1.py


Length of the string 47
Self :( 4 )
control :( 7 )
will :( 4 )
place :( 5 )
(a :( 2 )
man) :( 4 )
among :( 5 )
the :( 3 )
Gods; :( 5 )

>>> def splitString (str):


... for words in str.split():
... print(words, ":", "(", len(words), ")")
...
>>> string = "Self control will place (a man) among the Gods;"
>>> print(“Length of the string”, len(string))
>>> splitString(string)
Self : ( 4 )
control : ( 7 )
will : ( 4 )
place : ( 5 )
(a : ( 2 )
man) : ( 4 )
among : ( 5 )
the : ( 3 )
Gods; : ( 5 )
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur

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

countUpperAndLowerCase("Introduction to Strings in Python")

C:\Users\User> python > Exercise2.py


Upper case: 3
Lower case: 26

Answer >>> 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))
...
>>> countUpperAndLowerCase("Introduction to Strings in Python")

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)

# Python Program to Count Total Characters in a String


str1 = input("Please Enter your Own String : ")
total = 0
for i in str1:
total = total + 1
print("Total Number of Characters in this String = ", total)

C:\Users\User> python > Exercise3.py


Please Enter your Own String : hope
Total Number of Characters in this String = 4

Answer >>> str1 = input("Please Enter your Own String : ")


Please Enter your Own String : hope
>>> total = 0
>>> for i in str1:
... total = total + 1
...
>>> print("Total Number of Characters in this String = ", total)
Total Number of Characters in this String = 4

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

14. Write a program that obtains a user-supplied string. Use the


isnumeric() to determine whether or not it contains numbers.

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

16. The longest word in dictionary is


Pneumonoultramicroscopicsilicovolcanoconiosis.
Write a program that takes the longest given word in dictionary, as an
input and output the count of only the distinct characters in it.
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur

(Exercise4.py)

Answer def count_distinct_chars(string):


count = 0 #Intialise count
# examine each letter in the string, iteratively.
for ch in string:
# check for the presence of the character in the string
if string.find(ch) == string.rfind(ch):
count += 1
return count # Return the number of distinct characters
print(count_distinct_chars("Pneumonoultramicroscopicsilicovolcanoconios
is"))

C:\Users\User> python > Exercise4.py


3
5
5

Answer >>> def count_distinct_chars(string):


... count = 0 #Intialise count
... # examine each letter in the string, iteratively.
... for ch in string:
... # check for the presence of the character in the string
... if string.find(ch) == string.rfind(ch):
... count += 1
... return count # Return the number of distinct characters
...
>>>
print(count_distinct_chars("Pneumonoultramicroscopicsilicovolcanoconios
is"))
5

17. Given:
“string1=Pneumonoultramicroscopicsilicovolcanoconiosis.”
Write a Python program that, takes the given string, and returns a new
string with all the vowels stripped out.

Answer >>> string1="Pneumonoultramicroscopicsilicovolcanoconiosis"


>>>
>>> vowels = "aeiouAEIOU"
>>> new_string = ""
>>> for chr in string1:
... if chr not in vowels:
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur

... new_string+= chr


...
>>>
>>> print(new_string)
Pnmnltrmcrscpcslcvlcncnss

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

Answer >>> pulses_calories={'Chickpeas':164, 'Lentils': 116, 'Black beans': 127,


'Pinto beans': 115, 'Kidney beans': 112, 'Peas': 81}
>>> d = pulses_calories
>>> d
{'Chickpeas': 164, 'Lentils': 116, 'Black beans': 127, 'Pinto beans': 115,
'Kidney beans': 112, 'Peas': 81}

>>> print("Access Key at 'Chickpeas' location: ", d['Chickpeas']) # access


value at key ' Chickpeas'
Access Key at 'Chickpeas' location: 164
>>> print("Access Key at 'Lentils' location: ", d['Lentils']) # access value
at key 'Lentils.'
Access Key at 'Lentils' location: 116
>>> print("Access Key at 'Pinto beans' location: ", d['Pinto beans']) #
access value at key ' Pinto beans'
Access Key at 'Pinto beans' location: 115
>>> print("Access Key at 'Peas' location: ", d['Peas']) # access value at key
'Peas'
Access Key at 'Peas' location: 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).

Answer >>> n=int(input("Input a number "))


Input a number 8
University of Engineering and Management
Institute of Engineering & Management, Salt Lake Campus Institute of Engineering
& Management, New Town Campus University of Engineering & Management, Jaipur

>>> 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}

Answer >>> from collections import Counter


>>> # Initial Dictionary
>>> my_dict = {'Ether': 233, 'Bitcoin': 4854, 'Litecoin': 789, 'Gaslimit':
13325}
>>> k = Counter(my_dict)
>>> # Finding 3 highest values
>>> high = k.most_common(3)
>>>
>>> print("Dictionary with 3 highest values:")
Dictionary with 3 highest values:
>>> print("Keys: Values")
Keys: Values
>>> for i in high:
... print(i[0]," :",i[1]," ")
...
Gaslimit : 13325
Bitcoin : 4854
Litecoin : 789
>>>

21. Make a replica of the sample dictionary with the dict() function:
sampledict = {
"brand": "HP",
"model": "Pavilion",
"year": 2022
}

Answer >>> sampledict = {


... "brand": "HP",
... "model": "Pavilion",
... "year": 2022
... }
>>> dict1 = dict(sampledict)
>>> print("The original dictionary:")
The original dictionary:
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(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}
>>>

22. By writing a Python program, check if two sets have no elements in


common.

Answer >>> x = {"Ether","Bitcoin","Litecoin"}


>>> y = {"Bitcoin","Gas"}
>>> z = {"Ganache"}
>>> print("Original set elements:")
Original set elements:
>>> x
{'Ether', 'Bitcoin', 'Litecoin'}
>>> y
{'Gas', 'Bitcoin'}
>>> z
{'Ganache'}
>>> print("Confirm two given sets have no element(s) in common:")
Confirm two given sets have no element(s) in common:
>>> print("Compare x and y: " , x.isdisjoint(y))
Compare x and y: False
>>> print("Compare x and z: " , x.isdisjoint(z))
Compare x and z: True
>>> print("Compare y and z: " , y.isdisjoint(z))
Compare y and z: True
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 4:

LIST OF ASSIGNMENTS:

NO OF LABS REQUIRED:

You might also like