0% found this document useful (0 votes)
32 views12 pages

Unit. 3. L-6,7,8

Python lectures

Uploaded by

ldfh2536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
32 views12 pages

Unit. 3. L-6,7,8

Python lectures

Uploaded by

ldfh2536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 12
"local voriable is defined within a function, i's accesible exclusively within Abst function and ony statement win te function cn tz | | | | | *Local variables are established during the functo {erminated when the function concludes sxecution and are Ss ee... —— on. 8 and b with values 50 and with values 22 Notes nthe above code we have functions test1(), test2(. In these Hnctions local variables are declared with same variable names a and BB bith are stored in diferent part of memory and t Sree Sees *Aglobel variable is vsble to every function and canbe used by any pece of falls and throughout the program cxeatic Hime haves local variable defines th tho sare none 3s global vriable the lea variable tics procedence over the global variable While sing aclobal varies inside uncon, that variable seeds to be eck ise a local variable with the sme name Pies cic QUID 0 underiond more Wites rosram to demonstrat the usage of slobsl and loca vvsbles. ot | comment lines end complete theca, Fol telaoaeela a ey | nes Eiiiean example that explain futher lobe and local variable Note: Notice Cling change local has no effec PPG Riese input» fom the user which i cena oll fedtor contains two uncon allowing the comment ines = and Complete the code by Sree Se) feombines functions in away tet the return Value of each function ‘xgument of the net function, Lets consider an example: * Consider we functions * Compastion of two fu * isthe argument of f 2 # Theretum vale of 2s passed oc = The result ofthe compostion fons 1 and €2is denoted on argument to #4 the rtum value of 4 Fea itstictons ae write the missing code in tre below * Inthe blow program twa argument BP Fi the composition of two fn gsm, ions square and double ae dtined with an ions square(double(aum)) and pn che the input rom the user eke | See) 4 Func 621. @ heen f° ® program that uses function composition i following the data given below erable arguments have v umber of arguments function, you have the ying lengths. The t9 function This sgn flexblty to provide re employed to transmit a flexible ies that whenever you cal the numberof argu HHefe we pass functions es arguments SE EID 210910 tro uncon compe eases thet demonstates ction compeston Defi the following functions So The tng eM. fl, ees neftuncionfboc ooh AN EE | “Sequential t0 an input vate SS This tunction shou wo MOS anc segue, Firion shuts take a umber x and sours oa Sy Btn shot tte a rum and tun hal ots Hetil then do the folowing Sree | eee) =o © A trcion cl ten made wthin the defston of he same onion own leer win tn boty | the function cals the same uv function. This seo known Se memattion or oott- @ bre time a recursive calls mode, the arguments passed to the function shoul be G6SO Ia reference. Therm isa distinct ierepce more than one between thenomma and recursive func +A normal function vill be invoked by another fonction. + A recursive function is invoked by isl ect or iniecty aslong 25 the given fondition is aise, * Recursive functions 4° seul while constructing the data Stnuctres ike stacks, qu ues inked fists and ess necursve function most the flowing two erteia eed a jon function calls can be classified int A. Direct recursion : 2 The function calls itself directly. n suc ‘only one function is involved, whi ‘given condition is met. 43 Indirect recursion or circular recurs 4 Two or more functions can be involved, where the ecursive call be initiated by ane ofthe intermediary functions Ex=funt calls fund, fun 2 calls fun3, and fun3 cals the below two types: h cals itself until the fut ‘Some ofthe important points to remember when writing a program involving recursion process; *Afunction must call itself to enable recursion Recusion {Sccurs only within user-defined functions. ibrary functions Brent eligible because their source code isn't accessible. Mobis recursve function, i's crucial to establish 2 ferminating caqcition and an appropriate terminating Bistoment othfrwise the calls might recut into an infite | ‘oop withthe same name in recursive functions alls are stacked until the terminating condition (Once that happens the cals inthe stack are They execute ina last st-out order, which can to stack overflow if the call depth becomes too high Teka Oc eae Indirect recursion involves only one function eli Recursion involves calling only ane function, while {8 program to find the +. iat of a given number using the recursion Print the resuit as shown in the examples, below program it writes » recurfact() function. he main program it takes Jnput and sends it tothe recurfact( function WFscurfact( function contains base and recursive criterion, where base condition ‘only then the value equals zero (0), ple Input andl Output 1: ‘actorian: ste ° eke Ses Bpprogram tofind the ==: of a recursive numberist ike 1 = 11,10, = 5 71 97) print the result ax shown in the exmole We cannot use sum) becouse it wil produce the following e Traceback (noe be shove code and filin the missing code inthe elitor >» Siena) | ee) Program to add two inputs using recursion, Pint the result as shown inthe Seukeueai Ses Python berumerous | Pi oven rowthomatca fuetions | wich raya in math] [Bre cmstesion tars incon most. | | Jo 2035. sinpe program using econ BU La He 2x4 ites program at aca: rec catagories Some ofthe he E Sample Test Cases BB resnind ste 1@ 3014 wate a program at cacltes eer {© 2215, wine program 344 te number B02 Prt Functions Ba. ata ureters 10 5139. Undersandng Material urctons “B12 Random Numbe Functions random uncon retvns random lus bene Python provides may random umber futons. We we these ae snot seeing etap in andeange | | functions in ares like each, games| cryptography, simulation and other pletion Fisancoa fede a » 1.eofeee) trims com interpreter izes en Indexer 2. shuts) Isher» ven it. 4. randint(,b) - sets a random integer bebween aan nie

You might also like