Name: Parth Class: Roll No:
Gosavi D9A 21
EXPERIMENT NO. 3
AIM: Write python programs to define functions, control statements, lambda
function.
Q1. Write a function in python ‘primeUpto’ which finds out all prime numbers
upto given number.
Input:
def primeUpto(a,b):
for number in range
(a,b): if number>1:
for i in range
(2,number): if
(number%i)==0:
break
else:
print(number)
lower=eval(input("Enter the starting
value: ")) upper=eval(input("Enter the
ending value: "))
prime=primeUpto(lower,upper+1)
Output:
Q2. Write a program in python to find the factorial of a number using recursive
function.
Input:
def factorial(number):
if number==0 |
number==1: return 1
else:
return number*factorial(number-1)
fact=eval(input("Enter number to find
factoial of: ")) print("Factorial of the number
is: ",factorial(fact))
Output:
Name: Parth Class: Roll No:
Gosavi D9A 21
Q3. A list rotation consists of taking the first element and moving it to the
end. For instance,if we rotate the list [1,2,3,4,5], we get [2,3,4,5,1]. If we
rotate it again, we get [3,4,5,1,2]. Write a Python function rotatelist(l,k) that
takes a list l and a positive integer k and returns the list l after k rotations.
If k is not positive, your function should return l unchanged.
Input:
test_list = [1,2,3,4,5]
print ("Original list:
"+str(test_list)) test_list =
test_list[1:]+test_list[:1]
print ("List after left rotate by 1: "
+str(test_list)) test_list = test_list[-
1:]+test_list[:-1]
test_list = test_list[2:]+test_list[:2]
print ("List after left rotate by 2: "
+str(test_list)) test_list = test_list[-
2:]+test_list[:-2]
test_list = test_list[3:]+test_list[:3]
print ("List after left rotate by 3: "
+str(test_list)) test_list = test_list[-
3:]+test_list[:-3]
Output:
Q4. Write a python program to use lambda function as a trippler function.
Input:
tripler=lambda x:x*3
num=int(input("Enter a
number: "))
result=tripler(num)
print("Triple of",num,"is",result)
Output:
CONCLUSION: In this experiment, we have learnt how functions, recursive
functions, control statements and lambda function work and their
applications.