LAB 5 : Functions in Python
<div class="whitepaper-body-page">
<p>Functions are reusable pieces of programs. They allow you to give a name to a block of statements allowing you to run that block using the specified name anywhere in your program and any number of times. This is know as calling function.</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">printthis</span><span class="enlighter-g1">()</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'Welcome to RSTForum'</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">printthis</span><span class="enlighter-g1">()</span></div></div></div><div class="enlighter-raw">def printthis():
print('Welcome to RSTForum')
printthis()</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def printthis():
print('Welcome to RSTForum')
printthis()</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-77804" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-77804" aria-expanded="false">Show Output</button><div id="ac-77804" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>Welcome to RSTForum</code></pre>
</div></div>
<p>Above was an example of simple function without arguments.Lets see an example where we can provide an argument to the function</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">printthis</span><span class="enlighter-g1">(</span><span class="enlighter-text">string</span><span class="enlighter-g1">)</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">string</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">printthis</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'Hi All'</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">printthis</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'Welcome to RSTforum'</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">printthis</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'Best institute for python training'</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">def printthis(string):
print(string)
printthis('Hi All')
printthis('Welcome to RSTforum')
printthis('Best institute for python training')</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def printthis(string):
print(string)
printthis('Hi All')
printthis('Welcome to RSTforum')
printthis('Best institute for python training')</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-67237" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-67237" aria-expanded="false">Show Output</button><div id="ac-67237" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>Hi All
Welcome to RSTforum
Best institute for python training</code></pre>
</div></div>
<h4 class="wp-block-heading">Positional Argument in Python</h4>
<p>Positional arguments are arguments that need to be included in the proper position or order. The first positional argument always needs to be listed first when the function is called</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">addit</span><span class="enlighter-g1">(</span><span class="enlighter-text">a,b</span><span class="enlighter-g1">)</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> z=a+b</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">z</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">addit</span><span class="enlighter-g1">(</span><span class="enlighter-n1">100</span><span class="enlighter-text">,</span><span class="enlighter-n1">2345</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">addit</span><span class="enlighter-g1">(</span><span class="enlighter-n1">412</span><span class="enlighter-text">,</span><span class="enlighter-n1">456</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">def addit(a,b):
z=a+b
print(z)
addit(100,2345)
addit(412,456)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def addit(a,b):
z=a+b
print(z)
addit(100,2345)
addit(412,456)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-17903" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-17903" aria-expanded="false">Show Output</button><div id="ac-17903" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>2445
868</code></pre>
</div></div>
<h4 class="wp-block-heading"><strong>Default Arguments</strong> in python</h4>
<p>They allow you to give default value to your parameters. Default arguments can be given only to the last variable def fuc(a,b=5) is correct but def func(a=5,b) is wrong.</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">say</span><span class="enlighter-g1">(</span><span class="enlighter-text">string,times=</span><span class="enlighter-n1">2</span><span class="enlighter-g1">)</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">string<em>times</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">say</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'hello'</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">say</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'Bye'</span><span class="enlighter-text">,</span><span class="enlighter-n1">5</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">def say(string,times=2):
print(string</em>times)
say('hello')
say('Bye',5)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def say(string,times=2):
print(string*times)
say('hello')
say('Bye',5)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-50001" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-50001" aria-expanded="false">Show Output</button><div id="ac-50001" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>hellohello
ByeByeByeByeBye</code></pre>
</div></div>
<h4 class="wp-block-heading">Keyword Arguments</h4>
<p>Python allows functions to be called using keyword arguments. When we call functions in this way, the order (position) of the arguments can be changed.</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">func</span><span class="enlighter-g1">(</span><span class="enlighter-text">a=</span><span class="enlighter-n1">1</span><span class="enlighter-text">,b=</span><span class="enlighter-n1">4</span><span class="enlighter-text">,c=</span><span class="enlighter-n1">3</span><span class="enlighter-g1">)</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'a is:'</span><span class="enlighter-text">,a,</span><span class="enlighter-s0">'b is:'</span><span class="enlighter-text">,b,</span><span class="enlighter-s0">'c is:'</span><span class="enlighter-text">,c</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">func</span><span class="enlighter-g1">(</span><span class="enlighter-text">c=</span><span class="enlighter-n1">4</span><span class="enlighter-text">,b=</span><span class="enlighter-n1">6</span><span class="enlighter-text">,a=</span><span class="enlighter-n1">1</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">def func(a=1,b=4,c=3):
print('a is:',a,'b is:',b,'c is:',c)
func(c=4,b=6,a=1)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def func(a=1,b=4,c=3):
print('a is:',a,'b is:',b,'c is:',c)
func(c=4,b=6,a=1)
</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-99123" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-99123" aria-expanded="false">Show Output</button><div id="ac-99123" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>a is: 1 b is: 6 c is: 4</code></pre>
</div></div>
<h4 class="wp-block-heading">Variable Arguments in Python</h4>
<p>Sometimes you might want to define a function that can take <em>any </em>number of parameters, i.e. variable/multiple number of arguments , this can be achieved using the variable arguments</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">variab</span><span class="enlighter-g1">(</span><span class="enlighter-text">a,<em>number,**phonebook</span><span class="enlighter-g1">)</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'a'</span><span class="enlighter-text">,a</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k1">for</span><span class="enlighter-text"> i </span><span class="enlighter-k0">in</span><span class="enlighter-text"> number:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'number'</span><span class="enlighter-text">,i</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k1">for</span><span class="enlighter-text"> name,extn </span><span class="enlighter-k0">in</span><span class="enlighter-text"> phonebook.</span><span class="enlighter-m1">items</span><span class="enlighter-g1">()</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">name,extn</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">variab</span><span class="enlighter-g1">(</span><span class="enlighter-n1">10</span><span class="enlighter-text">,</span><span class="enlighter-n1">11</span><span class="enlighter-text">,</span><span class="enlighter-n1">3</span><span class="enlighter-text">,</span><span class="enlighter-n1">5</span><span class="enlighter-text">,paul=</span><span class="enlighter-n1">1000</span><span class="enlighter-text">,harry=</span><span class="enlighter-n1">1001</span><span class="enlighter-text">,richard=</span><span class="enlighter-n1">1101</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">def variab(a,</em>number,<strong>phonebook):
print('a',a)
for i in number:
print('number',i)
for name,extn in phonebook.items():
print(name,extn)
variab(10,11,3,5,paul=1000,harry=1001,richard=1101)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def variab(a,*number,</strong>phonebook):
print('a',a)
for i in number:
print('number',i)
for name,extn in phonebook.items():
print(name,extn)
variab(10,11,3,5,paul=1000,harry=1001,richard=1101)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-89086" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-89086" aria-expanded="false">Show Output</button><div id="ac-89086" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>10
11
3
5
paul 1000
harry 1001
richard 1101</code></pre>
<p></p>
</div></div>
<h4 class="wp-block-heading">Namespace and scope resolution</h4>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">x=</span><span class="enlighter-n1">50</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">fun</span><span class="enlighter-g1">()</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> x =</span><span class="enlighter-n1">2</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'value of x inside the func is'</span><span class="enlighter-text">,x</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">fun</span><span class="enlighter-g1">()</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'value of x outside the func is'</span><span class="enlighter-text">,x</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">x=50
def fun():
x =2
print('value of x inside the func is',x)<br>fun()
print('value of x outside the func is',x)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">x=50
def fun():
x =2
print('value of x inside the func is',x)<br>fun()
print('value of x outside the func is',x)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-90611" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-90611" aria-expanded="false">Show Output</button><div id="ac-90611" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>value of x inside the func is 2
value of x outside the func is 50</code></pre>
</div></div>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">x=</span><span class="enlighter-n1">50</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">fun</span><span class="enlighter-g1">()</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k0">global</span><span class="enlighter-text"> x</span></div></div><div class=""><div><span class="enlighter-text"> x =</span><span class="enlighter-n1">2</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'value of x inside the func is'</span><span class="enlighter-text">,x</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">fun</span><span class="enlighter-g1">()</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'value of x outside the func is'</span><span class="enlighter-text">,x</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">x=50
def fun():
global x
x =2
print('value of x inside the func is',x)<br>fun()
print('value of x outside the func is',x)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">x=50
def fun():
global x
x =2
print('value of x inside the func is',x)<br>fun()
print('value of x outside the func is',x)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-82901" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-82901" aria-expanded="false">Show Output</button><div id="ac-82901" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>value of x inside the func is 2
value of x outside the func is 2</code></pre>
</div></div>
<h4 class="wp-block-heading"><strong>Return Statement</strong></h4>
<p>The return statement is used to <em>exit or break out</em> of the function. We can optionally <em>return a value </em>from the function as well. Function always returns none value if we have not mentioned what value it has to return. Every function has a return none statement explicitly at the end .</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">say</span><span class="enlighter-g1">()</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'Hello'</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k0">return</span><span class="enlighter-text"> </span><span class="enlighter-n1">10</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-m0">say</span><span class="enlighter-g1">())</span></div></div></div><div class="enlighter-raw">def say():
print('Hello')
return 10
print(say())</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def say():
print('Hello')
return 10
print(say())</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-92629" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-92629" aria-expanded="false">Show Output</button><div id="ac-92629" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>Hello
10</code></pre>
</div></div>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">square</span><span class="enlighter-g1">(</span><span class="enlighter-text">x</span><span class="enlighter-g1">)</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k0">return</span><span class="enlighter-text"> x<strong></span><span class="enlighter-n1">2</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-m0">square</span><span class="enlighter-g1">(</span><span class="enlighter-n1">9</span><span class="enlighter-g1">))</span></div></div></div><div class="enlighter-raw">def square(x):
return x</strong>2
print(square(9))</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def square(x):
return x**2
print(square(9))</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-52726" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-52726" aria-expanded="false">Show Output</button><div id="ac-52726" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>81</code></pre>
</div></div>
<p>Example : To find average percentage of two students</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">percentage</span><span class="enlighter-g1">(</span><span class="enlighter-text">x</span><span class="enlighter-g1">)</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k1">if</span><span class="enlighter-text"> x</span><span class="enlighter-g1"><</span><span class="enlighter-n1">650</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> marks=x/</span><span class="enlighter-n1">650</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"> percent=marks<em></span><span class="enlighter-n1">100</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k0">return</span><span class="enlighter-text"> percent</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k1">else</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'Invalid marks'</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">peter=</span><span class="enlighter-m0">percentage</span><span class="enlighter-g1">(</span><span class="enlighter-n1">500</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'Peter has scored'</span><span class="enlighter-text">,peter</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">john=</span><span class="enlighter-m0">percentage</span><span class="enlighter-g1">(</span><span class="enlighter-n1">640</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'John has scored'</span><span class="enlighter-text">,john</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">average = </span><span class="enlighter-g1">(</span><span class="enlighter-text">peter + john</span><span class="enlighter-g1">)</span><span class="enlighter-text">/</span><span class="enlighter-n1">2</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">"The average percentage of both is"</span><span class="enlighter-text">,average</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">def percentage(x):
if x<650:
marks=x/650
percent=marks</em>100
return percent
else:
print('Invalid marks')
peter=percentage(500)
print('Peter has scored',peter)
john=percentage(640)
print('John has scored',john)
average = (peter + john)/2
print("The average percentage of both is",average)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def percentage(x):
if x<650:
marks=x/650
percent=marks*100
return percent
else:
print('Invalid marks')
peter=percentage(500)
print('Peter has scored',peter)
john=percentage(640)
print('John has scored',john)
average = (peter + john)/2
print("The average percentage of both is",average)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-81111" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-81111" aria-expanded="false">Show Output</button><div id="ac-81111" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>Peter has scored 76.92307692307693
John has scored 98.46153846153847
The average percentage of both is 87.69230769230771</code></pre>
</div></div>
<h4 class="wp-block-heading">Lambda() function in Python</h4>
<p>Lambda function is a way to create small anonymous functions, i.e. functions without a name. These functions are throw-away functions, i.e. they are just needed where they have been created.</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">add=lambda x </span><span class="enlighter-g0">:</span><span class="enlighter-text"> </span><span class="enlighter-n1">20</span><span class="enlighter-text">+x</span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">add</span><span class="enlighter-g1">(</span><span class="enlighter-n1">10</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">add=lambda x : 20+x
add(10)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">add=lambda x : 20+x
add(10)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-5258" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-5258" aria-expanded="false">Show Output</button><div id="ac-5258" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>30</code></pre>
</div></div>
<h4 class="wp-block-heading">Lambda() function with filter() function</h4>
<p>Lamda with filter() , filter () works on a pre defined condition. IN below example filter function will provide a list of numbers which are >5 in our given list.You cannot perform operation like division, multiplication , addition etc while using filter()</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">list1=</span><span class="enlighter-g1">[</span><span class="enlighter-n1">1</span><span class="enlighter-text">,</span><span class="enlighter-n1">3</span><span class="enlighter-text">,</span><span class="enlighter-n1">4</span><span class="enlighter-text">,</span><span class="enlighter-n1">5</span><span class="enlighter-text">,</span><span class="enlighter-n1">6</span><span class="enlighter-text">,</span><span class="enlighter-n1">8</span><span class="enlighter-text">,</span><span class="enlighter-n1">9</span><span class="enlighter-text">,</span><span class="enlighter-n1">12</span><span class="enlighter-g1">]</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">fil=</span><span class="enlighter-m0">filter</span><span class="enlighter-g1">(</span><span class="enlighter-k0">lambda</span><span class="enlighter-text"> x:x</span><span class="enlighter-g1">></span><span class="enlighter-n1">5</span><span class="enlighter-text">,list1</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-m0">list</span><span class="enlighter-g1">(</span><span class="enlighter-text">fil</span><span class="enlighter-g1">))</span></div></div></div><div class="enlighter-raw">list1=[1,3,4,5,6,8,9,12]
fil=filter(lambda x:x>5,list1)
print(list(fil))</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">list1=[1,3,4,5,6,8,9,12]
fil=filter(lambda x:x>5,list1)
print(list(fil))</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-94536" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-94536" aria-expanded="false">Show Output</button><div id="ac-94536" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>[6, 8, 9, 12]</code></pre>
</div></div>
<h4 class="wp-block-heading">Lambda() Function with map() function</h4>
<p>Lambda with map() , map () function is used to apply a particular operation like division, multiplication, addition etc.</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">x=</span><span class="enlighter-g1">[</span><span class="enlighter-n1">1</span><span class="enlighter-text">,</span><span class="enlighter-n1">3</span><span class="enlighter-text">,</span><span class="enlighter-n1">4</span><span class="enlighter-text">,</span><span class="enlighter-n1">5</span><span class="enlighter-text">,</span><span class="enlighter-n1">6</span><span class="enlighter-text">,</span><span class="enlighter-n1">8</span><span class="enlighter-text">,</span><span class="enlighter-n1">9</span><span class="enlighter-text">,</span><span class="enlighter-n1">12</span><span class="enlighter-g1">]</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">fil=</span><span class="enlighter-m0">map</span><span class="enlighter-g1">(</span><span class="enlighter-k0">lambda</span><span class="enlighter-text"> x:x<em></span><span class="enlighter-n1">5</span><span class="enlighter-text">,x</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-m0">list</span><span class="enlighter-g1">(</span><span class="enlighter-text">fil</span><span class="enlighter-g1">))</span></div></div></div><div class="enlighter-raw">x=[1,3,4,5,6,8,9,12]
fil=map(lambda x:x</em>5,x)
print( list(fil))</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">x=[1,3,4,5,6,8,9,12]
fil=map(lambda x:x*5,x)
print( list(fil))</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-49443" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-49443" aria-expanded="false">Show Output</button><div id="ac-49443" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>[5, 15, 20, 25, 30, 40, 45, 60]</code></pre>
</div></div>
<h4 class="wp-block-heading">Lambda function with reduce() function</h4>
<p>Lambda with reduce(), reduce() like map(), is used to apply an operation to every element in a sequence. However, it differs from the map in its working.</p>
<ul><li>Perform the defined operation on the first 2 elements of the sequence.</li><li>Save this result.</li><li>Perform the operation with the saved result and the next element in the sequence</li><li>Repeat until no more elements are left.</li></ul>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">from</span><span class="enlighter-text"> functools </span><span class="enlighter-k0">import</span><span class="enlighter-text"> reduce</span></div></div><div class=""><div><span class="enlighter-text">fil=</span><span class="enlighter-m0">reduce</span><span class="enlighter-g1">(</span><span class="enlighter-k0">lambda</span><span class="enlighter-text"> x,y:x+y,x</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">fil</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">from functools import reduce
fil=reduce(lambda x,y:x+y,x)
print(fil)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from functools import reduce
fil=reduce(lambda x,y:x+y,x)
print(fil)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-98326" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-98326" aria-expanded="false">Show Output</button><div id="ac-98326" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>48</code></pre>
<p></p>
</div></div>
<h4 class="wp-block-heading">Generator function and generator expression in Python</h4>
<p>Generator functions look and act just like regular functions, but with one defining characteristic. Generator functions use the Python yield keyword instead of return.</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">func</span><span class="enlighter-g1">(</span><span class="enlighter-text">x</span><span class="enlighter-g1">)</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k1">for</span><span class="enlighter-text"> i </span><span class="enlighter-k0">in</span><span class="enlighter-text"> x:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k0">yield</span><span class="enlighter-g1">(</span><span class="enlighter-text">i<em>i</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k1">if</span><span class="enlighter-text"> x.</span><span class="enlighter-m1">index</span><span class="enlighter-g1">(</span><span class="enlighter-text">i</span><span class="enlighter-g1">)</span><span class="enlighter-text">==</span><span class="enlighter-n1">2</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'index value 2 encountered'</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">li=</span><span class="enlighter-m0">func</span><span class="enlighter-g1">([</span><span class="enlighter-n1">10</span><span class="enlighter-text">,</span><span class="enlighter-n1">20</span><span class="enlighter-text">,</span><span class="enlighter-n1">30</span><span class="enlighter-text">,</span><span class="enlighter-n1">40</span><span class="enlighter-text">,</span><span class="enlighter-n1">50</span><span class="enlighter-g1">])</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">li</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-m0">next</span><span class="enlighter-g1">(</span><span class="enlighter-text">li</span><span class="enlighter-g1">))</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-m0">next</span><span class="enlighter-g1">(</span><span class="enlighter-text">li</span><span class="enlighter-g1">))</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-m0">next</span><span class="enlighter-g1">(</span><span class="enlighter-text">li</span><span class="enlighter-g1">))</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-m0">next</span><span class="enlighter-g1">(</span><span class="enlighter-text">li</span><span class="enlighter-g1">))</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-m0">next</span><span class="enlighter-g1">(</span><span class="enlighter-text">li</span><span class="enlighter-g1">))</span></div></div></div><div class="enlighter-raw">def func(x):
for i in x:
yield(i</em>i)
if x.index(i)==2:
print('index value 2 encountered')
li=func([10,20,30,40,50])
print(li)
print(next(li))
print(next(li))
print(next(li))
print(next(li))
print(next(li))</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def func(x):
for i in x:
yield(i*i)
if x.index(i)==2:
print('index value 2 encountered')
li=func([10,20,30,40,50])
print(li)
print(next(li))
print(next(li))
print(next(li))
print(next(li))
print(next(li))</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-30659" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-30659" aria-expanded="false">Show Output</button><div id="ac-30659" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code><generator object func at 0x000001DC29DAB410>
100
400
900
index value 2 encountered
1600
2500</code></pre>
<p></p>
</div></div>
<p>when next() is called on a generator object (either explicitly or implicitly within a for loop), the previously yielded variable is incremented, and then yielded again.</p>
<h4 class="wp-block-heading">Nested Function in python</h4>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">gen</span><span class="enlighter-g1">()</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> x=</span><span class="enlighter-n1">2</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">x,</span><span class="enlighter-s0">'this is the outher func'</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">g</span><span class="enlighter-g1">()</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">x,</span><span class="enlighter-s0">'this is inner'</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">g</span><span class="enlighter-g1">()</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">gen</span><span class="enlighter-g1">()</span></div></div></div><div class="enlighter-raw">def gen():
x=2
print(x,'this is the outher func')
def g():
print(x,'this is inner')
g()
gen()</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def gen():
x=2
print(x,'this is the outher func')
def g():
print(x,'this is inner')
g()
gen()</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-83019" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-83019" aria-expanded="false">Show Output</button><div id="ac-83019" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>2 this is the outher func
2 this is inner</code></pre>
<p></p>
</div></div>
<p></p>
<p></div></p>