Iterate und gelten bedingte Kriterien.
Run this notebook in your web browser:
Der Link öffnet b03-pyloop.py als interaktives marimo Notebook, das vollständig in Ihrem Webbrowser läuft, ohne etwas zu installieren.
Warten Sie einen Moment, bis Python im Browser geladen ist (das dauert nur am ersten Start länger).
Klicken Sie auf die gelbe Starttaste (▶, unten rechts) um alle Zellen auszuführen.
Bearbeiten und re-run jede Codezelle zu experimentieren: marimo automatisch aktualisiert alle Zellen, die davon abhängig sind.
Ihre Bearbeitungen bleiben nur im Browser-Tab. Um sie zu halten, speichern Sie das Notizbuch durch das Menü (≠, oben rechts) > Export... > Python.
Run this marimo notebook locally
Um b03-pyloop.py auf Ihrem eigenen Gerät zu betreiben, erhalten Sie es vom marimo
Repository:
git clone https://github.com/sschwindt/marimo-python-course.gitInstalliere marimo:
pip install marimoStarten Sie im Ordner Repository das Notebook mit
marimo edit b03-pyloop.py
Run this notebook with Jupyter
This page is also available as the Jupyter notebook b03-pyloop.ipynb in the jupyter, or install Python and JupyterLab locally to run b03-pyloop.ipynb on your own machine.
Python provides two basic types of loops to iterate through objects or functions: the for and the while loop statements. Both loop types have additional options and can be combined with conditional statements. Conditional statements evaluate boolean arguments (True/False) using the keywords if: ... else: .... This section introduces the two loop types and conditional statements as integral parts of loops.
Watch this section as a video
Watch this section as a video on the @Hydro-Morphodynamics channel on YouTube.
Bedingte Aussagen (IF - ELSE)¶
Conditional statements open with an if keyword, followed by a test condition (e.g., variable >= 2) and action to accomplish when the test condition is True (boolean test result). The conditional statement can be followed by the elif (else if) and/or else keywords, which represent alternative tests in the case that the if test condition was False. However, when the if statement was True, none of the following statements will be evaluated.
variable_2_test = "ice cream"
if "cream" in variable_2_test:
print("It's creamy, for sure.")
elif "ice" in variable_2_test:
print("It's cold, ice-cold cream.")
else:
print("Anything but ice cream.")It's creamy, for sure.
Any operator can be used in the test condition (see operators) and conditions can be nested, too.
number_of_scoops = 3
if number_of_scoops <= 0:
print("Why? How can you?")
elif number_of_scoops < 4:
if number_of_scoops == 1: # this is a nested if-statement
print("One is better than nothing.")
else:
print("That is reasonable.")
else:
print("A lot. Still reasonable. Maybe.")That is reasonable.
forloop¶
for loops serve for the sequential iteration through objects such as lists or arrays. for loops can also be complemented with else statements at the end (whyever you would want to do this...).
for e in range(0,8,2):
print("e is %d now." % e)
flavors = ["chocolate", "bread", "cherry"]
for index in range(len(flavors)):
print(flavors[index])
else:
print(" --- end of first loop.")
# produces the same
for e in flavors:
print(e)e is 0 now.
e is 2 now.
e is 4 now.
e is 6 now.
chocolate
bread
cherry
--- end of first loop.
chocolate
bread
cherry
In many cases, it is useful to iterate not only either on the iteration number (an incrementing integer value) or the elements of a list (e.g., a string value), but both simultaneously. Both the iteration step number and the list elements can be accessed with the enumeration built-in function:
for iteration_step, list_element in enumerate(flavors):
print("The list element {0} is at position number {1}.".format(list_element, str(iteration_step)))The list element chocolate is at position number 0.
The list element bread is at position number 1.
The list element cherry is at position number 2.
whileloop¶
while loops run until a test condition (expression) is True. Similar to the if statement, the test condition can be composed of just one variable or an expression including operators (e.g., while a > b). To modify a variable within a while loop, use += (add amount), -= (subtract amount), *= (multiply with), or /= (divide by). Also while loops can be complemented with else statements.
count = 10
while (count > 7):
count -= 1
print("Count down %d " % count)
else:
print("Mission aborted.")
count = 0
while True:
print("Count up: %d " % count)
count += 1 # Replaces count = count + 1 - also works with -=, *= and /=
if count > 3:
breakCount down 9
Count down 8
Count down 7
Mission aborted.
Count up: 0
Count up: 1
Count up: 2
Count up: 3
Beispiel¶
Use this code block to practice with data types, for loops and conditional if statements by modifying the variables scoops and favorite_flavor. Note the implementation of try and except statements ensures that whatever element number of scoops or favorite_flavor you define will not crash the script.
scoops = 2 # re-define the number of scoops
favorite_flavor = "vanilla" # choose your favorite flavor
size_scoops = {1: "small", 2: "medium", 3: "this is too much ice cream"}
price_scoops = {1: "3 dollars", 2: "5 dollars", 3: "your health"}
print("Hi,\nI want %d scoop-s in a waffle, please." % scoops)
try:
size = " " + str(size_scoops[scoops])
price = str(price_scoops[scoops])
except KeyError:
size = "n unavailable number of scoops"
price = "not defined"
print("My pleasure to serve you. You have chosen a" + size + " ice cream. The price is " + price + ".")
print("Let me guess your favorite flavor. Say stop when I'm correct.")
for f in flavors:
print("I guess your favorite flavor is %s." % f)
if f == favorite_flavor:
print("Stop, that\'s it!")
if f == "bread":
print("Sorry, this is not a bakery.")
break
Hi,
I want 2 scoop-s in a waffle, please.
My pleasure to serve you. You have chosen a medium ice cream. The price is 5 dollars.
Let me guess your favorite flavor. Say stop when I'm correct.
I guess your favorite flavor is chocolate.
I guess your favorite flavor is bread.
I guess your favorite flavor is cherry.
