3&4 Units Python Programs
3&4 Units Python Programs
Aim: Write a Python program to add each element of list x with list y using nested loops.
Program:
x = [1, 2, 3]
y = [4, 5, 6]
result = []
for i in range(len(x)):
sum_value = x[i] + y[i]
result.append(sum_value)
print("Resultant list after addition:", result)
Output: Resultant list after addition: [5, 7, 9]
Aim: Write a Python program to print the index at which a particular value exists. If the value
exists at multiple locations in the list, print all the indices and count the number of times that
value is repeated in the list.
Program:
my_list = [1, 2, 3, 2, 4, 2, 5]
value = 2
indices = []
count = 0
for i in range(len(my_list)):
if my_list[i] == value:
indices.append(i)
count += 1
print("Value", value, "exists at indices:", indices)
print("Count of", value, "in the list is:", count)
Output:
Value 2 exists at indices: [1, 3, 5]
Count of 2 in the list is: 3
Aim: Write a python program applying all the list methods ('append', 'clear', 'copy', 'count',
'extend', 'index', 'insert','pop', 'remove', 'reverse', 'sort') on the given list.
List = [100,’a’,’b’,102,2.3,4.5].
Aim: Write a Python program to add each element of x list with each element of y list using
loops.
Program:
x = [1, 2]
y = [3, 4]
result = []
for i in x:
for j in y:
result.append(i + j)
print("Resultant list after addition:", result)
Output:
Resultant list after addition: [4, 5, 5, 6]
Aim: Write a Python program to add each element of x list with each element of y list using list
comprehension.
Program:
x=[1,3]
y=[4,5]
Aim: Write a Python program to perform addition, subtraction, and multiplication of matrices.
Program:
X = [[1, 2], [3, 4]]
Y = [[5, 6], [7, 8]]
# You can try this in normal loops but for practice of list comprehension we tried on list
comprehensions
# Addition
result_add = [[X[i][j] + Y[i][j] for j in range(len(X[0]))] for i in range(len(X))]
print("Matrix Addition:", result_add)
# Subtraction
result_sub = [[X[i][j] - Y[i][j] for j in range(len(X[0]))] for i in range(len(X))]
print("Matrix Subtraction:", result_sub)
# Multiplication
result_mul = [[sum(X[i][k] * Y[k][j] for k in range(len(X[0]))) for j in range(len(Y[0]))] for i in
range(len(X))]
print("Matrix Multiplication:", result_mul)
Output:
Matrix Addition: [[6, 8], [10, 12]]
Matrix Subtraction: [[-4, -4], [-4, -4]]
Matrix Multiplication: [[19, 22], [43, 50]]
Aim: Write a Python program to count the number of vowels in a string without using control
flow.
Program:
string = "Hello World"
vowels = 'aeiouAEIOU'
count=0
for i in string:
if i in vowels:
count+=1
print("Number of vowels:", count)
Output:
Number of vowels: 3
Aim: Write a Python program to add a new key-value pair to an existing dictionary.
Program:
my_dict = {"name": "John", "age": 22}
print("Before dictionary update:",my_dict)
my_dict["address"] = "123 Street"
my_dict["college"]="NRI"
print("Updated dictionary:", my_dict)
Output:
Before dictionary update: {'name': 'John', 'age': 22}
Updated dictionary: {'name': 'John', 'age': 22, 'address': '123 Street', 'college': 'NRI'}
Aim: Write a Python program to sum all the items in a given dictionary.
Program:
my_dict = {"a": 10, "b": 20, "c": 30}
total=0
for i in my_dict.values():
total+=i
print("Sum of all items in the dictionary:", total)
Output:
Sum of all items in the dictionary: 60
Aim: Write a Python program that reads a string from the user and creates a dictionary with the
key as word length and value as the count of words of that length.
Program:
string = "A fat cat is on the mat"
words = string.split()
print("String after splitiing:",words)
print("length of splitted string:",len(words))
length_dict = {}
Aim: Write a Python program to print each line of a file in reverse order.
Program:
def print_reverse(file_name):
with open("C:\\Users\\GOPAL\\Desktop\\file.txt", 'r') as f:
lines = f.readlines()
for line in reversed(lines):
print(line.strip()[::-1])
# Usage
print_reverse('file.txt')
Output: UOY ERA WOH DLROW OLLEH IH
Aim: Write a function lines_count() that reads lines from a text file named 'zen.txt' and displays
the lines that begin with any vowel. Assume the file contains the following text and already
exists on the computer's disk:
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Program:
def lines_count():
vowels = 'AEIOUaeiou'
with open('C:\\Users\\GOPAL\\Desktop\\zen.txt', 'r') as f:
lines = f.readlines()
for line in lines:
if line[0] in vowels:
print(line.strip())
# Usage
lines_count()
Output:
Explicit is better than implicit.
Output:
Area of the circle is: 78.53981633974483
perimeter of the circle: 31.41592653589793
Output:
Account Number: 123456
Name: John Doe
Balance: 1235.0