213 lines
3.2 KiB
Python
213 lines
3.2 KiB
Python
|
# %% [markdown]
|
||
|
# # Kontrollstrukturen und Vergleichsoperatoren
|
||
|
|
||
|
# %% [markdown]
|
||
|
# ## Verzweigung (Branching)
|
||
|
|
||
|
# %%
|
||
|
# if / else / elif
|
||
|
|
||
|
alter = -1
|
||
|
|
||
|
if alter >= 16:
|
||
|
print("Nicht vergessen: am 9.6.2024 ist Europa-Wahl!")
|
||
|
elif alter > 0:
|
||
|
print("Schön, dass du geboren bist!")
|
||
|
else:
|
||
|
print("Hmm, ob das so stimmt?")
|
||
|
|
||
|
# %%
|
||
|
richtung = "s"
|
||
|
|
||
|
if richtung == "n":
|
||
|
print("Auf in den hohen Norden!")
|
||
|
elif richtung == "s":
|
||
|
print("Nach Süden, nach Süden!")
|
||
|
elif richtung == "w":
|
||
|
print("Go west!")
|
||
|
elif richtung == "o":
|
||
|
print("Bin schon da.")
|
||
|
|
||
|
|
||
|
# %%
|
||
|
# match
|
||
|
|
||
|
# richtung = input("Wo soll's denn lang gehen? ")
|
||
|
richtung = "n"
|
||
|
|
||
|
match richtung:
|
||
|
case "n" | "norden":
|
||
|
print("Auf in den hohen Norden!")
|
||
|
case "s" | "süden":
|
||
|
print("Nach Süden, nach Süden!")
|
||
|
case "w" | "westen":
|
||
|
print("Go west!")
|
||
|
case "o" | "osten":
|
||
|
print("Bin schon da.")
|
||
|
case _:
|
||
|
print("Das Glück liegt manchmal abseits des direkten Wegs!")
|
||
|
|
||
|
# %% [markdown]
|
||
|
# ## Schleifen
|
||
|
|
||
|
# %%
|
||
|
# for
|
||
|
|
||
|
for i in range(5):
|
||
|
print("Schleifendurchlauf Nr. ", i)
|
||
|
|
||
|
|
||
|
# %%
|
||
|
result = 0
|
||
|
for i in range(1, 11):
|
||
|
result += i
|
||
|
print("Summe der Zahlen 1-10:", result)
|
||
|
|
||
|
# %%
|
||
|
for i in (2, 4, 6, 8, 9, 10):
|
||
|
if i % 2:
|
||
|
print(i, "passt nicht in die Reihe")
|
||
|
|
||
|
# %%
|
||
|
# while
|
||
|
|
||
|
i = 10
|
||
|
while i > 0: # or: while True:
|
||
|
print(i, "... ", end="")
|
||
|
i -= 1
|
||
|
print("Lift off!")
|
||
|
|
||
|
# %%
|
||
|
# continue
|
||
|
|
||
|
for i in range(10, 20):
|
||
|
if i == 13:
|
||
|
continue
|
||
|
print(i)
|
||
|
|
||
|
# %%
|
||
|
# break
|
||
|
|
||
|
i = 0
|
||
|
result = 0
|
||
|
maximum = 10_000
|
||
|
|
||
|
while True:
|
||
|
result += i
|
||
|
|
||
|
if result >= maximum:
|
||
|
print("Iteration: ", i+1)
|
||
|
break
|
||
|
|
||
|
i += 1
|
||
|
|
||
|
|
||
|
# %%
|
||
|
alphabet = "abcdefghifklmnopqrstuvwxyz"
|
||
|
buchstabe = "q"
|
||
|
i = 0
|
||
|
for char in alphabet:
|
||
|
i += 1
|
||
|
if char == buchstabe:
|
||
|
print(buchstabe, "ist Buchstabe Nr.", i, "des dt. Alphabets.")
|
||
|
break
|
||
|
|
||
|
|
||
|
# %%
|
||
|
# einfacher:
|
||
|
|
||
|
for i, char in enumerate(alphabet):
|
||
|
if char == buchstabe:
|
||
|
print(buchstabe, "ist Buchstabe Nr.", i + 1, "des dt. Alphabets.")
|
||
|
break
|
||
|
|
||
|
# %% [markdown]
|
||
|
# # Funktionen
|
||
|
|
||
|
# %%
|
||
|
# Aufruf eingebauter Funktionen
|
||
|
|
||
|
x = pow(2, 8)
|
||
|
y = max(x, 100)
|
||
|
z = min(max(0, x), 100)
|
||
|
print(x, y, z)
|
||
|
|
||
|
# %%
|
||
|
# optionale Parameter
|
||
|
|
||
|
print()
|
||
|
print("")
|
||
|
|
||
|
# %%
|
||
|
# eigene Funktionen
|
||
|
|
||
|
def add(a, b):
|
||
|
return a + b
|
||
|
|
||
|
print(add(2, 3))
|
||
|
|
||
|
# %%
|
||
|
# Keyword arguments
|
||
|
|
||
|
print(add(a=5, b=10))
|
||
|
print(add(4, b=20))
|
||
|
|
||
|
# %%
|
||
|
# Das geht nicht:
|
||
|
print(add(a=6, 10))
|
||
|
|
||
|
# %% [markdown]
|
||
|
# # Vergleichsausdrücke
|
||
|
|
||
|
# %%
|
||
|
a = 10
|
||
|
b = 50
|
||
|
c = 2
|
||
|
d = 5 * a
|
||
|
|
||
|
print(a == b)
|
||
|
print(a != d)
|
||
|
print(a > b)
|
||
|
print(a > c)
|
||
|
print(a >= c)
|
||
|
print(c > a)
|
||
|
print(c < b)
|
||
|
print(c >= 2)
|
||
|
|
||
|
# %%
|
||
|
# Negation
|
||
|
|
||
|
print(not c > a)
|
||
|
print(not a == b) # klarer: a != b
|
||
|
print(not b != d) # klarer: b == d
|
||
|
print(not a > c)
|
||
|
|
||
|
# %%
|
||
|
# logische Kombination
|
||
|
|
||
|
print(b > a and a > c)
|
||
|
print(a > b or a > c)
|
||
|
print(not a < c and b >= d)
|
||
|
|
||
|
# %%
|
||
|
# Bool'scher (True/False) Wert von Integers, Floats und Strings
|
||
|
print(a > c)
|
||
|
print(bool(a > c)) ## redundant, da Vergleichsausdrücke immer einen bool'schen Wert haben
|
||
|
# Aber:
|
||
|
print(bool(a))
|
||
|
print(not a) # auch ein bool'scher Ausdruck
|
||
|
|
||
|
# %%
|
||
|
print(bool(0))
|
||
|
print(bool(1))
|
||
|
print(bool(-1))
|
||
|
print(bool(0.0))
|
||
|
print(bool(0.1))
|
||
|
print(bool(-0.1))
|
||
|
print(bool("True"))
|
||
|
print(bool("False")) # !!!
|
||
|
print(bool(""))
|
||
|
print(bool("\0"))
|
||
|
|
||
|
|