BeginnerLesson 3 of 12
Conditions: if, elif, else – Python
Making decisions in code.
With if you run code only when a condition is true. The indentation (4 spaces) determines what belongs to the block.
punkte = 73
if punkte >= 90:
note = "very good"
elif punkte >= 70:
note = "good"
else:
note = "needs improvement"
print(note)Comparisons: ==, !=, <, >, <=, >=. You can combine them with and, or, not. Chained comparisons are also possible:
x = 15
print(10 < x < 20) # True
print(x % 2 == 0 or x > 10) # True
alter = 20
status = "adult" if alter >= 18 else "minor"
print(status)TipEmpty collections,
0, "" and None count as false: if not liste: is an elegant check for "empty".