Combined exercises
Module 1 — The typical workflow
Function above, called in the main block, returned in a variable.
Requirements
In this module, constructs from previous learning units are used together: Functions (Definition with def and return), the idiom if __name__ == "__main__":, as well
Conditions and Grinding. If you are missing something in a component, take another look in the respective module.
The recurring pattern
- Function top define:
def name(parameter):, calculate in the body, at the endreturn ergebnis. - In the block
if __name__ == "__main__":Create inputs as variables. - Call function and the Return in a variable save.
- First after the result with the function call
print()output.
This pattern makes the code reusable and easy to test — the function doesn't "know" where its input comes from.
In most parts of the programming task, you read the entries in the main block
input() (introduced in the Variables module) and output only the result — the check automatically feeds in the input and compares your output character by character. However, for two parts (welcome and login) it says function itself in the center: you don't read anything there, but define the function and call it with values you choose - the runner then checks the function directly. The examples here use fixed values so that the Run button runs them directly.
Example 1.1 — Define a function, call it in the main block
Loading Python…Tests
Module 2 — Conditions within a function
A function returns different values depending on the input.
Multiple return-Pfade
A function can have several return-Instructions included. It is up to them to decide which of these will be carried out if-branching. As soon as a
return is executed, the function is finished.
def begruessung(alter):
if alter < 18:
return "Hallo Kind"
else:
return "Hallo Erwachsener"
Example 2.1 — Function with if/else and return
Loading Python…Tests
Module 3 — Loops inside a function
Build accumulator patterns and lists.
Accumulate a value (while)
As with those while-exercises: an auxiliary variable outside the loop, inside it is built up step by step. After You pass the result to the loop return back - not with print.
Example 3.1 — Sum in a function
Loading Python…Tests
Build a list from a list (for)
The accumulator pattern also works for strings or lists. Create an empty list before the loop and use .append(...) Enter values and return the finished list.
Example 3.2 — even/odd via for + if
Loading Python…Tests
Module 4 — More than one return value
Functions can return multiple values at the same time.
Tuple-Return
Do you write in return If multiple values are separated by commas, Python returns them as Tuple back. In the main block you can distribute them directly to several variables - this is called Tuple-Unpacking.
def minmax(zahlen):
return min(zahlen), max(zahlen)
kleinster, groesster = minmax([4, 7, 2, 9])
Example 4.1 — Tuple return and unpacking
Loading Python…Tests
TipOne function, many calls
The strength of features becomes apparent as soon as you use them multiple with different values. That's exactly what they're there for: define once, use as often as you like.
Example 4.2 — Call a function multiple times with different values
Loading Python…Tests
Sources & further literature
Where the knowledge imparted here is officially documented.
Official Python documentation
- Python tutorial — 4. More Control Flow Tools: docs.python.org/3/tutorial/controlflow.html
- Python tutorial — 4.7. Defining functions: docs.python.org/3/tutorial/controlflow.html#defining-functions
- Python tutorial — 5.1. More on Lists: docs.python.org/3/tutorial/datastructures.html#more-on-lists
- PEP 8 — Style Guide for Python Code: peps.python.org/pep-0008 — as soon as functions, conditions and loops come together, it's worth taking a look at the style guide.
Tutorials & Manuals
- Real Python — Conditional Statements in Python: realpython.com/python-conditional-statements
- Real Python — Defining Your Own Python Functions: realpython.com/defining-your-own-python-function
- Theis, Thomas: Getting started with Python. 7. Edition, Rheinwerk Verlag, 2023 — Chapter on functions and control structures.
- Klein, Bernd: Introduction to Python 3. 4. Edition, Hanser Verlag, 2021 — “Functions” section.