In this article Python Programming Lab Programs cover basic operations, lists, dictionaries, recursion, OOP, and file handling, making them ideal for practical examinations.
Python Programming Lab Programs
# Program to calculate area of Triangle, Rectangle, and Circle
print(“1. Area of Triangle”)
print(“2. Area of Rectangle”)
print(“3. Area of Circle”)
choice = int(input(“Enter your choice (1/2/3): “))
if choice == 1:
base = float(input(“Enter base of triangle: “))
height = float(input(“Enter height of triangle: “))
area = 0.5 * base * height
print(“Area of Triangle =”, area)
elif choice == 2:
length = float(input(“Enter length of rectangle: “))
breadth = float(input(“Enter breadth of rectangle: “))
area = length * breadth
print(“Area of Rectangle =”, area)
elif choice == 3:
radius = float(input(“Enter radius of circle: “))
area = 3.14 * radius * radius
print(“Area of Circle =”, area)
else:
print(“Invalid choice”)
# Program to find union of two lists
list1 = []
list2 = []
n1 = int(input(“Enter number of elements in first list: “))
for i in range(n1):
element = int(input(“Enter element: “))
list1.append(element)
n2 = int(input(“Enter number of elements in second list: “))
for i in range(n2):
element = int(input(“Enter element: “))
list2.append(element)
union_list = list(set(list1) | set(list2))
print(“First List:”, list1)
print(“Second List:”, list2)
print(“Union of Lists:”, union_list)
O/P:
First List: [1, 2, 3, 4]
Second List: [3, 4, 5, 6]
Union of Lists: [1, 2, 3, 4, 5, 6]
# Program to find intersection of two lists
list1 = []
list2 = []
n1 = int(input(“Enter number of elements in first list: “))
for i in range(n1):
element = int(input(“Enter element: “))
list1.append(element)
n2 = int(input(“Enter number of elements in second list: “))
for i in range(n2):
element = int(input(“Enter element: “))
list2.append(element)
intersection_list = list(set(list1) & set(list2))
print(“First List:”, list1)
print(“Second List:”, list2)
print(“Intersection of Lists:”, intersection_list)
O/P:
First List: [1, 2, 3, 4]
Second List: [3, 4, 5, 6]
Intersection of Lists: [3, 4]
# Program to remove the i-th occurrence of a given word in a list
words = []
n = int(input(“Enter number of words in the list: “))
for i in range(n):
word = input(“Enter word: “)
words.append(word)
key = input(“Enter the word to remove: “)
occ = int(input(“Enter the occurrence number to remove: “))
count = 0
for i in range(len(words)):
if words[i] == key:
count += 1
if count == occ:
del words[i]
break
print(“Updated List:”, words)
O/P:
The Enter number of words in the list: 6
Enter word: apple Enter word: banana Enter word: apple Enter word: orange Enter word: apple
Enter word: mango Enter the word to remove: apple Enter the occurrence number to remove: 2
Updated List: [‘apple’, ‘banana’, ‘orange’, ‘apple’, ‘mango’]
# Program to count occurrences of each word in a sentence
sentence = input(“Enter a sentence: “)
words = sentence.split()
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
print(“Word Occurrences:”)
for word, count in word_count.items():
print(word, “:”, count)
O/P:
Enter a sentence: python is easy and python is powerful
Word Occurrences:
python : 2
is : 2
easy : 1
and : 1
powerful : 1
# Program to check if a substring is present in a string
string = input(“Enter a string: “)
substring = input(“Enter a substring to search: “)
if substring in string:
print(“Substring is present in the given string.”)
else:
print(“Substring is not present in the given string.”)
O/P:
Enter a string: Python programming is easy
Enter a substring to search: programming
Substring is present in the given string.
# Program to map two lists into a dictionary
keys = []
values = []
n = int(input(“Enter number of elements: “))
print(“Enter keys:”)
for i in range(n):
key = input()
keys.append(key)
print(“Enter values:”)
for i in range(n):
value = input()
values.append(value)
result = dict(zip(keys, values))
print(“Mapped Dictionary:”, result)
O/P:
Enter number of elements: 3
Enter keys:
a
b
c
Enter values:
10
20
30
Mapped Dictionary: {‘a’: ’10’, ‘b’: ’20’, ‘c’: ’30’}
# Program to count frequency of words in a string using dictionary
string = input(“Enter a string: “)
words = string.split()
frequency = {}
for word in words:
if word in frequency:
frequency[word] += 1
else:
frequency[word] = 1
print(“Word Frequency:”)
for word in frequency:
print(word, “:”, frequency[word])
O/P:
Enter a string: data science is fun and data science is useful
Word Frequency:
data : 2
science : 2
is : 2
fun : 1
and : 1
useful : 1
# Program to create dictionary with first character as key
# and words starting with that character as values
words = []
n = int(input(“Enter number of words: “))
for i in range(n):
word = input(“Enter word: “)
words.append(word)
result = {}
for word in words:
key = word[0]
if key in result:
result[key].append(word)
else:
result[key] = [word]
print(“Result Dictionary:”)
print(result)
O/P:
Enter number of words: 5
Enter word: apple Enter word: ant Enter word: banana Enter word: ball Enter word: cat
Result Dictionary:
{‘a’: [‘apple’, ‘ant’], ‘b’: [‘banana’, ‘ball’], ‘c’: [‘cat’]}
# Program to find length of a list using recursion
def list_length(lst):
if lst == []:
return 0
else:
return 1 + list_length(lst[1:])
n = int(input(“Enter number of elements in the list: “))
lst = []
for i in range(n):
element = input(“Enter element: “)
lst.append(element)
length = list_length(lst)
print(“Length of the list:”, length)
O/P:
Enter number of elements in the list: 4
Enter element: 10 Enter element: 20 Enter element: 30 Enter element: 40
Length of the list: 4
# Program to compute diameter, circumference, and volume of a sphere using class
class Sphere:
def __init__(self, radius):
self.radius = radius
def diameter(self):
return 2 * self.radius
def circumference(self):
return 2 * 3.14 * self.radius
def volume(self):
return (4/3) * 3.14 * self.radius * self.radius * self.radius
r = float(input(“Enter radius of the sphere: “))
s = Sphere(r)
print(“Diameter of Sphere =”, s.diameter())
print(“Circumference of Sphere =”, s.circumference())
print(“Volume of Sphere =”, s.volume())
O/P:
Enter radius of the sphere: 7
Diameter of Sphere = 14
Circumference of Sphere = 43.96
Volume of Sphere = 1436.03
# Program to read a file and capitalize the first letter of every word
filename = input(“Enter file name: “)
with open(filename, “r”) as file:
content = file.read()
capitalized_content = content.title()
with open(filename, “w”) as file:
file.write(capitalized_content)
print(“File content updated successfully.”)
O/P:
Example
Input file content (data.txt):
python programming is very easy
file handling in python
Output file content:
Python Programming Is Very Easy
File Handling In Python
Some More:
- POP- Introduction to Programming Using ‘C’
- DS – Data structure Using C
- OOP – Object Oriented Programming
- Java Programming
- DBMS – Database Management System
- RDBMS – Relational Database Management System
- https://defineinfoloop.blogspot.com/?m=1