Übung 3 – „Hallo Weingarten“
Ziel dieser Übung
Sie schreiben Ihr erstes Python-Programm, speichern es als Datei und führen es im Terminal aus. Dabei lernen Sie Anweisungen und Kommentare kennen.
Tipp: Jeder Code lässt sich mit „Kopieren“ übernehmen. Zum Lernen ist es aber besser, ihn selbst abzutippen – so üben Sie Schreibweise und Umgang mit dem Terminal.
1. Skript schreiben, speichern, ausführen
- Legen Sie in VS Code im Ordner
uebungen die Datei hallo.py an (Endung .py!). - Schreiben Sie diese Zeile hinein und speichern Sie (Strg/Cmd + S):
print("Hallo, Weingarten!")
- Wechseln Sie im Terminal in den Ordner und starten Sie das Programm:
cd uebungen
python hallo.py
Hallo, Weingarten!
Mac: python3 hallo.py. Alternativ startet der ▷-Knopf oben rechts in VS Code die Datei.
2. Anweisungen
Jede Zeile wie print(…) ist eine Anweisung: ein Befehl, den Python ausführt – von oben nach unten, eine nach der anderen. Anweisungen können z. B.
- etwas auf der Konsole ausgeben,
- rechnen,
- Werte in Variablen speichern,
- den Ablauf steuern (Bedingungen, Schleifen – kommt später).
3. Kommentare
Alles hinter einem # ist ein Kommentar. Python ignoriert ihn – er ist für Menschen gedacht, die den Code lesen.
# Mein erstes Programm
# Kommentare werden nicht ausgeführt.
print("Hallo, Weingarten!") # gibt einen Gruß aus
Tipp: Kommentieren Sie am Anfang lieber zu viel als zu wenig – später schreiben Sie Kommentare vor allem dort, wo der Code nicht selbsterklärend ist.
Aufgabe · Ihr eigenes Programm
- Erstellen Sie
ueber_mich.py: Das Programm gibt drei Zeilen über Sie aus (Name, Studiengang, ein Hobby). - Schreiben Sie über jede Zeile einen Kommentar, was sie tut.
- Führen Sie das Programm im Terminal aus.
Lösung anzeigen
# Name ausgeben
print("Ich heiße Alex.")
# Studiengang ausgeben
print("Ich studiere Medienbildung.")
# Hobby ausgeben
print("In meiner Freizeit spiele ich Gitarre.")
Weiter: Übung 4 – Print und Zahlen →
Exercise 3 – “Hello Weingarten”
Goal of this exercise
You write your first Python program, save it as a file and run it in the terminal. Along the way you learn about statements and comments.
Tip: every code block can be copied with “Copy”. For learning, it is better to type it yourself – that is how you practise syntax and using the terminal.
1. Write, save and run a script
- In VS Code, create the file
hallo.py in the folder uebungen (extension .py!). - Write this line into it and save (Ctrl/Cmd + S):
print("Hallo, Weingarten!")
- In the terminal, go to the folder and run the program:
cd uebungen
python hallo.py
Hallo, Weingarten!
Mac: python3 hallo.py. Alternatively, the ▷ button at the top right in VS Code runs the file.
2. Statements
Every line such as print(…) is a statement: a command that Python executes – from top to bottom, one after another. Statements can, for example,
- output something on the console,
- calculate,
- store values in variables,
- control the flow (conditions, loops – coming later).
3. Comments
Everything after a # is a comment. Python ignores it – it is meant for people reading the code.
# Mein erstes Programm
# Kommentare werden nicht ausgeführt.
print("Hallo, Weingarten!") # gibt einen Gruß aus
Tip: at the beginning, comment too much rather than too little – later you mainly write comments where the code is not self-explanatory.
Task · Your own program
- Create
ueber_mich.py: the program prints three lines about you (name, degree programme, a hobby). - Write a comment above each line saying what it does.
- Run the program in the terminal.
Show solution
# Name ausgeben
print("Ich heiße Alex.")
# Studiengang ausgeben
print("Ich studiere Medienbildung.")
# Hobby ausgeben
print("In meiner Freizeit spiele ich Gitarre.")
Next: Exercise 4 – Print and numbers →