Last active
April 9, 2022 07:30
-
-
Save JosephTLyons/ae56cb96fc51775ae7f24016d1e0b911 to your computer and use it in GitHub Desktop.
Gentlemen's addition to spicy loops
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
# 1: A true classic | |
# -------------------------------------- | |
animals = ["dog", "cat", "bird", "mouse", "goat"] | |
for animal in animals: | |
print(animal) | |
# 2: A dash of pepper ... | |
# -------------------------------------- | |
animals = ["dog", "cat", "bird", "mouse", "goat"] | |
for index, animal in enumerate(animals): | |
print(animals[index]) | |
# 3: It's a Friday night and there is no dress code | |
# -------------------------------------- | |
animals = ["dog", "cat", "bird", "mouse", "goat"]\ | |
index = 0 | |
while True: | |
try: | |
print(animals[index]) | |
index += 1 | |
except IndexError: | |
break | |
# 4: Peak performance | |
# -------------------------------------- | |
animals = ["dog", "cat", "bird", "mouse", "goat"] | |
while True: | |
try: | |
animal, *animals = animals | |
print(animal) | |
except ValueError: | |
break | |
# 5: Bring the thunder | |
# -------------------------------------- | |
animals = ["dog", "cat", "bird", "mouse", "goat"] | |
funcs = [next for _ in animals] | |
animal_iter = iter(animals) | |
for func in funcs: | |
print(func(animal_iter)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment