Exercise 1
In [30]: def table(num): #for loop concept
for i in range(1,11):
print(num, "*",i, "=", num* i)
def lst(): #list concept
lst_1= []
n = int(input("Enter number of elements"))
for i in range(n):
element=input("Enter elements")
lst_1.append(element)
return lst_1
def dict(): #dictionary concept
dictionary = {}
n = int(input("Enter number of key-value pairs: "))
for i in range(n):
key = input("Enter key: ")
value = input("Enter value: ")
dictionary[key] = value
return dictionary
In [8]: table(5)
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
In [20]: lst()
Out[20]: ['1', '2', '3']
In [22]: dict()
Out[22]: {'Name': 'Taha', 'Roll No': '21B-039-CS'}
Exercise 2
In [90]: def reverse(strg):
if len(strg) == 0:
return
var = strg[0]
reverse(strg[1:])
print(var, end='')
In [91]: reverse("Taha")
ahaT
Exercise 3
In [42]: nums=[23,5,1,2,3,25,22]
In [43]: def minmax(nums):
min_num = 1000000
max_num = 0
for num in nums:
if num>max_num:
max_num=num
if num<min_num:
min_num=num
print(tuple([min_num,max_num]))
(1, 25)
Exercise 4
In [55]: import numpy as np
def mulp_inverse(lst, row=0, col=0):
if row == len(lst):
return lst
if col == len(lst[row]):
return mulp_inverse(lst, row + 1, 0)
if lst[row][col] != 0:
lst[row][col] = 1 / lst[row][col]
return mulp_inverse(lst, row, col + 1)
In [57]: input_list = [[5, 2], [4, 1]]
result = mulp_inverse_recursive(input_list)
print(result)
[[0.2, 0.5], [0.25, 1.0]]
Exercise 5
In [265… lst=[1,2,3,3,4,5]
unique_lst=[]
In [266… for numbers in lst:
if numbers not in unique_lst:
unique_lst.append(numbers)
print(unique_lst)
[1, 2, 3, 4, 5]
Exercise 6
In [23]: def palindrome(s):
if len(s) <= 1:
return True
if s[0] != s[-1]:
return False
return palindrome(s[1:-1])
In [27]: palindrome("racecar")
Out[27]: True
In [28]: palindrome("TAHA")
Out[28]: False