Last active
March 19, 2022 04:51
-
-
Save abadger/91108db3c11f3bef900db6c338d4e608 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Three ways to do for loops: | |
## The common way | |
Most of the time, you have an iterable (a list, dictionary, string, etc) and want to loop over it. | |
Python makes this the easiest way to use a for loop: | |
``` python | |
contestants = ["First place finisher", "Second is still good", "the first civilized age"] | |
for contestant in contestants: | |
print(contestant) | |
``` | |
## Sometimes you want to add flair | |
Sometimes you write a list and you actually need the list index for some reason. Instead of actually retrieving | |
the index you should indirectly get an equivalent to the index using enumerate. | |
``` python | |
medals = ["Gold", "Silver", "Bronze"] | |
contestants = ["First place finisher", "Second is still good", "the first civilized age"] | |
for idx, contestant in enumerate(contestants): | |
print("{}: {}".format(medals[idx], contestant)) | |
``` | |
Although, if you control your input, there's usually one or more better ways: | |
``` python | |
medals = ["Gold", "Silver", "Bronze"] | |
contestants = ["First place finisher", "Second is still good", "the first civilized age"] | |
for medal, contestant in zip(medals, contestants): | |
print("{}: {}".format(medal, contestant)) | |
``` | |
or | |
``` python | |
finisher = {1: ("Gold", "First place finisher"), | |
2: ("Silver", "Second is still good"), | |
3: ("Bronze", "the first civilized age")} | |
for _idx, (medal, contestant) in sorted(finisher.items()): | |
# finisher.items() returns tuples of (key, value). Since the value is itself a tuple, | |
# each tuple looks like this: (1, ("Gold", "First place finisher")) | |
# | |
# sorted sorts those tuples. Since the first entry in every tuple is a unique integer, | |
# the tuples are returned in the order from 1 to 3. | |
# | |
# _idx, (medal, contestant) unpacks the returned tuples into their own variables. For the first entry, | |
# _idx = 1, medal = "Gold", and contestnat = "First place finisher". | |
# We don't have to do this unpacking if we don't want to the first time through this loop: | |
# for _idx, value in sorted(finisher.items()) _idx = 1, value = ("Gold", "First place finisher") | |
# Or even: | |
# for entry in sorted(finisher.items()) entry = (1, ("Gold", "First place finisher")) | |
print("{}: {}".format(medal, contestant)) | |
``` | |
## On rare occassions, you want a counting loop | |
Sometimes you really do want to count a discrete range of numbers. For those you should | |
iterate over the iterator returned by the range() function. For instance to try something | |
three times and then give up: | |
``` python | |
for count in range(0, 3): | |
try: | |
data = http_get("http://flakey.resource.com/") | |
except: | |
# Try again | |
pass | |
``` | |
Or to print out the numbers from one to three. | |
``` python | |
# Print out a blank top 3 finishers' form | |
for place in range(1, 4): | |
print("{}. ________________".format(place)) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment