0% found this document useful (0 votes)
2 views6 pages

Python Lab Programs

PYTHON LABS

Uploaded by

srishtisvkm2006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Python Lab Programs

PYTHON LABS

Uploaded by

srishtisvkm2006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Lab Programs

Program 1: Map Two Lists into a Dictionary


keys = ["name", "age", "city"]
values = ["Srishti", 20, "Mumbai"]

result = dict(zip(keys, values))

print("Keys List:", keys)


print("Values List:", values)
print("Mapped Dictionary:", result)

Output:
Keys List: ['name', 'age', 'city']
Values List: ['Srishti', 20, 'Mumbai']
Mapped Dictionary: {'name': 'Srishti', 'age': 20, 'city': 'Mumbai'}
Program 2: Word Frequency Using Dictionary
sentence = input("Enter a sentence: ")

words = [Link]()
freq = {}

for word in words:


freq[word] = [Link](word, 0) + 1

print(freq)

Output:
Enter a sentence: python is easy and python is fun
{'python': 2, 'is': 2, 'easy': 1, 'and': 1, 'fun': 1}
Program 3: Dictionary with First Character as Key
words = ["apple", "banana", "ball", "cat", "car"]

result = {}

for word in words:


first = word[0]
if first not in result:
result[first] = []
result[first].append(word)

print(result)

Output:
{'a': ['apple'], 'b': ['banana', 'ball'], 'c': ['cat', 'car']}
Program 4: Length of List Using Recursion
def list_length(lst):
if lst == []:
return 0
return 1 + list_length(lst[1:])

numbers = [10, 20, 30, 40, 50]

print("Length of list:", list_length(numbers))

Output:
Length of list: 5
Program 5: Compute Diameter, Circumference and Volume of Sphere Using
Class
import math

class Sphere:
def __init__(self, radius):
[Link] = radius

def diameter(self):
return 2 * [Link]

def circumference(self):
return 2 * [Link] * [Link]

def volume(self):
return (4/3) * [Link] * [Link]**3

r = float(input("Enter radius: "))


s = Sphere(r)

print("Diameter =", [Link]())


print("Circumference =", [Link]())
print("Volume =", [Link]())

Output:
Enter radius: 7
Diameter = 14
Circumference = 43.98
Volume = 1436.75
Program 6: Read a File and Capitalize First Letter of Every Word
file = open("[Link]", "r")

content = [Link]()
[Link]()

capitalized_text = [Link]()

print("Original Content:")
print(content)

print("After Capitalization:")
print(capitalized_text)

Output:
Original Content:
python is a programming language

After Capitalization:
Python Is A Programming Language

You might also like