Python Programming Assignment 2
1. Python program to print the numbers from a given
number n till 0 using recursion
def number(a):
if a>=0:
print(a)
return number(a-1)
else:
return 0
a=int(input("enter no"))
number(a)
2. Python program to find the factorial
of a number using recursion
def fact(n):
if n>0:
return n*fact(n-1)
else:
return 1
n=int(input())
f=fact(n)
print(f)
3. Python program to display the sum of n numbers using
a list without using built-in functions.
n=[]
sum=0
a=int(input("how many no you want to input list"))
for i in range(0,a):
b=int(input(f"enter element {i} :"))
n.append(b)
sum=sum+n[i]
print("sum of number in list is :",sum)
4. Python program to implement linear search
def linear_Search(list1, n, key):
#Searching list1 sequentially
for i in range(0, n):
if (list1[i] == key):
return i
return -1
list1 = [1 ,3, 5, 4, 7, 9]
key = 7
n = len(list1)
res = linear_Search(list1, n, key)
if(res == -1):
print("Element not found")
else:
print("Element found at index: ", res)
5. Python program to find the odd numbers in an array
list=[]
a=int(input("how many no you want to input in list"))
for i in range(0,a):
b=int(input(f"enter element {i} :"))
list.append(b)
print("--------------------")
print(list)
for i in range(0,len(list)):
if (list[i]%2==0):
print()
else:
print("odd no",list[i])
6. Python program to find the largest number in a list without using built -in
functions
max=0
list=[1,2,4,44,34,221,34]
for i in range(0,len(list)):
for j in range(i+1,len(list)):
if list[i]>list[j]:
max=list[i]
list[i]=list[j]
list[j]=max
print(list)
print("greater no is ",list[-1])
7. Python program to insert a number to any position in a list
print("Enter 10 Elements of List: ")
nums = []
for i in range(10):
nums.insert(i, input())
print("Enter an Element to Insert at End: ")
elem = input()
nums.append(elem)
print("\nThe New List is: ")
print(nums)
8. Python program to delete an element from a list by index
n=[]
a=int(input("how many no you want to input list"))
for i in range(0,a):
b=int(input(f"enter element {i} :"))
n.append(b)
print("list is",n)
c=int(input("enter index number which number you want to delete "))
del n[c]
print("new list is",n)
9. Python program to check whether a string is palindrome or not
my_string=input("Enter string:")
if(my_string==my_string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")
10. Python program to implement matrix addition and multiplication
take a 3x3 matrix
A = [[12, 7, 3],[4, 5, 6],[7, 8, 9]]
# take a 3x4 matrix
B = [[5, 8, 1, 2],[6, 7, 3, 0],[4, 5, 9, 1]]
result = [[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0]]
# iterating by row of A
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)
11. Python program to check leap year
year =int(input("Please enter year"))
if (( year%400 == 0)or (( year%4 == 0 ) and ( year%100 != 0))):
print("%d is a Leap Year" %year)
else:
print("%d is Not the Leap Year" %year)
12. Python program to find the Nth term in a Fibonacci series using recursion
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = int(input("enter no which term series show"))
# check if the number of terms is valid
if (nterms <= 0):
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
13. Python program to print all the items in a Dictionary
dictionary = {'Novel': 'Pride and Prejudice','year': '1813','author': 'Jane Austen',
'character': 'Elizabeth Bennet'}
print(dictionary.values(),dictionary.keys())
14. Python program to implement a calculator to do basic operations
a, b = 15, 2
#displaying catalog for the user choice
print("1 - Addition
2 - Substraction
3 - Multiplication
4 - Division
5 - Floor Division
6 - Modulo")
# getting option from the user
option = int(input("Enter one option from the above list:- "))
# writing condition to perform respective operation
if option == 1:
print(f"Addition: {a + b}")
elif option == 2:
print(f"Substraction: {a - b}")
elif option == 3:
print(f"Multiplication: {a * b}")
elif option == 4:
print(f"Division: {a / b}")
elif option == 5:
print(f"Floor Division: {a // b}")
elif option == 6:
print(f"Modulo: {a % b}")
15. Write a Python program to convert a list of tuples into a dictionary .
def listtodict(A, di):
di = dict(A)
return di
# Driver Code
A = [("Adwaita", 5), ("Aadrika", 5), ("Babai", 37), ("Mona", 7), ("Sanj", 25), ("Sakya", 30)]
di = {}
print ("The Dictionary Is ::>",listtodict(A, di))
16. Replace last value of tuples in a list
l = [(10, 20, 40), (40, 50, 60), (70, 80, 90)]
print([t[:-1] + (100,) for t in l])
17. Write a Python program to remove an empty tuple(s) from a list of tuples.
def Remove(tuples):
for i in tuples:
if(len(i) == 0):
tuples.remove(i)
return tuples
# Driver Code
tuples = [(), ('ram', '15', '8'), (), ('laxman', 'sita'),
('krishna', 'akbar', '45'), ('', ''), ()]
print(Remove(tuples))
18. Write a Python program to divide two numbers using exception handling.
try:
a=int(input("enter 1 no "))
b=int(input("enter 2 no"))
c=a/b
except:
print("cannot divide by zero")
finally:
print("divide is",c)