Python Interactive Guide - Step 3 Functions (1) - Function Basics (1)
- This course, Python Interactive Guide, is designed to help you learn the basics of Python programming through hands-on, interactive examples.
- The “Style Guide” sections introduce clean coding practices, mainly based on PEP8.
- You can run and experiment with every code example.
Feel free to try things out - reloading the page will reset everything.
Starting from this section, we’ll learn about “Step 3 Functions”, which allows us to organize code into reusable components.
Step 3 Functions
As programs grow larger, you’ll find yourself writing the same processes multiple times.
This is where functions come in handy.
By using functions, you can group a series of operations, name them, and call them whenever needed.
3.1. Function Basics
3.1.1. Defining and Calling Functions
Functions are defined as follows:
-
Functions are defined using the
def
keyword -
The function name is followed by parentheses
()
and ends with a colon:
-
The function body is written in an indented block
-
The function naming rules are the same as those for Variable Naming Rules:
- Use letters (
a-z
,A-Z
), numbers, and underscores (_
) - Do not start with a number
- Do not use reserved words (
if
,for
,while
, etc.)
- Use letters (
Defined functions can be called and executed as follows:
- Defined functions are called by writing the function name followed by parentheses
()
- Top-level function definitions should have 2 blank lines before and after them
- For non-top-level functions (like nested functions), use 1 blank line before and after
- You can add blank lines to make logical groupings more clear
- Use function names that clearly indicate their functionality
- In Python, function names typically use lowercase snake_case (words separated by underscores)
Let’s create a program that achieves the same output by creating a function for the separator ("=" * 30
) output shown below:
Example Solution
Next, let’s change the separator from equal (=
) to hyphen (-
).
Example Solution
Using functions offers the following benefits:
- Code Reuse: You don’t need to write the same process multiple times
- Improved Maintainability: When you need to change a process, you only need to modify the function definition
- Improved Readability: Naming a process makes the intention of the code clearer
3.1.2. Local Variables and Global Variables
Variables defined within a function are called local variables.
Conversely, variables defined outside are called global variables.
Local variables do not affect global variables:
The “range of accessibility” that determines where a variable can be accessed from is called scope.
We’ll discuss scope in more detail later.
3.1.3. Return Values (return
Statement)
Functions can return processing results as return values.
Return values are specified using the return
statement.
When a return
statement is executed, the function ends immediately.
Any code after the return
statement won’t be executed.
Using Multiple return
Statements
Multiple return
statements can be used, allowing different return values based on conditions:
Since return
statements end the function’s execution, the above code can be rewritten as follows:
Returning None
Functions without a return
statement, or with a return
statement that doesn’t specify a value, return None
:
Returning Multiple Values Simultaneously
You can return multiple values simultaneously by separating them with commas (,
) in a return
statement:
- This actually returns a tuple
(As we saw in Creating Tuples, tuple parentheses can be omitted) - You can use unpacking to assign the return values to separate variables
In the program above, the width and height of the rectangle are fixed within the function.
To perform calculations for various rectangle patterns, we need to use parameters, which will be introduced in the next section.
Create a function that returns both the minimum and maximum values of a list defined within the function,
then print both values on the screen.