Python/C4/Advanced-features-of-functions/English

From Script | Spoken-Tutorial
Revision as of 13:13, 2 December 2012 by Pravin1389 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
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 'advanced features of functions'.
Show Slide 2

Learning objective

At the end of this tutorial, you will be able to,
  1. Assign default values to arguments, when defining functions.
  2. Define and call functions with keyword arguments.
  3. Learn some of the built-in functions available in Python standard library and the scientific computing libraries.


Show Slide 3

Pre-requisite slide

Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with functions".
Open the terminal
ipython -pylab.
Let us Start the ipython interpreter
round(2.484)
round(2.484, 2)
Let's use the round function as an example to understand what a default value of an argument means. Let's type the following expressions in the terminal.
Both the first expression and the second are calls to the round 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 .
Show Slide 4

Examples of functions showing default values

s.strip() # strips on spaces.

s.strip('@') # strips the string of '@' symbols.

Thus it can be said that here, blank space is the default argument.

plot(x, y) # plots with x vs. y using default line style. plot(x, y, 'o') # plots x vs. y with circle markers.

Hence, here when third argument is not provided, it shows default line style.

linspace(0, 2*pi, 100) # returns 100 points between 0 and 2pi linspace(0, 2*pi) # returns 50 points between 0 and 2pi

Hence, the default for the third argument is 50.

Switch to terminal
def welcome(greet, name="World"):
    print greet, name
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.
welcome("Hi", "Guido") Let us first call the function with two arguments, one for greet and other for name.
welcome("Hello") We get the expected welcome message, "Hi Guido".

Now let us call the function with just one argument "Hello".

"Hello" is treated as the greet and we get "Hello World" as the output. "World" is the default value for the argument name.

Pause the video here, try out the following exercise and resume the video.

Show Slide 5

Assignment 1

Redefine the function welcome, by interchanging it's

arguments. Place the name argument with it's default value of "World" before the greet argument.

Continue from paused state Switch to the terminal
def welcome(name="World", greet):
    print greet, name
Switch to the terminal for solution
Show Slide 6

Solution 1

We get an error that reads SyntaxError: non-default argument follows default argument.
When defining a function all the argument with default values should come at the end.

Pause the video here, try out the following exercise and resume the video.

Show Slide 7

Assignment 2

See the definition of linspace using ? and make a note of all

the arguments with default values are towards the end.

Continue from paused state Switch to the terminal
linspace?
Switch to the terminal for solution
As we go on hitting the enter key, we the the number of arguments this command has. Please read the content on your terminal. <pause>

Again,Pause the video here,try out the following exercise and resume the video.

Show Slide 8

assignment 3

Redefine the function welcome with a default value of

"Hello" to the greet argument. Then, call the function without any arguments.

Continue from paused state Switch to the terminal
def welcome(greet="Hello", name="World"):
    print greet, name

welcome()
Switch to the terminal for solution
As we can see, we get the output as Hello World.

Let us now learn what keyword arguments or named arguments are. We shall refer to them as keyword arguments, henceforth.

Switch to ipython terminal
welcome()

welcome("Hello", "James")

welcome("Hi", name="Guido")
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. loc, linewidth, xy and labels are being called with keyword arguments.

Let us try and understand this better using the welcome 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.

welcome(name="Guido", greet="Hey! ") When no keyword is specified, the arguments are allotted based on their position. So, "Hi" is the value of the argument greet and name is passed the value "Guido".If we type,
welcome(name="Guido", "Hey") When keyword arguments are used, the arguments can be called in any order. And if we call our function as,
This call returns an error that reads, non-keyword arg after keyword arg. Python expects all the keyword to be present towards the end.

That brings us to the end of what we wanted to learn about keyword arguments.

Show Slide 9

built-in functions

Before defining a function of your own, make sure that you check the standard library, for a similar function. Python is popularly called a "Batteries included" language, for the huge library that comes along with it.

Math functions - abs, sin, .... Plot functions - plot, bar, pie ... Boolean functions - and, or, not ...

Show Slide 10

Showing classes of functions in pylab, scipy

Apart from the standard library there are other libraries like pylab, scipy, etc which have a huge collection of functions for scientific purposes.

pylab

plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog

scipy (modules)

fftpack, stats, linalg, ndimage, signal, optimize, integrate

Show Slide 11

Summary slide

This brings us to the end of this tutorial. In this tutorial, we have learnt to,
  1. Define functions with default arguments.
  2. Call functions using keyword arguments.
  3. Use the range of functions available in the Python standard library and the Scientific Computing related packages.


Show Slide 12

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. All arguments of a function cannot have default values. - True or False?

2. The following is a valid function definition. True or False?

def seperator(count=40, char, show=False):
    if show:
        print char * count
    return char * count
  1. When calling a function,
    • the arguments should always be in the order in which they are defined.
    • the arguments can be in any order.
    • only keyword arguments can be in any order, but should be called
      at the beginning.
    • only keyword arguments can be in any order, but should be called at the end.


Show Slide 13

Solution of self assessment questions on slide

And the answers,
  1. False. All arguments of a Python function can have default values.
  2. False. All parameters with default arguments should be defined at the end.
  3. When calling a function,only keyword arguments can be in any order, but should be called at the end.


Show Slide 14

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Pravin1389