Professional Documents
Culture Documents
when it is called
Inbuilt
function
These are functions which are pre built in
Python and will have a special meaning inside python
ex type C lent 7 prints input
key wyd y
space
a
open
Iggy
def function name
down at the
body of end
ofnathme
In function
Indentation
cant include any
Python statements
def is used
D definition key word to define the
function
2 Space is left between the def and the function
name
Case Sensitive
Simple function
First step define a function
fl is the
functions name of
functional
these
statements
Memory
Advantages of functions
Reusability of the code
D chunks
2 when we are writing huge Python
code with the help of functions we can divide
debug it
def sum
catyfarameters
As parameters are given
printer y we do not need to
write the input statements
in the body of function
parameters
Simple parameters
default parameters
default parameters
is taken
when we pass arguments to default parameters
then the default value will be replaced by
arguments passed
default Parameter should always blow non default
parameters
Arguments are of two types
Positional arguments
Keyword arguments
arguments these arguments will be
Positional
assigned to the parameters based on the position
ex bio name age height
so if bio stys 23 it this is correct
but if we give
x here though we
bio xyz 170 23 do not get error
logic is wrong
so positional arguments might create issues when
we do not know the position of the argument
to be passed Instead better to use keyword
arguments
the best
keyword arguments are
ex def fl n 10,9 10
fll
no error
P
n 2
n
Em
i e fi se or fl se
Dictionary Kwang
so
for args
represents arg
Parameter name
def fl af here since it is a tuple
for y in up we dont mention
print y
Internally
fl i 2 3 y
sy N 1 2,3 4,5
I so here n is tuple
2
which has 5 elements
Presents Kwang
instead of as we do in Dictionary
dictionary
N keys C name Age Sen
N values pi 25 Maley
n items C name Pi Cage 25
semi male
even numbers
input n numbers
output even numbers from this
n numbers
def even n
for y in n
2 0
if 4
print y
even 1 2 3 4 5,617,8
defined
return keyword p
user
None
So we use
keyword return inside the function
def even n
L C
for y in n
f y
if 4 2 20
l append Y
f CT
for y in n
X
if Yt 22 0
stops
Program
return here and will
y
not execute
d append y the rent
commands
return syntax
ext return hi
is return none
default
it to a variable
ex
2 4 6 8
L
Yar function parameters
t
this variable can be used
in other places
or var 1 function
var 2 var 1
temporary varible assigned
a function
has the return value
ex n even
y KC 1,2 3 4 5,6 7 8
Print Cy
2 4 6,8
tuple i e hi 1,2 3 4
Practice Programs
oh
def odd on Cnp
oof fagged
L
for y in range tenens
if 4 21 0
le append
n Cy
return l
N odd In abcdefghijk
a value stores the return value
Q Create a function
Input Dynamic string
output string with odd Ondened characters upper
case and even ordered lower case
def od up e lo n
l C
for y in range Clench
0
if 44.21
l append n Cy upper c
else
l append hey hewers
Lif
s join e n abedefgh
return S S ab Derg H
print 3 23 23 format my n
y
se c mult tab 5 4
C
5 1 5
5 2 10
5 3 15
5 4 20
doc string document string
Inside afunction if we want to document what
the function is doing then we can write a
string
describing what it is doing
def bio name age height
message will come here
other Python
statements
def FIC
yield hi
generator
yield 123
yield bye
def fac
yield hi
yield 123
Yield bye
rent q
hi
rent a
123
rent E
bye
nent q
Iteration is done so we get
x a error
for y in fac
Print y
hi
123
bye
Enumerators
often when dealing with iterable objects we also
by default is zero bu
we can give our own
ext
Inder
N abedef
for y in enumerate 2100
print y
100 a
101 b
102 c
103 d
104 e
105 f
we can use enumerate C function to change the
index value temporarily and it will only function
with in a loop
ext
Ciao because it was only a
n x
temporary change Ncioe
does not exist
ex n abedefgh
index C
value C
for y in enumerate 2100
index append Yeo
value append YC
y Cooia
Ciel d
eosin
indene
too 101 107
value
a s d in
Anonymous function lambda function
syntax
n no of arguments
lambda arguments expression
only one expression
to call it
Y lambda arguments expression
t
name
yl
output
only one expression
ex 2 lambda n y ayy
2610 20
30
instead of
def fi my 2
Print mtg 2
610
or we can store the output of the lambda
function in a variable
P q lo Ze 30
PY 610
ex q lambda 2 2 10 9 20 mtg z
y default parameters
ex
fl 2
as a iterable object
map Fl 1,2 107
here paranthesis are not used as
map object
Fi Ci I
f 2 4
100
fi lo
107
map object map 71 1,2
list Cy
l 4,9 1003
they need to be
iterable object for both
neg
listcq
4,10 18
join lista
er
join list map lambda n n upperc abider'D
ABC DEF
Syntax
filter function Iterable object
False
n 1 2 3 4 5 6 7
T F T F T F T
False
Q Using map lambda values filter out odd values
based on a condition
Q Given a
string filter out vowels are i O u
ext ab cedt
C'ai E
abcd abode
ex
N 1 2 3 4
y C'a b'did
2 ab
Cdefe exceeds the length so it
is ignored
list zip x y 27
Cl d d 2 b 552,8 d d Gidi'd
iterable or not
syntan
Zip zip Citerable objects iterable object 2
unzipping
Each variable will be assigned an iterable
Object
ex N 1 2 3
y
51617182 excluded
list zip Ny
I s 2,6 3,77
Z W Zip zip In y
z
1 2 3
2 4,5 617
elements of 142
Original
P C T
for y in ltz
p append y
1 2 3,4 5,4 5 6,7
on
p d z
ex l 1 2 3 4
2 abcd
p e 2
1 2,3 4 a D d d
2 4,5 6,7
P e 23
1 2 3,4 516,73
concept of storing return address in stack memory
Memory
retardress
stack
1
2 2X I
3 3 2 1
4 4 3 2 1
5 5 473 2 1
se
def fl se
digit
since l is the last
if 2 21 multiplied
return 1
else
return N F1 N D
Calling itself
fl a
Ty fi 3
I Fica
fi CD
4 3 2 I
24
summation nth D n 2 t it to
if n 0
return O
else
return n summation n D
summation S
I summation Y
t summation 3
y summation 2
It summation i
ft summation o
5 4 3 2 t t t O
15
Advantages of Recursion
1 Recursive functions make the code look
clean and Elegant
2 A complex problem can be broken down in
1
sometimes the logic behind recursion is hard
to follow through
2 Recursive calls are expensive inefficient
as they take up a lot of memory and
time
3 Recursive functions are hard to
debug
Decorators Tricky Advanced
Syntax any
be
can
thisassigned name a function
takes as parameter
def decor fun name forename
def wrapper fun name
additional
calls the fun c name pl functionality
original function to be added
and wraps it
ext
def fl 7
Print welcome
Bye
we can use decorator
fun CC wrapping
Print Bye
return wrap C
decor fi
Hi
welcome
Bye
Q we have a function and we want to check
return C
def wrap c
Ste time times
fun CC
et time time c
return wrap c
deco 1672
2.961811