Python Experiments
Python Experiments
Program:
Output:
Program:
Output
The area of the rectangle with length 10 and width 5 is 50
Program:
Output:
Program :
Output:
Program:
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
Output:
Program:
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:
else:
return new_lst
my_word = "geeks"
N=2
remove_nth_occurrence(my_list, my_word, N)
Output:
Program:
def count_words(sentence):
word_counts = {}
words = sentence.split()
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
return word_counts
sentence = "the quick brown fox jumps over the lazy dog"
word_counts = count_words(sentence)
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
def
is_substring_present(string,
substring):
substring = "world"
is_present =
is_substring_present(string,
substring)
in '{string}'? {is_present}")
Output:
world!'? True
EXPERIMENT NO . 07
Program:
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
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 = {}
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:
Output:
5
EXPERIMENT NO. 11
Program:
import math
class Sphere:
self.radius = radius
def diameter(self):
return 2 * self.radius
def circumference(self):
def volume(self):
s = Sphere(5)
Output:
Diameter: 10
Circumference: 31.41592653589793
Volume: 523.5987755982989
EXPERIMENT NO. 12
Program:
def capitalize_words(filename):
lines = file.readlines()
capitalized_lines = []
words = line.split()
capitalized_lines.append(capitalized_line)
return '\n'.join(capitalized_lines)
print(capitalize_words('yourfile.txt'))
Output: