Exp 6
Jagrat mohan mehta
590022800
B24
Experiment 6: Functions
1. Write a Python function to find the maximum and minimum numbers from a sequence of numbers. (Note: Do not use built-in
functions.)
2. def find_max_min(lst):
3. mx = mn = lst[0]
4. for i in lst:
5. if i > mx:
6. mx = i
7. if i < mn:
8. mn = i
9. return mx, mn
10.
11. lst = list(map(int, input().split()))
12. print(find_max_min(lst))
2. Write a Python function that takes a positive integer and returns the sum of the cube of all the positive integers smaller than the
specified number.
def cube_sum(n):
s=0
for i in range(1, n):
s += i**3
return s
n = int(input())
print(cube_sum(n))
3. Write a Python function to print 1 to n using recursion. (Note: Do not use loop)
def print_n(n):
if n == 0:
return
print_n(n-1)
print(n)
n = int(input())
print_n(n)
4. Write a recursive function to print Fibonacci series upto n terms.
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
n = int(input())
for i in range(n):
print(fib(i), end=" ")
5. Write a lambda function to find volume of cone.
import math
cone = lambda r, h: (1/3) * [Link] * r * r * h
r = float(input())
h = float(input())
print(cone(r, h))
6. Write a lambda function which gives tuple of max and min from a list.
Sample input: [10, 6, 8, 90, 12, 56]
Sample output: (90,6)
lst = list(map(int, input().split()))
f = lambda x: (max(x), min(x))
print(f(lst))
7. Write functions to explain mentioned concepts:
a. Keyword argument
b. Default argument
c. Variable length argument
def func(a, b=5, *c):
print(a, b, c)
func(1)
func(1, 2)
func(1, 2, 3, 4)
8. Write a program to check whether all the values in a dictionary are same or not using lambda function.
d = {'a':1, 'b':1, 'c':1}
res = lambda x: len(set([Link]())) == 1
print(res(d))
9. Write a program to create two lists and generate a dictionary with keys from list1 and values from list2.
l1 = list(input().split())
l2 = list(input().split())
d = dict(zip(l1, l2))
print(d)