DE EN

Variables & Data Types

How Python stores and processes data, and why it's so much easier than you think.

45 Min Leicht

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
# Python — einfach und direkt
name = "Max"
alter = 16
pi = 3.14159
spieler_aktiv = True
Python vs. JavaScript — Das kennst du schon!

Du kennst bereits JavaScript aus dem JS-Quest. Hier ist der direkte Vergleich:

Python
name = "Max"
alter = 16
JavaScript
let name = "Max";
let alter = 16;
Merke: Python doesn't need `let`, `const`, or `var`. Simply name = value. Types are recognized automatically!

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 — 42
  • float (Floating Point): Decimal numbers — 3.14
  • bool (Boolean): True/False — True or False

With type(), you can check at any time which type is inside a variable.

Python
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'>
Ausgabe
<class 'str'> <class 'int'> <class 'float'> <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().

Python
# 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)
Ausgabe
21 Der Preis ist: 19.99
Python vs. JavaScript — Das kennst du schon!

Du kennst bereits JavaScript aus dem JS-Quest. Hier ist der direkte Vergleich:

Python
zahl = int("16")
text = str(19.99)
JavaScript
let zahl = parseInt("16");
let text = String(19.99);
Merke: Python uses `int()` and `str()` directly as functions — in JavaScript, you need `parseInt()` or `String()`.

Variables in Action: Gaming Example

Imagine you're programming a small RPG. Every player has values that change — variables are perfect for exactly that.

Python
# 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}!")
Ausgabe
Spieler: DragonSlayer99 Level: 5 HP: 100 Mana: 50.5 Online: True Level-Up! Du bist jetzt Level 6!

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: 1name is forbidden
  • No spaces — use underscores instead: mein_name
  • Be descriptive: alter instead of a
  • No reserved words: print, if, for are off-limits

This is called snake_case — the Python standard notation.

Python
#  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))

Solution
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}$")

Solution
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}!")

Solution
# 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}!")
Learning Break

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