<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC4%2FAdvanced-features-of-functions_%2FEnglish</id>
		<title>Python/C4/Advanced-features-of-functions /English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC4%2FAdvanced-features-of-functions_%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C4/Advanced-features-of-functions_/English&amp;action=history"/>
		<updated>2026-04-08T20:00:28Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C4/Advanced-features-of-functions_/English&amp;diff=509&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and Welcome to the tutorial on '…'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C4/Advanced-features-of-functions_/English&amp;diff=509&amp;oldid=prev"/>
				<updated>2012-11-29T06:21:17Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and Welcome to the tutorial on &amp;#039;…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1 &lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello friends and Welcome to the tutorial on 'advanced features of functions'.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2 &lt;br /&gt;
&lt;br /&gt;
Learning objective &lt;br /&gt;
| At the end of this tutorial, you will be able to,&lt;br /&gt;
&lt;br /&gt;
# Assign default values to arguments, when defining functions.&lt;br /&gt;
# Define and call functions with keyword arguments.&lt;br /&gt;
# Learn some of the built-in functions available in Python standard library and the scientific computing libraries.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3&lt;br /&gt;
&lt;br /&gt;
Pre-requisite slide &lt;br /&gt;
| Before beginning this tutorial,we would suggest you to complete the tutorial on &amp;quot;Getting started with functions&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Open the terminal &lt;br /&gt;
&lt;br /&gt;
 ipython -pylab.&lt;br /&gt;
| Let us Start the ipython interpreter&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  round(2.484)&lt;br /&gt;
 &lt;br /&gt;
 round(2.484, 2)&lt;br /&gt;
| Let's use the &amp;lt;tt&amp;gt;round&amp;lt;/tt&amp;gt; function as an example to understand what a default value of an argument means. Let's type the following expressions in the terminal.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Both the first expression and the second are calls to the &amp;lt;tt&amp;gt;round&amp;lt;/tt&amp;gt; function, but the first calls it with only one argument and the second calls it with two arguments. By observing the output, we can guess that the first one is equivalent to call with the second argument being 0. 0 .&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4 &lt;br /&gt;
&lt;br /&gt;
Examples of functions showing default values &lt;br /&gt;
| '''s.strip() # strips on spaces.'''&lt;br /&gt;
&lt;br /&gt;
s.strip('@') # strips the string of '@' symbols.&lt;br /&gt;
&lt;br /&gt;
Thus it can be said that here, blank space is the default argument.&lt;br /&gt;
&lt;br /&gt;
plot(x, y) # plots with x vs. y using default line style. plot(x, y, 'o') # plots x vs. y with circle markers.&lt;br /&gt;
&lt;br /&gt;
Hence, here when third argument is not provided, it shows default line style.&lt;br /&gt;
&lt;br /&gt;
linspace(0, 2*pi, 100) # returns 100 points between 0 and 2pi linspace(0, 2*pi) # returns 50 points between 0 and 2pi&lt;br /&gt;
&lt;br /&gt;
Hence, the default for the third argument is 50.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch to terminal &lt;br /&gt;
&lt;br /&gt;
 def welcome(greet, name=&amp;quot;World&amp;quot;):&lt;br /&gt;
     print greet, name&lt;br /&gt;
| Let's now define a simple function that uses default arguments. We define a simple function that prints a welcome message to a person, given a greeting and his/her name.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  welcome(&amp;quot;Hi&amp;quot;, &amp;quot;Guido&amp;quot;)&lt;br /&gt;
| Let us first call the function with two arguments, one for &amp;lt;tt&amp;gt;greet&amp;lt;/tt&amp;gt; and other for &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  welcome(&amp;quot;Hello&amp;quot;)&lt;br /&gt;
| We get the expected welcome message, &amp;quot;Hi Guido&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Now let us call the function with just one argument &amp;quot;Hello&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| &amp;quot;Hello&amp;quot; is treated as the &amp;lt;tt&amp;gt;greet&amp;lt;/tt&amp;gt; and we get &amp;quot;Hello World&amp;quot; as the output. &amp;quot;World&amp;quot; is the default value for the argument &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 5 &lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| '''Redefine the function &amp;lt;tt&amp;gt;welcome&amp;lt;/tt&amp;gt;, by interchanging it's'''&lt;br /&gt;
&lt;br /&gt;
arguments. Place the &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; argument with it's default value of &amp;quot;World&amp;quot; before the &amp;lt;tt&amp;gt;greet&amp;lt;/tt&amp;gt; argument.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 def welcome(name=&amp;quot;World&amp;quot;, greet):&lt;br /&gt;
     print greet, name&lt;br /&gt;
| Switch to the terminal for solution&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6 &lt;br /&gt;
&lt;br /&gt;
Solution 1 &lt;br /&gt;
| We get an error that reads &amp;lt;tt&amp;gt;SyntaxError: non-default argument follows default argument&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| When defining a function all the argument with default values should come at the end.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 7 &lt;br /&gt;
&lt;br /&gt;
Assignment 2 &lt;br /&gt;
| '''See the definition of linspace using &amp;lt;tt&amp;gt;?&amp;lt;/tt&amp;gt; and make a note of all'''&lt;br /&gt;
&lt;br /&gt;
the arguments with default values are towards the end.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 linspace?&lt;br /&gt;
| Switch to the terminal for solution&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| &amp;lt;nowiki&amp;gt;As we go on hitting the enter key, we the the number of arguments this command has. Please read the content on your terminal. &amp;lt;pause&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again,Pause the video here,try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 8 &lt;br /&gt;
&lt;br /&gt;
assignment 3 &lt;br /&gt;
| '''Redefine the function &amp;lt;tt&amp;gt;welcome&amp;lt;/tt&amp;gt; with a default value of'''&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Hello&amp;quot; to the &amp;lt;tt&amp;gt;greet&amp;lt;/tt&amp;gt; argument. Then, call the function without any arguments.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 def welcome(greet=&amp;quot;Hello&amp;quot;, name=&amp;quot;World&amp;quot;):&lt;br /&gt;
     print greet, name&lt;br /&gt;
 &lt;br /&gt;
 welcome()&lt;br /&gt;
| Switch to the terminal for solution&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| As we can see, we get the output as &amp;lt;tt&amp;gt;Hello World&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let us now learn what keyword arguments or named arguments are. We shall refer to them as keyword arguments, henceforth.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch to ipython terminal &lt;br /&gt;
&lt;br /&gt;
 welcome()&lt;br /&gt;
 &lt;br /&gt;
 welcome(&amp;quot;Hello&amp;quot;, &amp;quot;James&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 welcome(&amp;quot;Hi&amp;quot;, name=&amp;quot;Guido&amp;quot;)&lt;br /&gt;
| When you are calling functions in Python, you don't need to remember the order in which to pass the arguments. Instead, you can use the name of the argument to pass it a value. This slide shows a few function calls that use keyword arguments. &amp;lt;tt&amp;gt;loc&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;linewidth&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;xy&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;labels&amp;lt;/tt&amp;gt; are being called with keyword arguments.&lt;br /&gt;
&lt;br /&gt;
Let us try and understand this better using the &amp;lt;tt&amp;gt;welcome&amp;lt;/tt&amp;gt; function that we have been using all along. Let us call it in different ways and observe the output to see how keyword arguments work.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  welcome(name=&amp;quot;Guido&amp;quot;, greet=&amp;quot;Hey! &amp;quot;)&lt;br /&gt;
| When no keyword is specified, the arguments are allotted based on their position. So, &amp;quot;Hi&amp;quot; is the value of the argument &amp;lt;tt&amp;gt;greet&amp;lt;/tt&amp;gt; and name is passed the value &amp;quot;Guido&amp;quot;.If we type,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  welcome(name=&amp;quot;Guido&amp;quot;, &amp;quot;Hey&amp;quot;)&lt;br /&gt;
| When keyword arguments are used, the arguments can be called in any order. And if we call our function as,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| This call returns an error that reads, &amp;lt;tt&amp;gt;non-keyword arg after keyword arg&amp;lt;/tt&amp;gt;. Python expects all the keyword to be present towards the end.&lt;br /&gt;
&lt;br /&gt;
That brings us to the end of what we wanted to learn about &amp;lt;tt&amp;gt;keyword&amp;lt;/tt&amp;gt; arguments.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 9 &lt;br /&gt;
&lt;br /&gt;
built-in functions &lt;br /&gt;
| Before defining a function of your own, make sure that you check the standard library, for a similar function. Python is popularly called a &amp;quot;Batteries included&amp;quot; language, for the huge library that comes along with it.&lt;br /&gt;
&lt;br /&gt;
Math functions - abs, sin, .... Plot functions - plot, bar, pie ... Boolean functions - and, or, not ...&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 10 &lt;br /&gt;
&lt;br /&gt;
Showing classes of functions in pylab, scipy &lt;br /&gt;
| Apart from the standard library there are other libraries like &amp;lt;tt&amp;gt;pylab&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;scipy&amp;lt;/tt&amp;gt;, etc which have a huge collection of functions for scientific purposes.&lt;br /&gt;
&lt;br /&gt;
'''pylab'''&lt;br /&gt;
&lt;br /&gt;
plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog&lt;br /&gt;
&lt;br /&gt;
'''scipy (modules)'''&lt;br /&gt;
&lt;br /&gt;
fftpack, stats, linalg, ndimage, signal, optimize, integrate&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 11 &lt;br /&gt;
&lt;br /&gt;
Summary slide &lt;br /&gt;
| This brings us to the end of this tutorial. In this tutorial, we have learnt to,&lt;br /&gt;
&lt;br /&gt;
# Define functions with default arguments.&lt;br /&gt;
# Call functions using keyword arguments.&lt;br /&gt;
# Use the range of functions available in the Python standard library and the Scientific Computing related packages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 12 &lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide &lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
# All arguments of a function cannot have default values. - True or False?&lt;br /&gt;
&lt;br /&gt;
2. The following is a valid function definition. True or False?&lt;br /&gt;
&lt;br /&gt;
 def seperator(count=40, char, show=False):&lt;br /&gt;
     if show:&lt;br /&gt;
         print char * count&lt;br /&gt;
     return char * count&lt;br /&gt;
&lt;br /&gt;
# When calling a function,&lt;br /&gt;
** the arguments should always be in the order in which they are defined.&lt;br /&gt;
** the arguments can be in any order.&lt;br /&gt;
** '''only keyword arguments can be in any order, but should be called'''&amp;lt;br/&amp;gt; at the beginning.&lt;br /&gt;
** only keyword arguments can be in any order, but should be called at the end.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 13&lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
# False. All arguments of a Python function can have default values.&lt;br /&gt;
# False. All parameters with default arguments should be defined at the end.&lt;br /&gt;
# When calling a function,only keyword arguments can be in any order, but should be called at the end.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 14 &lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide &lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank you!&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>