11) The user to enter the value of n and calculate the factorial of n using recursion.
def fac(n):
if n==1:
return 1;
else:
return(n*fac(n-1))
n = int(input('Enter the number\t:'))
factorial = fac(n)
print('Factorial of ',n, ' is ', factorial)
Output
Enter the number 5
Factorial of 5 is 120
12) The user to enter the values of a and b and calculate a to the power of b, using recursion.
def power(a, b):
if b==1:
return a
else:
return (a*power(a, b-1))
a = int(input('Enter the first number\t:'))
b = int(input('Enter the second number\t:'))
p = power(a,b)
print(a, ' to the power of ',b,' is ', p)
Output
Enter the first number 3
Enter the second number 4
3 to the power of 4 is 81
13) There is an example of the use of loops in processing nested lists. What is expected output ?
Program
hb=["Programming in C#",["cloudvikas", 2015]]
rm=["SE is everything",["Obscure Publishers", 2015]]
writer=[hb, rm]
print(writer)
for i in range(len(writer)):
for j in range(len(writer[i])):
for k in range(len(writer[i][j])):
print(str(i)+" "+str(j)+" "+str(k)+" "+str(writer[i][j][k])+"\n")
print()
Output
[['Programming in C#', ['cloudvikas', 2015]], ['SE is everything', ['Obscure Publishers', 2015]]]
0 0 0 P
0 0 1 r
0 0 2 o
0 0 3 g
0 0 4 r
0 0 5 a
0 0 6 m
0 0 7 m
0 0 8 i
0 0 9 n
0 0 10 g
0 0 11
0 0 12 i
0 0 13 n
0 0 14
0 0 15 C
0 0 16 #
0 1 0 cloudvikas
0 1 1 2015
1 0 0 S
1 0 1 E
1 0 2
1 0 3 i
1 0 4 s
1 0 5
1 0 6 e
1 0 7 v
1 0 8 e
1 0 9 r
1 0 10 y
1 0 11 t
1 0 12 h
1 0 13 i
1 0 14 n
1 0 15 g
1 1 0 Obscure Publishers
1 1 1 2015
14) Write a program that calculates the mean of numbers entered by the user.
n=int(input("Enter the number of elements to be inserted: "))
a=[]
for i in range(0,n):
element=int(input("Enter element one by one: "))
a.append(element)
avg=sum(a)/n
print("Average of elements in the list",round(avg,2))
15) Write a program that calculates the mode of numbers entered by the user.
Mode : The mode is the number that occurs most often within list of numbers.
from collections import Counter
n_num = [3, 2, 1, 7, 5, 5]
n = len(n_num)
data = Counter(n_num)
get_mode = dict(data)
mode = [k for k, v in get_mode.items() if v == max(list(data.values()))]
if len(mode) == n:
get_mode = "No mode"
else:
get_mode = "Mode is / are: " + ', '.join(map(str, mode))
print(get_mode)
16) Write a program that calculates the median of numbers entered by the user.
The median is the middle number in a group of numbers.
n_num = [7, 6, 3, 4, 5]
n = len(n_num)
n_num.sort()
if n % 2 == 0:
median1 = n_num[n//2]
median2 = n_num[n//2 - 1]
median = (median1 + median2)/2
else:
median = n_num[n//2]
print("Median is: " + str(median))
17) Write a program that finds the maximum of the numbers from a given list.
Way 1:
list1.sort()
print("Largest element is:", list1[-1])
Way 2:
# printing the maximum element
print("Largest element is:", max(list1))
Way 3:
def myMax(list1):
max = list1[0]
for x in list1:
if x > max :
max = x
return max
print("Largest element is:", myMax(list1))
18) Write a function that finds the minimum of the numbers from a given list.
Way 1:
list1 = [10,1,2,4,6,8,3]
list1.sort()
print(list1)
print("smallest element is:", list1[0])
Way 2:
print("smallest element is:", min(list1))
Way3:
def myMin(list1):
min = list1[0]
for x in list1:
if x < min :
min = x
return min
print("smallest element is:", mymin(list1))
19) Write a function that finds the maximum of three numbers entered by the user.
Way 1:
def maximum(a, b, c):
if (a >= b) and (a >= c):
largest = a
elif (b >= a) and (b >= c):
largest = b
else:
largest = c
return largest
# Driven code
a = 1
b = 4
c = 2
print(maximum(a, b, c))
Way 2:
print(max(a, b, c))
20) Write a program that searches for an element from a given list.
try:
# Define a list of countrys
countryList = ['India', 'Japan',
'China']
# Take the name of the country that you want to search in the list
countryName = input("Enter a country name:")
# Search the element using index method
search_pos = int(countryList.index(countryName))
# Print found message
print("%s country is found in the list" %(countryName))
except(ValueError):
# Print not found message
print("%s country is not found in the list" %(countryName))