Lernziele dieses Kapitels
- You explain what a variable is and how it works
- You distinguish the 4 most important data types in Python
- You use type conversions (castings) appropriately
- You store the PyBuddy user in a variable
Variables — Labeled Boxes
A variable is like a labeled box in the warehouse of your computer. You write on it what's inside (name), throw something in ("Max"), and can find it later via the label.
In JavaScript, you created variables with let or const. In Python, you don't need any keyword — just name, equals sign, value. Done.
# Python — einfach und direkt
name = "Max"
alter = 16
pi = 3.14159
spieler_aktiv = True
Du kennst bereits JavaScript aus dem JS-Quest. Hier ist der direkte Vergleich:
name = "Max"
alter = 16
let name = "Max";
let alter = 16;
The 4 Data Types You Need to Know
Python recognizes the data type automatically. You don't have to declare anything. But you should know which types exist:
str(String): Text in quotes —"Hallo"int(Integer): Whole numbers —42float(Floating Point): Decimal numbers —3.14bool(Boolean): True/False —TrueorFalse
With type(), you can check at any time which type is inside a variable.
name = "Python"
alter = 30
groesse = 1.75
aktiv = True
print(type(name)) # <class 'str'>
print(type(alter)) # <class 'int'>
print(type(groesse)) # <class 'float'>
print(type(aktiv)) # <class 'bool'>
Type Conversion — When Python Doesn't Play Along
Sometimes you need to convert one type into another. Example: input() always returns a string. If you want to calculate with it, you first have to convert it to int or float.
This is called Type Casting — in Python with int(), float(), str(), or bool().
# input() liefert immer einen String!
alter_text = "16"
# So geht es NICHT:
# alter_text + 5 → Fehler!
# So geht es RICHTIG:
alter_zahl = int(alter_text)
print(alter_zahl + 5) # 21
# Und zurück:
preis = 19.99
preis_text = str(preis)
print("Der Preis ist: " + preis_text)
Du kennst bereits JavaScript aus dem JS-Quest. Hier ist der direkte Vergleich:
zahl = int("16")
text = str(19.99)
let zahl = parseInt("16");
let text = String(19.99);
Variables in Action: Gaming Example
Imagine you're programming a small RPG. Every player has values that change — variables are perfect for exactly that.
# Spieler-Status in einem RPG
spieler_name = "DragonSlayer99"
level = 5
hp = 100
mana = 50.5
ist_online = True
print(f" Spieler: {spieler_name}")
print(f" Level: {level}")
print(f" HP: {hp}")
print(f" Mana: {mana}")
print(f" Online: {ist_online}")
# Level-Up!
level = level + 1
hp = hp + 20
print(f" Level-Up! Du bist jetzt Level {level}!")
Good Names — The Art of Variable Names
In Python, there are conventions for variable names. They help you and others read the code. Here are the most important rules:
- Only lowercase letters, numbers, and underscores:
spieler_name - No numbers at the beginning:
1nameis forbidden - No spaces — use underscores instead:
mein_name - Be descriptive:
alterinstead ofa - No reserved words:
print,if,forare off-limits
This is called snake_case — the Python standard notation.
# Gute Namen
spieler_name = "Max"
aktuelles_level = 42
ist_angemeldet = True
# Schlechte Namen
# x = "Max" → zu kurz
# 1name = "Max" → Zahl am Anfang
# mein Name = "Max" → Leerzeichen!
# print = 5 → reserviertes Wort!
Warm-Up: Variable Quiz
Write a program variablen.py that defines 4 variables with different data types and outputs their type with type().
Hinweis: name = "Python"
version = 3.11
ist_cool = True
anteil = 0.85
print(type(name))
print(type(version))
print(type(ist_cool))
print(type(anteil))
name = "Python"
version = 3.11
ist_cool = True
anteil = 0.85
print(type(name))
print(type(version))
print(type(ist_cool))
print(type(anteil))
Challenge: Currency Converter
Write a currency converter that converts euros to dollars. Use float(input()) for the input. Exchange rate: 1€ = 1.08$
Hinweis: euro = float(input("Betrag in Euro: "))
dollar = euro * 1.08
print(f"{euro}€ = {dollar:.2f}$")
euro = float(input("Betrag in Euro: "))
dollar = euro * 1.08
print(f"{euro}€ = {dollar:.2f}$")
PyBuddy Checkpoint: User Profile
Expand PyBuddy: Store the user's name in a variable and output a personalized greeting. This is the first building block of your assistant!
Hinweis: # pybuddy/main.py
nutzer_name = "Max"
print(f" Hallo {nutzer_name}!")
print("Ich bin PyBuddy, dein digitaler Assistent.")
print(f"Bereit für deine Befehle, {nutzer_name}!")
# pybuddy/main.py
nutzer_name = "Max"
print(f" Hallo {nutzer_name}!")
print("Ich bin PyBuddy, dein digitaler Assistent.")
print(f"Bereit für deine Befehle, {nutzer_name}!")
In The Sims, every character has variables for hunger, energy, fun, and social. From a programming perspective, it's nothing other than hunger = 80 — and when your Sim eats, the value is increased. You're currently programming the basics for your own game!
Zusammenfassung
- Variables = labeled boxes for data
- The 4 main types: str, int, float, bool
- Type conversion with int(), float(), str(), bool()
- Variables change during the program (level up!)
- Snake_case: spieler_name, aktuelles_level