Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

TPCT’s

College of Engineering, Dharashiv


List Of Experiment
Class:S.Y.B.Tech Sub:-Python

Sr. Title of Experiment


No

1 Program to calculate area of triangle , rectangle, circle

2 Program to find the union of two lists.


3 Program to find intersection of two files.
4 Program to remove the -ith occurrence of the given word in a list where
words repeat.
5 Program to count the occurrences of each word in a given string
sentence.
6 Program to check if a substring is present in a given string.
7 Program to map two lists into a dictionary.
8 Program to count the frequency of words appearing in a string using a
dictionary.
9 Program to create a dictionary with key as first character and value as
words starting with that character.
10 Program to find the length of a list using recursion.
11 compute the diameter, circumference, and volume of a sphere using
class.
12 Program to read a file and capitalize the first letter of every word in the
file.

Prof: A.A.Rankhamb Prof: S.A.Gaikwad


Subject Incharge HOD CSE
EXPERIMENT NO. 01

 Program:

# Function to calculate the area of a triangle


def calculate_area(base, height):
return 0.5 * base * height

# Test the function


base = 10
height = 5
area = calculate_area(base, height)
print(f"The area of the triangle with base {base} and height {height} is {area}")

 Output:

The area of the triangle with base 10 and height 5 is 25.0

 Program:

# Function to calculate the area of a rectangle


def calculate_area(length, width):
return length * width

# Test the function


length = 10
width = 5
area = calculate_area(length, width)
print(f"The area of the rectangle with length {length} and width {width} is {area}")

 Output
The area of the rectangle with length 10 and width 5 is 50

 Program:

# Function to calculate the area of a circle


import math
def calculate_area(radius):
return math.pi * radius ** 2

# Test the function


radius = 5
area = calculate_area(radius)
print(f"The area of the circle with radius {radius} is {area}")

 Output:

The area of the circle with radius 5 is 78.53981633974483


EXPERIMENT NO. 02

 Program :

# Function to find the union of two lists


def union_lists(list1, list2):
return list(set(list1) | set(list2))

# Test the function


list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
union = union_lists(list1, list2)
print(f"The union of {list1} and {list2} is {union}")

 Output:

The union of [1, 2, 3, 4, 5] and [4, 5, 6, 7, 8] is [1, 2, 3, 4, 5, 6, 7, 8]


EXPERIMENT NO. 03

 Program:

# Function to find the intersection of two lists

def intersection_lists(list1, list2):

return list(set(list1) & set(list2))

# Test the function

list1 = [1, 2, 3, 4, 5]

list2 = [4, 5, 6, 7, 8]

intersection = intersection_lists(list1, list2)

print(f"The intersection of {list1} and {list2} is {intersection}")

 Output:

The intersection of [1, 2, 3, 4, 5] and [4, 5, 6, 7, 8] is [4, 5]


EXPERIMENT NO . 04

 Program:

def remove_nth_occurrence(lst, word, N):

new_lst = []

count = 0

for i in lst:

if i == word:

count += 1

if count != N:

new_lst.append(i)

else:

new_lst.append(i)

if count == 0:

print("Item not found")

else:

print("Updated list:", new_lst)

return new_lst

my_list = ["geeks", "for", "geeks"]

my_word = "geeks"

N=2

remove_nth_occurrence(my_list, my_word, N)

 Output:

Updated list: ['geeks', 'for']


EXPERIMENT NO . 05

 Program:

# Function to count the occurrence of each word in a sentence

def count_words(sentence):

word_counts = {}

words = sentence.split()

for word in words:

if word in word_counts:

word_counts[word] += 1

else:

word_counts[word] = 1

return word_counts

# Test the function

sentence = "the quick brown fox jumps over the lazy dog"

word_counts = count_words(sentence)

print(f"The word counts in the sentence '{sentence}' are {word_counts}")

 Output:

The word counts in the sentence 'the quick brown fox jumps over the lazy dog' are {'the': 2, 'quick': 1,
'brown': 1, 'fox': 1, 'jumps': 1, 'over': 1, 'lazy': 1, 'dog': 1}
EXPERIMENT NO . 06

 Program:

# Function to check if a

substring is present in a string

def

is_substring_present(string,

substring):

return substring in string

# Test the function

string = "Hello, world!"

substring = "world"

is_present =

is_substring_present(string,

substring)

print(f"Is '{substring}' present

in '{string}'? {is_present}")

 Output:

Is 'world' present in 'Hello,

world!'? True
EXPERIMENT NO . 07

 Program:

# Function to map two lists into a dictionary

def map_lists_to_dict(keys, values):

return dict(zip(keys, values))

# Test the function

keys = ['name', 'age', 'job']

values = ['John', 30, 'Engineer']

dictionary = map_lists_to_dict(keys, values)

print(f"The dictionary mapped from the lists is {dictionary}")

 Output:

The dictionary mapped from the lists is {'name': 'John', 'age': 30, 'job': 'Engineer'}
EXPERIMENT NO . 08

 Program:
# Function to count the frequency of words in a string
def count_word_frequency(sentence):
word_counts = {}
words = sentence.split()
for word in words:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
return word_counts

# Test the function


sentence = "the quick brown fox jumps over the lazy dog"
word_counts = count_word_frequency(sentence)
print(f"The word counts in the sentence '{sentence}' are {word_counts}")

 Output:

The word counts in the sentence 'the quick brown fox jumps over the lazy dog' are {'the': 2,
'quick': 1, 'brown': 1, 'fox': 1, 'jumps': 1, 'over': 1, 'lazy': 1, 'dog': 1}
EXPERIMENT NO . 09

 Program:

#Program to create a dictionary with key as first character and value as words starting with that character.

string_input = '''GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well
thought and well explained computer science and programming articles, quizzes etc.'''
words = string_input.split()
dictionary = {}

for word in words:


first_char = word[0].lower() # Make it case-insensitive
if first_char not in dictionary:
dictionary[first_char] = []
if word not in dictionary[first_char]:
dictionary[first_char].append(word)

print(dictionary)

 Output:

{'g': ['GeeksforGeeks', 'geeks.'], 'i': ['is', 'It'], 'a': ['a', 'and', 'articles,'], 'c': ['Computer', 'contains', 'computer'],
's': ['Science', 'science'], 'p': ['portal', 'programming'], 'f': ['for'], 'w': ['well', 'written,'], 't': ['thought'], 'e':
['explained', 'etc.'], 'q': ['quizzes']}
EXPERIMENT NO. 10

 Program:

#length of list using recusion


def length(lst):
if not lst:
return 0
else:
return 1 + length(lst[1:])

# Test the function


print(length([1, 2, 3, 4, 5])) # Output: 5

 Output:
5
EXPERIMENT NO. 11

 Program:

import math

class Sphere:

def __init__(self, radius):

self.radius = radius

def diameter(self):

return 2 * self.radius

def circumference(self):

return 2 * math.pi * self.radius

def volume(self):

return 4/3 * math.pi * self.radius**3

# Create a sphere with radius 5

s = Sphere(5)

print("Diameter:", s.diameter()) # Output: 10

print("Circumference:", s.circumference()) # Output: 31.41592653589793

print("Volume:", s.volume()) # Output: 523.5987755982989

 Output:

Diameter: 10
Circumference: 31.41592653589793
Volume: 523.5987755982989
EXPERIMENT NO. 12
 Program:

def capitalize_words(filename):

with open(filename, 'r') as file:

lines = file.readlines()

capitalized_lines = []

for line in lines:

words = line.split()

capitalized_words = [word.capitalize() for word in words]

capitalized_line = ' '.join(capitalized_words)

capitalized_lines.append(capitalized_line)

return '\n'.join(capitalized_lines)

# Test the function

print(capitalize_words('yourfile.txt'))

 Output:

Traceback (most recent call last):


File "/home/main.py", line 15, in <module>
print(capitalize_words('yourfile.txt'))
File "/home/main.py", line 2, in capitalize_words
with open(filename, 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'yourfile.txt'

You might also like