Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Creating a Function

In python a function is defined using the def keyword:

RUN

Python Function 1
Calling a Function
To call a function, use the function name followed by parenthesis:

RUN

Python Function 2
Arguments
When the function is called, we pass alos a first name, which is used
inside the function to print the full name:

RUN

Python Function 3
Number of Arguments
This function expects 2 arguments and gets 2 arguments:

RUN

Python Function 4
Number of Arguments
This function excepts 2 arguments, but gets only 1:

RUN

Python Function 5
Arbitary Arguments, *args
If the number of arguments is unknown, add a * before the parameter
name:

RUN

Python Function 6
Keyword Arguments
You can also send arguments with the key = value syntax
This way the order of the arguments does not matter

RUN

Python Function 7
Arbitary Keyword Arguments, **kwargs
If the number of the keyword arguments is unknown add a double **
before the parameter name:

RUN

Python Function 8
Default Parameter Value
The following example show how to use a default parameter value.
If we call the function without argument, it uses the default value.

RUN

Python Function 9
Passing a List as an Argument
E.g. if you send a list as an argument, it will still be a list when it reaches
the function:

RUN

Python Function 10
Return Values
To let a function return a value, use the return statement:

RUN

Python Function 11
The pass Statement
Function definitions cannot be empty, but if you for some reason have
a function definition with no content, put in the pass statement to
avoid getting an error.

RUN

Python Function 12
Recursion

RUN

Python Function 13
Phython For Loops
Print each fruit in a fruit list:

RUN

Python For Loops 14


Looping Through a String
Loop through the letters in the word “banana”:

RUN

Python For Loops 15


The break Statement
Exit the loop when x is ”banana”:

RUN

Python For Loops 16


The break Statement

Exit the loop when x is “banana”, but this time the break comes before
the print:

RUN

Python For Loops 17


The continue Statement
Do not print “banana”:

RUN

Python For Loops 18


The range() Function
Using the range() function:

RUN

Python For Loops 19


The range() Statement
Using the start parameter:

RUN

Python For Loops 20


The range() Statement
Increment the sequence with 3 (default is 1):

RUN

Python For Loops 21


Else in For Loops
Print all numbers from 0 to 5, and print a message when the loop has
ended:

RUN

Python For Loops 22


Nested Loops
Print each adjective for every fruit:

RUN

Python For Loops 23


The pass Statement
for loops cannot be empty, but if you for some reason heve a for loop
with no content, put in the pass statement to avoid getting error.

RUN

Python For Loops 24


The while Loop
Print i as long as I is less than 6:

RUN

Python While Loops 25


The break Statement

Exit the loop when is 3:

RUN

Python While Loops 26


The continue Statement
Continue to the next literation if I is 3:

RUN

Python While Loops 27


The else Statement
Print a message once the condition is false:

RUN

Python While Loops 28

You might also like