Python Assignment 1
1. Write a Python program to rotate the value of x, y, z such that x has the
value of y, y has the value of z and z has the value of x
=> print("Enter the value of three variable ")
x = int(input("Enter the value of x : "))
y = int(input("Enter the value of y : "))
z = int(input("Enter the value of z : "))
temp = x
x=y
y=z
z = temp
print(x)
print(y)
print(z)
2.Write a Python program to check whether a given character is present in a
string or not.
=> string = input("Enter a string : ")
char = input("Enter a character to find : ")
flag = 0
for i in range(0, len(string)):
if string[i] == char:
flag = 1
if(flag == 1):
print("Found")
else:
print("Not Found")
3.Write a Python program to add two complex numbers.
=> print("Enter two complex numbers : ")
num1 = complex(input("Enter the 1st number : "))
num2 = complex(input("Enter the 1st number : "))
print("Sum = " , num1 + num2)
5. Write a Python program that prints out the decimal equivalents of 1/2,
1/3, 1/4,...., 1/10 using for loop.
=> a=[]
for i in range(2,11):
a.append(1/i)
print(a)
8. Write a Python program to find the maximum of a list of numbers.
=> list1 = []
num = int(input("Enter number of elements in list: "))
for i in range(1, num + 1):
element = int(input("Enter elements: "))
list1.append(element)
list1.sort()
print("Largest element is:", list1[num-1])
9. Write a Python program to multiply two matrices.
=> A = [[2,6, 8],
[4, 9, 5],
[10, 13, 1]]
B = [[7, 9, 11, 32],
[62, 17, 65, 21],
[29, 0, 33, 2]]
result = [[0, 0, 0, 0],
[0, 0, 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)):
result[i][j] += A[i][k] * B[k][j]
for r in result:
print(r)
10. Write a Python program to reverse all the elements in a tuple without
using the tuple function.
=> t1= (1,2,787, 8.6,"It is a tuple")
t1 = tuple(reversed(t1))
print(t1)
11. Write a Python program to check whether an element exists within a
tuple.
=>
tuple= ("suv", "muv", "mpv")
if "mpv" in tuple
print("Yes, 'mpv' is in the vehicle category tuple")
15. Write a Python program to define a function to compute GCD and LCM of
two numbers hence to
find out GCD and LCM of two numbers.
=> def compute_GCD(x, y):
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
GCD = i
return GCD
def compute_LCM(x, y):
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
LCM = greater
break
greater += 1
return LCM
num1 = 25
num2 = 20
print("The H.C.F. is", compute_GCD(num1, num2))
print("The L.C.M. is", compute_LCM(num1, num2))