Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 16: Compute the sum ,
subtraction, multiplication, division and
exponent by given variable.
#Age of persons is stored in l
l=[12,65,71,32,45,79,59]
c=0
for i in l:
if i>=60 and i<=90:
c=c+1
print("Number of persons having age greater than 60 and less than 90
",c)
Output:
19 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 17: Compute transpose of a
matrix.
#Age of persons is stored in l
N=4
def transpose(A):
for i in range(N):
for j in range(i+1, N):
A[i][j], A[j][i] = A[j][i], A[i][j]
A = [ [2, 2, 2, 2],
[7, 7, 7, 7],
[3, 3, 3, 3],
[5, 5, 5, 5]]
transpose(A)
print("Modified matrix is")
for i in range(N):
for j in range(N):
print(A[i][j], " ", end='')
print()
20 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
Output:
21 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 18: Perform following
operations on two matrices. 1) Addition
2) Subtraction 3) Multiplication.
A = [[1,2,3],
[4,5,6],
[7 ,8,9]]
B = [[9,8,7],
[6,5,4],
[3,2,1]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
# adding two matrices
for i in range(len(A)):
for j in range(len(A[0])):
result[i][j] = A[i][j] + B[i][j]
print("Addition of matrix")
for x in result:
print(x)
# multiply two matrices
res = [[0,0,0],
22 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
[0,0,0],
[0,0,0]]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
res[i][j] += A[i][k] * B[k][j]
print("Multiplication of matrix")
for r in res:
print(r)
# Subtracting two matrices
ress = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(A)):
for j in range(len(A[0])):
ress[i][j] = A[i][j] - B[i][j]
print("Subtraction of matrix")
for q in ress:
print(q)
23 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
Output:
24 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 19: Count occurrence of
vowels
# string of vowels
vowels = 'aeiou'
ip_str = input("Enter the words or a sentence _")
# make it suitable for caseless comparisions
ip_str = ip_str.casefold()
# make a dictionary with each vowel a key and value 0
count = {}.fromkeys(vowels,0)
# count the vowels
for char in ip_str:
if char in count:
count[char] += 1
print(count)
Output
25 | P a g e
Programming in Python Laboratory Name: Sambhav Gandhi
Subject Code: UGCA 1917 Roll Number: 2102940
PROGRAM 20: Count total number of
vowels in a word
# Python Program to Count Vowels in a String
str1 = input("Please Enter Your Own String : ")
vowels = 0
for i in str1:
if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u' or i == 'A'
or i == 'E' or i == 'I' or i == 'O' or i == 'U'):
vowels = vowels + 1
print("Total Number of Vowels in this String = ", vowels)
Output
26 | P a g e