Python — Grundlagen
Kapitel 2 · Theorie

Syntax & Datentypen

Variablen — keine Deklaration nötig

name = "Anna"            # str
alter = 28               # int
groesse = 1.72           # float
istStudent = True        # bool
hobbys = None            # NoneType

Operatoren

KategorieOperatorenBeispiel
Arithmetik+ - * / // % **7 // 2 = 3, 2 ** 10 = 1024
Vergleich== != < > <= >=5 == 5 → True
Logikand or notTrue and not False → True
Mitgliedschaftin, not in3 in [1, 2, 3] → True

Strings

name = "Mohammad Ali"
print(f"Hallo {name}, du bist {len(name)} Zeichen lang")
# Slicing
print(name[0:8])      # "Mohammad"
print(name[::-1])     # "ilA dammahoM"
f-StringsSeit Python 3.6: f"..." mit {ausdruck} ist die schnellste und lesbarste Art, Strings zusammenzubauen. Ablöser von %-Format und .format().
Zurück zu Informatik