C h ap t er 01 Fun c ti o n s N e w s y llabus 2 2 4 - 2 5
F unction Int r oduction A fun cti o n is a p r og r ammi ng bloc k of c odes w hich is u sed t o per f orm a sin g le, r ela t ed ta s k . I t on l y r u ns w h e n it is c alle d . W e c a n pa s s da ta, kn o wn as pa r am e t er s , in t o a fun cti o n . A fun cti o n c a n r eturn dat a as a r esult . W e h a v e al r eady used so m e p ython bui l t in functi o ns li k e p r i nt() , e t c a nd fun ct i ons de f ined in modul e li k e mat h.p o w (),e t c . Bu t w e c a n also c r ea t e our o wn f u n ction s . Thes e fun c tions a r e c a lled u s e r - de f ined func t ion s .
A d v anta g es o f U sing function s : P r o g ram d e v e lo p me n t made ea s y an d f a s t : Wo r k can be d i v id e d amo n g proj e ct members th u s implement a tio n ca n b e compl e t ed f a s t . P r o g ram t e st ing b e com es ea s y : Ea s y to loc a t e an d isola t e a f a ult y func t ion for furth e r inve stiga t ion Cod e sh a r ing b e com es p o ss i bl e : A func t ion may b e us ed l a t e r b y ma n y o t he r prog r ams thi s me a ns tha t a p y tho n progr a mmer can u s e func t ion w r i t te n by oth e rs , in st e ad of st a r ti n g ove r fro m scratc h . Cod e r e -usabi lity incre as e s : A func t ion can b e us ed to ke e p awa y fro m r e w r itin g th e s a me blo c k of cod es w h ich we are go i ng us e t w o o r m o r e loc a ti o ns in a prog r am. This is e s pe cia l l y us e fu l if th e cod e invol v ed is lo n g or complic a t e d .
A d v anta g es o f U sing function s : I nc r eas e s pro g r am r e a dabili t y : The l ength of t he sou r c e pro gr am c an be r e du c ed b y u s ing/callin g fu n ctio n s a t a p propria t e p lac e s so pro g ram b e come mo r e r ead a bl e . Functi o n f a c il it a t e s proc e du r al abs tracti o n : O n c e a func t ion is w r itte n, prog r amme r w oul d ha v e to know to invoke a func t ion on l y , no t its coding. Functi o n s f a ci l it a t e th e f a ct o r ing of cod e : A func t ion can b e cal led in o t h er func t ion an d so on …
T ype s o f function s : 1 .Buil t - in func t ion s 2 . Function s defi n ed in mo d ule 3 . Use r d e fi n ed func t ion s
1). Buil t-in F unction s : F u n c t i o ns w hi c h a r e a lr ea d y w r i t t en o r de f i n e d i n p ytho n. A s these f u n ct i o ns a r e al r ea d y de f i n e d so w e d o not need t o d e f i ne these f u n c tion s . Bel o w a r e s o me b u i lt- i n f u n ct io n s o f P y tho n . F u n ct i on name De scr ipt i on le n() l i st() max() mi n() open ( ) pr i nt() st r () sum() type() tuple() I t r eturns the len g th o f an obj ect/ v al u e . I t r eturns a l i st. I t i s use d t o r eturn maxim u m v al u e f r om a seq uen c e ( l i st,set s ) e t c. I t i s use d t o r eturn mi n im u m v al u e f r om a seq uen c e ( l i st,set s ) e t c. I t i s use d t o open a f i l e . I t i s use d t o pr i n t sta t ement . I t i s use d t o r eturn str i ng object / v al u e . I t i s use d t o sum the v al ues i n side seq uen c e . I t i s use d t o r eturn the type o f obj ect . I t i s use d t o r eturn a tupl e .
2 ). F unction s de f ine d in modul e : A modu l e is a fil e consistin g o f Pyth o n cod e . A modu l e ca n d e fine functions , classe s and va r iables. Save thi s cod e in a fil e named mymodul e.py de f gree t ing(name ) : p r in t ("Hell o , " + name) Im p or t th e mo d ul e named mymodule, and cal l th e gree t ing func tio n : impo r t mymodule mymodul e.gre e t in g (“India")
3). U ser de f ine d fun ctio n : Func t io n s tha t w e defi ne ou r selve s to do c ertain s p e cific tas k a re r efer r e d a s use r- defi n e d function s b ecaus e t h e s e a r e not alr ea d y availabl e .
C r eating & callin g a F unction (user de f ined) /Fl o w of e x ecution A fun ct i on is de f ined us i ng t he p ytho n . E . g . p r og r am is g i v en bel o w . de f k e y w o r d in de f m y_ o w n_fu n ctio n (): p r int(" H el lo f r om a f un c tion") #p r og r am sta r t he r e. pr i nt("hello be f o r e call in g a function") m y_ o w n_fun c tion() # fu nc tion c al lin g . n o w fu nc tion c o d es w il l b e e x ecu t ed pr i nt("hello af t er call in g a function") S av e the ab o v e sou r c e c ode i n p ython f ile and e x e c u t e it # F unctio n bl oc k / d e f init i on/c r eation
V ariabl e ’ s S c ope in function The r e a r e th r ee types of v ar i a b les with the v ie w of s c ope. L ocal v ar i a b le – a cc es sibl e o n l y i n side the f u n ct io n al b lo c k w he r e i t is decla r ed. Gl o b al v ar i a b le – v ar i a b le w hi c h is a cc es sibl e among w hole p r og r am us ing g lobal k e y w o r d. N on loc a l v ariable – a cc essible i n nest i ng of f u n ct i on s , us i ng n o n l o c al k e y w o r d. L ocal v ar i a b le p r og r am: d ef fun( ) : s = " I l o v e I n d ia!" p r i n t(s) s = " I l o v e W o r l d !" fun() p r i n t(s) Output: I l o v e Ind i a! I l o v e W o r ld! Gl o b al v ar i a b le p r og r am: d ef fun( ) : g lo b al s # a c c essi n g/ makin g g lo b al v ariable f or fun() p r i n t(s) s = "I l o v e I nd ia!“ # c h angi n g g lo b al v ariabl e ’ s v alue p rint( s) s = " I l o v e w o r l d !" fun() p r i n t(s) Output: I l o v e w o r ld! I l o v e Ind i a! I l o v e Ind i a! Guess me! Guess me!
V ariabl e ’ s S c ope in function # F in d the outpu t of bel o w p r og r am de f fu n (x, y) : g lo b al a a = 10 x, y = y ,x b = 20 b = 30 c = 3 pr i nt ( a, b ,x, y) a, b , x , y = 1, 2 , 3,4 fu n (50, 10 0) print(a, b , x , y )
# F i n d th e o u t p u t of bel o w p r og r am de f fu n (x, y) : g lobal a a = 10 x, y = y ,x b = 20 b = 30 c = 30 p ri n t(a, b ,x, y ) a, b , x , y = 1, 2 , 3 ,4 fu n (50, 100) p ri n t(a, b , x , y) V ariabl e ’ s S c ope in function
V ariabl e ’ s S c ope in function Gl o ba l v ar i ab l es i n n es t ed fu n ct i on O U T P U T : Before calling fun2 : 100 Inside fun2 x= 200 After calling fun2 : 100 x in main: 200 Guess Me!
V ariabl e ’ s S c ope in function N on local v ariab l e OU T P U T : Inside fun2 x= 200 Be f o r e call in g fun2: 100 Af t er call in g fun2: 200 x i n main : 50 Guess Me!
F unction P a r am e t ers / A r gume nts P assing and r eturn v alue Thes e a r e sp eci f ie d af t er the fun c ti o n name, ins i d e the pa r e nthe s e s . M ultip le pa r ame t e r s a r e s ep a r a t ed b y c o m m a. Th e f o l l o w i n g e x ample has a funct i o n w i t h t w o p a r am e t ers x an d y . W hen th e f u n cti o n i s calle d , w e p ass t w o v alue s , w hich i s use d i nside the fu n cti o n t o s u m u p the v a l ue s an d s t o r e i n z an d then r eturn the r esult ( z ) : de f sum(x,y): #x, y a r e f ormal a r guments z=x+y r eturn z # r et u r n the v alue/ r esult x,y=4,5 r=su m (x,y) #x, y a r e actual a r guments p ri n t(r) N o t e :- 1 . F uncti o n P r o t ot yp e i s de c la r ati o n o f f uncti o n w i th name, a r gume n t a n d r e tu r n ty p e. 2. A f o r ma l p a r ame t e r , i .e. a p a r ame t e r , i s in the fun c t i on de f in i t i o n . A n actual p a r ame t e r , i .e. a n a r gum e nt, i s in a funct i on call .
F unction F u n ction A r gume nts F u n ct i o n s can be cal l ed us i ng f ol l o wi n g types of f ormal a r g u ments − R e q u i r ed a r g um e n ts / P osi t ional pa r a m e t er - a r g um e n ts p assed in c or r ect p ositional o r d er K e y w o r d a r g um e n ts - the cal ler identi f i e s t h e a r g um e n ts b y the p a r ame t er n ame Default a r g um e n ts - that ass um es a d efa ult v alue i f a v alue is n ot p ro vided t o a r g u. V ariable-le n gth a r g um e n ts – p ass multipl e v alues with si n g le a r g um e n t n ame. # R equ i r ed a r g u ments de f squa r e(x ) : z=x*x r eturn z r =s qua r e() pr i nt(r) #In ab o v e f u n c t i on squ a r e() w e h av e t o de f i n i t e l y nee d t o pass some v al u e t o a r g u men t x. # K e y w o r d a r g u ments de f f u n( name, a g e ): "T h is p ri nts a pas sed i n f o i n t o th is f u n ct io n " p ri n t (" N ame: " , name ) p ri n t ("A g e " , a g e) r etur n; # N o w y ou ca n ca l l pri n t i n f o f u n c t i on f u n( a g e= 1 5, name=" moha k " ) # v al u e 1 5 and moha k is b eing pas sed t o r ele v ant a r g u men t b ased on k e y w o r d use d f or them.
F unction #D e fault a r gume nts / #D e fault P a r am e t er de f sum(x=3,y = 4 ) : z=x+y r eturn z r=su m () p ri n t(r) r=su m (x=4) p rint(r) r=su m (y= 4 5) p ri n t(r) #default v alue of x and y i s being use d w hen i t i s n o t p assed # V ari a bl e length a r guments de f sum( * v artu p le ): s=0 f or v ar i n v artupl e : s = s+in t ( v a r ) r eturn s; r=su m ( 7 0, 60, 5 ) p rint(r) r=su m ( 4 ,5) p rint(r) #n o w the ab o v e functi o n sum() can sum n number of v alues
L amda P y thon L am b da A lam b d a fun c t i on i s a small an o n ym o u s function w hich can ta k e a n y n umbe r of a r gument s , b u t ca n o n l y h a v e o n e e x p r ession. helpful when you want to pass a function as an argument Syntax : lambda arguments: expression E. g . x = l a m b d a a, b : a * b pr i nt(x(5 , 6)) OUT P U T : 30
M utable/immuta ble p r opert i e s of dat a o b ject s w/r function E v e ry thi n g in P y thon is an object, an d e v e r y obj e cts in P y thon c an b e ei th er mu t abl e o r immu t abl e . Sin c e e v e r ythin g in P y thon is a n Obje c t, e v e r y v ariable hol d s a n obje c t inst a n c e . When a n o b j e c t is i n itia t ed, it is assig ned a uniqu e o b jec t i d. I ts t ype is de f ine d at runt i m e a n d o n c e s et ca n ne v e r cha n g e, h o w e v er its sta t e ca n be chan g ed if it is mutabl e . M eans a m ut a ble o b j e c t ca n b e c han g ed af t er it is c r ea t ed, and an immu t ab l e o b jec t ca n ’ t . M ut a ble o b ject s : list, dic t , set, b y t e ar r a y Immutable o b j e ct s : in t , f loat, c ompl ex, f r o z en set , b y t es st r in g , tup l e,
M ut a ble/immu t ab l e p r opert i e s of dat a o b ject s w/r function H o w ob j e c ts a r e passed t o F u n ctions # P ass b y r e f e r en c e de f upda t eL i s t ( list1): pr i nt( id(list1)) l i st1 += [1 ] pr i nt( id(list1)) n = [50 , 60] pr i nt( i d( n)) u pda t e L i st( n ) pr i nt( n ) pr i nt( i d( n)) OU T P UT 3 41 2 2 928 3 41 2 2 928 3 41 2 2 928 [50 , 60, 10] 3 41 2 2 928 #In a b o v e func t i o n li s t 1 an obj e c t i s b e in g p a ss ed and i t s c o n t en t s a r e changin g becaus e i t i s mutable tha t ’ s w h y it is beh a ving li k e p a s s b y r e f e r en c e Visit : p ython . m y k v s . i n f or r egu l ar upda t es # P ass b y v al u e de f upda t e N umbe r (n): pr i nt( i d( n)) n + = 10 pr i nt( i d( n)) b = 5 pr i nt( id(b)) u pda t e N u mbe r (b) pr i nt(b) pr i nt( id(b)) OU T P UT 16 9 104 6 4 16 9 104 6 4 16 9 104 22 4 5 16 9 104 6 4 # In ab o v e fu nc tion v alue of v aria bl e b is n ot bei n g c h an g ed because it is immutable that ’ s wh y it is b eh a ving l i k e p ass b y v alue
F un c tio n s using lib r ar i es Math e mat i ca l functions: Math e m a tical functions a r e a v ailable un d er mat h module . To us e mathe mat i ca l f u n c t i ons u n de r th i s mo d ule , w e h a v e to impor t the mo d ul e us i ng impor t mat h. F or e . g . T o us e sq rt () function w e h a v e t o wri t e sta t ements li k e g i v en be l o w . impor t math r = mat h . sq rt ( 4 ) print(r) OU T P UT : 2 .0 Visit : p ython . m y k v s . i n f or r egu l ar upda t es
F un c tio n s usin g lib r aries F un c tio n s a v aila b le i n P ytho n M ath M o d ule Visit : p ython . m y k v s . i n f or r egu l ar upda t es
F unction s us i n g lib r ar i es ( S y s t em de f ine d function) St r in g functions: St r in g functions a r e a v ailable i n p ytho n standa r d mo d ule.Th e se a r e a lw a y s a v ailble t o use. F or e. g . capi t ali z e() function C o n v er ts the f i r st cha r ac t er of str i n g t o uppe r cas e. s="i l o v e p r og r amming" r= s .c a pitalize() pr i nt(r) OU T P U T : I l o v e p r og r amming Visit : p ython . m y k v s . i n f or r egu l ar upda t es
F unction s us i n g lib r ar i es St r in g functions: Visit : p ython . m y k v s . i n f or r egu l ar upda t es M ethod D escrip t ion cap i t a l i z e() cas e f old() c en t er() c oun t () en c ode() ends w ith() f ind() f orm a t () index() C o n v e r ts the f irs t cha r ac t er t o uppe r case C o n v e r ts st r i n g i n t o l o w er case R eturns a c en t e r ed st r i n g R eturns the n u mbe r of t i me s a spec i f i ed v al u e o c c u rs i n a str i ng R eturns an en c oded v e r sion of the st r i n g R eturns tr u e i f the st r i n g ends with the speci f ie d v al u e S ea r che s the st r i n g f or a speci f ie d v al u e and r eturns the posit i on of w he r e i t w as f o u nd F ormats speci f ie d v al u es i n a st r i n g S ea r che s the st r i n g f or a speci f ie d v al u e and r eturns the posit i on of w he r e i t w as f o u nd
F unctions us i ng lib r ar i e s Stri n g functi o ns: Visit : p ython . m y k v s . i n f or r egu l ar upda t es M ethod D escrip t ion is a lnu m () is a lpha() i sdec i m al() isdigit() isidenti f ie r() is l o w er() isnu m eri c () isprint a b l e() is s pa c e() is t it l e() isuppe r() join() l j u st () l o w er() l s tri p() part ition() R eturns T r u e i f all cha r ac t e r s i n the st r i n g a r e alpha nume r ic R eturns T r u e i f all cha r ac t e r s i n the st r i n g a r e i n the alphabet R eturns T r u e i f all cha r ac t ers i n the str i ng a r e dec i mals R eturns T r u e i f all cha r ac t e r s i n the st r i n g a r e d i g i ts R eturns T r u e i f the st r i n g i s an ident i f ier R eturns T r u e i f all cha r ac t e r s i n the st r i n g a r e l o w er case R eturns T r u e i f all cha r ac t e r s i n the st r i n g a r e n u me r ic R eturns T r u e i f all cha r ac t e r s i n the st r i n g a r e pr i n table R eturns T r u e i f all cha r ac t e r s i n the st r i n g a r e w hi t espa c es R eturns T r u e i f the st r i n g f ol l o ws the r ul es of a tit l e R eturns T r u e i f all cha r ac t e r s i n the st r i n g a r e uppe r case J o i ns the elements of an i t e r a b le t o the end of the st r i n g R eturns a left j u sti f i ed v ersi o n of the str i ng C o n v e r ts a st r i n g i n t o l o w er case R eturns a left tr i m v e r sion of the st r i n g R eturns a tup l e w he r e the st r i n g i s par t ed i n t o th r ee parts
F unction s us i n g lib r ar i es St r in g functions: Visit : p ython . m y k v s . i n f or r egu l ar upda t es M ethod D escrip t ion r epl a c e() s plit() s plitl ine s () s t a r t sw ith() s w apca s e() t it l e() t r an s l a t e() uppe r() z f il l () R eturns a st r i n g w he r e a speci f ie d v al u e i s r epla c ed with a speci f ied v al u e S pl i ts the st r i n g at the speci f ie d sepa r a t o r , and r eturns a l i st S pl i ts the st r i n g at li n e b r eaks and r eturns a l i st R eturns tr u e i f the st r i n g sta r ts with the speci f ie d v al u e S w aps cases , l o w er cas e be c omes uppe r cas e and v i c e v e r sa C o n v e r ts the f irs t cha r ac t er of each w o r d t o uppe r case R eturns a t r ansla t ed st r i n g C o n v erts a str i ng i n t o u pper case F i l ls the st r i n g with a speci f ie d n u m b er of v al u es at the beg i n n i n g