Skip to content

Kapitel 5: Operatoren und Ausdrücke

🎯 Lernziele

In diesem Kapitel lernst du:

  • Arithmetische Operatoren (+, -, *, /, ...)
  • Zuweisungsoperatoren (+=, -=, ...)
  • Vergleichsoperatoren (==, !=, >, <, ...)
  • Logische Operatoren (and, or, not)
  • Operatorpriorität

5.1 Arithmetische Operatoren

➕ Grundlegende Operatoren

python
a = 10
b = 3

print(a + b)   # 13 (Addition)
print(a - b)   # 7 (Subtraktion)
print(a * b)   # 30 (Multiplikation)
print(a / b)   # 3.3333... (Division - Float)
print(a // b)  # 3 (Ganzzahldivision)
print(a % b)   # 1 (Modulo - Rest)
print(a ** b)  # 1000 (Potenz - 10^3)

📏 Division vs. Ganzzahldivision

python
# Normale Division (/)
print(10 / 3)   # 3.3333333333333335

# Ganzzahldivision (//)
print(10 // 3)  # 3 (Nachkommastellen abgeschnitten)

# Negativer Wert
print(-10 // 3)  # -4 (abgerundet!)

🔢 Modulo-Operator (%)

Der Modulo-Operator gibt den Rest einer Division zurück:

python
# Rest einer Division
print(10 % 3)   # 1 (10 = 3*3 + 1)
print(17 % 5)   # 2 (17 = 5*3 + 2)

# Praxis: Prüfen, ob eine Zahl gerade ist
zahl = 14
if zahl % 2 == 0:
    print("Gerade Zahl")
else:
    print("Ungerade Zahl")

💡 Potenzierung (**)

python
# Potenz
print(2 ** 3)   # 8 (2^3)
print(5 ** 2)   # 25 (5^2)

# Wurzel ziehen (mit 0.5 Potenz)
print(9 ** 0.5)  # 3.0 (Wurzel aus 9)

🛠️ Praxisbeispiel: Einfacher Taschenrechner

python
print("=== Einfacher Taschenrechner ===")

a = float(input("Gib die erste Zahl ein: "))
op = input("Gib den Operator ein (+, -, *, /): ")
b = float(input("Gib die zweite Zahl ein: "))

if op == "+":
    print(f"Ergebnis: {a + b}")
elif op == "-":
    print(f"Ergebnis: {a - b}")
elif op == "*":
    print(f"Ergebnis: {a * b}")
elif op == "/":
    if b != 0:
        print(f"Ergebnis: {a / b}")
    else:
        print("Fehler: Division durch 0!")
else:
    print("Ungültiger Operator!")

5.2 Zuweisungsoperatoren

✏️ Einfache und zusammengesetzte Zuweisung

python
# Einfache Zuweisung
x = 10

# Zusammengesetzte Zuweisung
x += 5   # x = x + 5 (x ist jetzt 15)
x -= 3   # x = x - 3 (x ist jetzt 12)
x *= 2   # x = x * 2 (x ist jetzt 24)
x /= 4   # x = x / 4 (x ist jetzt 6.0)
x //= 2  # x = x // 2 (x ist jetzt 3.0)
x %= 2   # x = x % 2 (x ist jetzt 1.0)
x **= 3  # x = x ** 3 (x ist jetzt 1.0)

📊 Beispiel

python
# Zahler erhöhen
counter = 0
counter += 1  # counter ist jetzt 1
counter += 1  # counter ist jetzt 2

# Rabatt berechnen
preis = 100
rabatt = 20
preis -= rabatt  # preis ist jetzt 80

5.3 Vergleichsoperatoren

⚖️ Vergleichsoperatoren (geben True/False zurück)

python
a = 10
b = 5

print(a == b)  # False (gleich)
print(a != b)  # True (ungleich)
print(a > b)   # True (größer)
print(a < b)   # False (kleiner)
print(a >= b)  # True (größer oder gleich)
print(a <= b)  # False (kleiner oder gleich)

⚠️ Wichtig: == vs. =

python
# == vergleicht (Vergleich)
x = 10
print(x == 10)  # True

# = weist zu (Zuweisung)
x = 20  # Weist 20 x zu

🎯 Praxisbeispiel

python
alter = int(input("Wie alt bist du? "))

if alter >= 18:
    print("Du bist volljährig!")
else:
    print("Du bist minderjährig.")

5.4 Logische Operatoren

🔗 Logische Operatoren

OperatorBedeutungBeispielErgebnis
andUND(5 > 3) and (10 > 5)True
orODER(5 > 3) or (5 > 10)True
notNICHTnot (5 > 3)False

📋 Wahrheitstabellen

AND (und):

  • True and TrueTrue
  • True and FalseFalse
  • False and TrueFalse
  • False and FalseFalse

OR (oder):

  • True or TrueTrue
  • True or FalseTrue
  • False or TrueTrue
  • False or FalseFalse

NOT (nicht):

  • not TrueFalse
  • not FalseTrue

💡 Beispiele

python
alter = 25
gehalt = 3000

# AND - Beide Bedingungen müssen wahr sein
if alter >= 18 and gehalt >= 2000:
    print("Kredit möglich")

# OR - Mindestens eine Bedingung muss wahr sein
if alter < 18 or gehalt < 2000:
    print("Kredit nicht möglich")

# NOT - Umkehren des Wahrheitswerts
ist_regen = False
if not ist_regen:
    print("Draußen spielen")

🎯 Praxisbeispiel: Altersüberprüfung

python
alter = int(input("Wie alt bist du? "))

if alter >= 0 and alter <= 120:
    print("Gültiges Alter")
else:
    print("Ungültiges Alter!")

5.5 Operatorpriorität

📐 Reihenfolge der Auswertung

Wie in der Mathematik: Punkt- vor Strichrechnung!

Reihenfolge (von hoch zu niedrig):

  1. () (Klammern)
  2. ** (Potenz)
  3. *, /, //, % (Multiplikation/Division)
  4. +, - (Addition/Subtraktion)
  5. Vergleichsoperatoren (==, !=, >, <)
  6. Logische Operatoren (not, and, or)

🧮 Beispiele

python
# Ohne Klammern
print(2 + 3 * 4)  # 14 (nicht 20!)

# Mit Klammern
print((2 + 3) * 4)  # 20

# Komplexer Ausdruck
ergebnis = 10 + 5 * 2 ** 3  # 10 + 5 * 8 = 10 + 40 = 50
print(ergebnis)  # 50

💡 Tipp: Verwende Klammern!

python
# Schwer zu lesen
if alter >= 18 and gehalt >= 2000 or bonus >= 500:
    pass

# Besser: Klammern setzen
if (alter >= 18) and (gehalt >= 2000 or bonus >= 500):
    pass

📝 Zusammenfassung

In diesem Kapitel hast du gelernt:

  • ✅ Arithmetische Operatoren (+, -, *, /, //, %, **)
  • ✅ Zuweisungsoperatoren (=, +=, -=, ...)
  • ✅ Vergleichsoperatoren (==, !=, >, <, >=, <=)
  • ✅ Logische Operatoren (and, or, not)
  • ✅ Operatorpriorität und Klammerregeln

🎯 Übung

  1. Schreibe einen Taschenrechner mit allen Grundrechenarten
  2. Prüfe, ob eine Zahl durch 3 und 5 teilbar ist
  3. Berechne den Flächeninhalt eines Kreises (A = πr²)
  4. Prüfe, ob eine Person für eine Versicherung qualifiziert ist (Alter 18-65 UND nicht vorbestraft)

⏭️ Nächstes Kapitel

In Kapitel 6 lernen wir Bedingte Anweisungen (if-elif-else)!

Frei für alle Anfänger