The Python tutorials are written as Jupyter notebooks and run directly in Google Colab—a hosted notebook environment that requires no setup. Click the Run in Google Colab button.
Colab link - Open colab
Python Functions - A function is a block of code which only runs when it is called.**
**You can pass data, known as parameters, into a function(Function_Name(Parameters)).**
**A function can return a value if specified.**
Answers to the exercises are given at the end of the notebook
**Creating a Function:** In Python a function is defined using the def keyword.
def my_function():
print("Hello from a function")
**Parameters:** Information can be passed to functions as parameter.
Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
The following example has a function with one parameter (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name.
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
**NOTE:** If we call the function without parameter, it uses the default value.
def my_function(country = "Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
**Return Statement** When the function results in some value and that value has to be stored in a variable or needs to be sent back or returned for further operation to the main algorithm, return statement is used.
def times(x,y):
z = x*y
return z
The above defined times( ) function accepts two arguements and return the variable z which contains the result of the product of the two arguements
c = times(4,5)
print c
20
The z value is stored in variable c and can be used for further operations.
Instead of declaring another variable the entire statement itself can be used in the return statement as shown.
def times(x,y):
return x*y # This multiplies the two input arguments and returns it.
c = times(4,5)
print c
20
Since the times( ) is now defined, we can document it as shown above. This document is returned whenever times( ) function is called under help( ) function.
**Implicit arguments** When an argument of a function is common in majority of the cases or it is "implicit" this concept is used.
def implicitadd(x,y=3):
return x+y
**implicitadd( )** is a function accepts two arguments but most of the times the first argument needs to be added just by 3.
Hence the second argument is assigned the value 3. Here the second argument is implicit.
implicitadd(4) # if the second argument is not defined when calling the implicitadd( ) function then it considered as 3.
But if the second argument is specified then this value overrides the implicit value assigned to the argument
implicitadd(4,4) # if the second argument is specified then this value overrides the implicit value assigned to the argument
## ** Arbitrary arguments** If the number of arguments that is to be accepted by a function is not known then a asterisk symbol is used before the argument.
def add_n(*args):
res = 0
reslist = []
for i in args:
reslist.append(i)
print reslist
return sum(reslist)
The above function accepts any number of arguments, defines a list and appends all the arguments into that list and return the sum of all the arguments.
add_n(1,2,3,4,5)