Save / load workspace

Save your lab results on this server for 48 hours. Use the token to continue on another device. Anyone who knows the token can open your workspace.

Save workspace

A name is required when you first save and is then permanently bound to this workspace. Task values, entries and progress are saved together.

Load workspace

New workspace

Start with empty results and a name of your choice. Previously saved tokens remain valid until they expire. Unsaved changes in this tab will be discarded.

Saving also transfers any included measurement files and analysis videos to this server. Running simulations are saved as a set of results and displayed in a paused state when loaded.

Main block (`if __name__ == "main"`)

Module 1 — What is if __name__ == "__main__"?

A switch that distinguishes between "executed directly" and "imported".

Definition

Every Python file has a built-in variable __name__. Will the file direct executed (python meine_datei.py), contains __name__ the value "__main__". However, the file is from another file imported, contains __name__ the module name.

With the check

if __name__ == "__main__":
    <Skript-Code>

you separate cleanly between Library use (Functions are imported) and Script use (the program should start). If someone imports your file, the main block runs not — but the functions are available.

Example 1.1 — Output __name__

Loading Python…
Output

    

Module 2 — Building a clean file

Functions above, process in the main block.

Recommended layout

A well-structured Python file consists of two parts:

  • Top: all def-Functions — they define, what the program can do.
  • Below the if __name__ == "__main__":-Block: the specific order of calls — what should be done this time.

Variables are created in the main block, the functions are called with these variables and the returns are in new Variables saved.

Example 2.1 — Function above, call below

Loading Python…
Output

    

Module 3 — Calls, return values and variables

The recurring pattern in the main block.

Model

In the main block always applies:

  1. Inputs into variables first save (e.g. B. name = "Mia").
  2. Function with these variables call.
  3. Return in a new variable caught (e.g. B. text = hallo(name)).
  4. Only then print() call.

This means that intermediate results can be used later and the code remains clear.

TipIn the exercises

With most exercises you read the input values in the main block input() (as introduced in the “Variables” module). The test feeds these values automatic and compare your output character by character - only output what is required.

With two exercises (part 4 and part 6) you set the values against it yourself in the main block — without input(). The focus there is on defining the function, and the test calls your function directly with multiple values.

Example 3.1 — input, call, return

Loading Python…
Output

    

Multiple return values

A function can be included return a, b return multiple values at once. On the call side they are distributed across two variables — this is called Tupel unpacking.

Example 3.2 — two returns on two variables

Loading Python…
Output

    

Module 4 — Why the check at all?

What happens without it? if __name__ == "__main__"?

The problem

Do you write function calls and print() simple direct into the file (without main block), they will also when importing executed. That's almost never what you want — you just wanted to use the feature.

With the main block it is clear: “These lines only run if I start the file directly”.

Example 4.1 — direct execution with main block

Loading Python…
Output

    

TipRule of thumb

The following applies throughout this exercisesseries: Define function above, in the main block the values provide (per input() read or set yourself), the function call and the result output. So you don't have to read everything in - sometimes you just set the values yourself.

Sources & further literature

Where the knowledge imparted here is officially documented.

Official Python documentation

Tutorials & Manuals

  • Real Python — Defining Main Functions in Python: realpython.com/python-main-function
  • Theis, Thomas: Getting started with Python. 7. Edition, Rheinwerk Verlag, 2023 — Section on modules and scripts.
  • Klein, Bernd: Introduction to Python 3. 4. Edition, Hanser Verlag, 2021 — Chapter on modules.