Variables
Module 1 — Output with print()
Without print() the console remains empty.
What is it about?
Python runs your code from top to bottom but shows nothing
on the console unless you explicitly request it. There is a built-in function for this print(). Everything that is between the round brackets ends up in the output.
Run the example and watch what appears in the console. You can change the code and run it again at any time.
Example 1.1 — Simple output
Loading Python…Tests
Output multiple values at once
In print() you can multiple values separate with commas. Python then inserts a space between the values — this saves you the hassle of assembling strings.
Example 1.2 — multiple values in one output
Loading Python…Tests
Module 2 — Variables
Name, store and reuse values.
What is a variable?
One Variable is a named location in memory. You put them with that References = on — the name on the left, the value on the right. The stored value is output again print() from module 1.
A variable can store a number — how alter = 15 or
preis = 3.5 — or a text in quotation marks, like
name = "Max".
Example 2.1 — Creating and outputting variables
Loading Python…Tests
Variables can be overwritten
You can assign a new value to a variable at any time - the old one is then gone. The variable itself may also appear on the right side of an assignment (x = x + 1): Python first calculates the right side and then writes the result into the variable.
Example 2.2 — Overwrite variable
Loading Python…Tests
Swap two values
If you want to swap the contents of two variables, you need one
Auxiliary variable as a buffer. Otherwise you will overwrite the first value before you have a chance to save it. The order is:
x cache → x with y overwrite →
y put from the buffer.
Example 2.3 — swap two values
Loading Python…Tests
TipName conventions
Select descriptive names in English or German — that is
area = 10 or flaeche = 10 the a = 10. In Python it is snake case usual (max_iterationen,
mittlere_temperatur). Avoid special characters (ä → ae, ö → oe, ü → ue, ß → ss).
Module 3 — Calculating with variables
Add values, multiply and save the result.
The most important arithmetic operators
You calculate numbers in Python the way you know from a calculator:
+Addition-Subtraction*Multiplication/Division (the result is always a decimal number)
Python follows the usual order of operations; parentheses let you enforce a different order.
Example 3.1 — Calculating with numbers
Loading Python…Tests
Store results in a variable
The exciting thing about variables: you can convert the result of a calculation into one
new Save variables and continue working with them later. This is exactly what you need, for example, for the total price preis and anzahl.
Example 3.2 — Save result to a variable
Loading Python…Tests
Rounding and decimal places
Rounding and formatting are different: round(wert, n) rounds the numeric value to n decimal places but does not add zeros. An f-string with .2f displays exactly two decimal places. Decimal places are not significant figures.
Module 4 — Using variables in the text
From “print(alter)" becomes "I am 15 years old."
What is an f-string?
A f-string is a text with a small f before the quotation mark. In curly brackets { } You can then insert variables - or even entire calculations - directly. This allows you to build fluid sentences without having to laboriously glue individual pieces together.
Example 4.1 - Incorporating a variable into the sentence
Loading Python…Tests
Multiple variables in one sentence
You can have as many as you want within an f-string { }-Use wildcards. This way you get clearly readable sentences First name, last name and
City — or a finished invoice in one line.
Example 4.2 — combining multiple variables
Loading Python…Tests
Ready for the programming task?
You now know the most important building blocks for solving the six exercises: Output with print(), creating and overwriting variables, calculating with variables and inserting variables in f-strings.
Module 5 — Input with input()
Read values at runtime instead of writing them hard into the code.
What does input()?
input() read one line from the keyboard and delivers it as
Text (str). For calculations, you convert the text into a number - with int() (integer) or float() (decimal number):
name = input() # Text, z.B. "Anna" alter = int(input()) # "20" -> 20 (ganze Zahl) preis = float(input()) # "3.5" -> 3.5 (Kommazahl)
Every call from input() reads that next line. So you read three values with three input()-Call one after the other.
TipIn the exercises
Most of the programming tasks in this module (parts 1–5) read their values
input() one: the test feeds the inputs automatically and compare your output character by character. Therefore, type exactly what is requested - no additional texts such as "Please enter name".
Not everything has to be read in: at part 6 (swap) you set the values yourself in the code — there is none there input(), variables and output are checked directly.
Sources & further literature
Where the knowledge imparted here is officially documented.
Official Python documentation
- Python tutorial — 3. An Informal Introduction to Python: docs.python.org/3/tutorial/introduction.html
- Python Library — Built-in functions (
print): docs.python.org/3/library/functions.html#print - Python Reference — Assignment statements: docs.python.org/3/reference/simple_stmts.html#assignment-statements
- PEP 498 — Literal string interpolation (f strings): peps.python.org/pep-0498
Tutorials & Manuals
- Real Python — Variables in Python: realpython.com/python variables
- Real Python — Python's F-String for String Interpolation: realpython.com/python-f-strings
- Theis, Thomas: Getting started with Python. 7. Edition, Rheinwerk Verlag, 2023 — Chapter on variables and output.
- Klein, Bernd: Introduction to Python 3. 4. Edition, Hanser Verlag, 2021 — Section “Simple data types and variables”.