Professional Documents
Culture Documents
Creating A Function: in Python A Function Is Defined Using The Keyword
Creating A Function: in Python A Function Is Defined Using The 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
RUN
RUN
Exit the loop when x is “banana”, but this time the break comes before
the print:
RUN
RUN
RUN
RUN
RUN
RUN
RUN
RUN
RUN
RUN
RUN
RUN