Python-3.4.3/C4/Getting-Started-with-Functions/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Welcome to the spoken tutorial on Getting started with functions.
00:06 In this tutorial, we will learn to,

Define a function

Define functions with arguments and

use docstrings.

00:17 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

Python 3.4.3 and

IPython 5.1.0

00:32 To practise this tutorial, you should know how to use tuples.

If not, see the relevant Python tutorials on this website.

00:44 First we will learn about functions.
00:48 A function is a portion of code within a larger program that performs a specific task.
00:55 Functions are useful in reusing the code and eliminate code redundancy.
01:01 Functions are also used to organise our code into manageable blocks.
01:07 Here is the syntax for defining the functions.
01:11 def is the keyword which defines the function name.
01:15 colon is used to mark the end of the function name.
01:19 docstring is the documentation string to describe what the function does.

It is an optional, but recommended.

01:29 Statement makes the function body and it must have 4 indentation level.
01:36 return statement is to return a value from the function and it is also indented by 4 spaces.
01:44 Now we will understand the functions with an example.
01:49 Consider a mathematical function f of x is equal to x squared.
01:55 Here x is a variable. f of x changes when x changes.
02:02 Let us define our function f of x.
02:06 The first line def f of x is used to define the function name and its parameters.
02:13 The second line uses the function parameters to return the required value.
02:19 Let us start ipython.

Open the terminal.

02:24 Type ipython3 and press Enter.
02:29 From here onwards, remember to press the Enter key after typing every command on the terminal.
02:36 Type,

def f inside brackets x colon return x asterisk x

and press the Enter key two times.

02:47 Let us call the f of x with different arguments.
02:51 Type,

f inside brackets 2

f inside brackets 2.5

It returned 4 and 6.25 respectively.

03:05 Now let us see how to write functions without arguments.
03:10 Type the code as shown.

It defines a new function named greet which will print "No function arguments"

03:21 Now we will call the function as,greet open and close parentheses
03:28 Note that it is not mandatory for a function to return values.
03:33 The function greet neither takes any arguments nor returns any value.
03:40 Next we will learn how to comment in a code.
03:44 Documenting/commenting code is a good practice.
03:48 Docstrings are triple quoted comments entered just after the function definition.

It implies what the function does.

03:57 Let us write a function which returns average of two numbers.
04:02 Type the code as shown. and press the Enter key two times.

Comments within triple quotes give a clear explanation about the code.

04:14 Type, avg question mark

Here we can see the docstring of the function avg.

04:23 Now let us pass 3 and 5 as values to the arguments a and b to the function avg.
04:31 Type, avg inside brackets 3 comma 5 We get 4.0 as output.
04:41 Pause the video.Try this exercise and then resume the video.
04:47 Write a function circle which returns the area and perimeter of a circle with given radius r.
04:54 Switch back to the terminal for the solution.
04:58 Type the code as shown. Press the Enter key two times.
05:05 The circle function requires us to return two values.
05:10 A python function can return any number of values in the form of a tuple.
05:16 Let us call the function circle as, a comma p is equal to circle inside brackets 6
05:25 Now Type, print inside brackets a comma p
05:31 We can see the output as area and perimeter of a circle of radius 6.
05:38 This brings us to the end of this tutorial. Let us summarize.
05:44 In this tutorial, we have learnt to,

Define functions in Python

Call a function by specifying the function name

05:54 Write docstrings to a function by putting it as a triple quoted string

Pass parameters to a function

Return values from a function.

06:06 Here are some self assessment questions for you to solve

1. How many arguments can be passed to a Python function?

06:15 2.Write a function to find the area of a rectangle.
06:20 And the answers,

1. Any number of arguments can be passed to a Python function.

06:28 2. We can write a function to find the area of a rectangle as:def rectangle underscore area inside brackets l comma b colon

return l asterisk b

06:42 Please post your timed queries in this forum.
06:46 Please post your general queries on Python in this forum.
06:51 FOSSEE team coordinates the TBC project.
06:55 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website.
07:04 This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Pratik kamble