You are on page 1of 10

User Defined Functions:

Strings and Tuples are IMMUtable Objects :: can’t Modify

Lists and Dictionaries are Mutable Objects : CAN Modify structure

#sample program to present list as mutable and tuple is immutable


def overwrite(l,t):

l[2] = 'sai'

print('changes list is',l)

try:

t[2] = 'sai'

print('changes tuple is',t)

except TypeError:

print('Tuple cannot be updated ')

l=list(x for x in range(int(input('enter the range '))))

print(l)

t=tuple(x for x in range(int(input('enter the range '))))

print(t)

overwrite(l,t)
Default Parameters
#Sample program using default parameters
def add(a=3,b=2):

c=a+b

print(c)

a=int(input('enter the value of a '))

b=int(input('enter the value of b '))

add()
Error: Non-default arguments cannot follow default arguments
def add(a=3,b):

c=a+b

print(c)

a=int(input('enter the value of a '))

b=int(input('enter the value of b '))

add()
#sample program of one parameter receiving multiple
arguments
def add(*a):

tot=0

for i in range(len(a)):

tot=tot+a[i]

print('the result is',tot)

add(111,111,111)
Debugging of the above program for Local and global Variables

a(111,111,111) has become a local variable not a global variable along with tot
and i
Recursive Functions: Calling a function in itself is called Recursive functions
#sample program on finding the factorial of given number using
Recursive functions
def fact(a):

if(a!=0):

return a*fact(a-1)

else:

return 1
n=int(input('enter the value of n '))

k=fact(n)

print('the factorial value is',k)


#sample program to find power of a value using recursive
functions

INNER FUNCTIONS
INNER FUNCTIONS USING DEBUGGER
NESTED FUNCTIONS
NESTED FUNCTIONS USING DEBUGGER
Multiple Return Of Values
Sending default parameters to User Defined Function's
Using Default Arguments(y=None)

#program to print Tuple and Dictionary


def f1(x, y, z=3, *p, **k):

print (x, y, z)

print (p) # p as Tuple

print (k) # k as dictionary

print( f1(1,2,43,22, 6,5.7, ['sai',25], d=4, e=1, t=2))


#program to print fibonacci series using Recursive Functions
def fib(n,a,b):

if n==1:

return 'end'

else:

a,b=b,a+b

return str(b)+ ' '+ fib(n-1,a,b)

n=int(input('enter the value of n '))

a=int(input('enter thevalue of a '))

b=int(input('enter the value of b '))

k=fib(n,a,b)

print('Fibonacci Series', a,b,k)

#sample program to print even numbers using lambda functions

ls=[x for x in range(11)]

k = list(filter(lambda x:x%2==0,ls))

print(k)

You might also like