Loops Cheat Sheet

For loops

Use a for loop to do something to every element in a list.

names = ["Jessica", "Adam", "Liz"]
for name in names:
    print(name)

names = ["Jessica", "Adam", "Liz"]
for name in names:
    print("Hello " + name)

if statements inside for loop

for name in ["Alice", "Bob", "Cassie", "Deb", "Ellen"]:
    if name[0] in "AEIOU":
        print(name + " starts with a vowel.")

Sometimes you want to start with a new empty list, and only add to that list if some condition is true:

vowel_names = []
for name in ["Alice", "Bob", "Cassie", "Deb", "Ellen"]:
    if name[0] in "AEIOU":
        vowel_names.append(name)
print(vowel_names)

for loops inside for loops

You can put for loops inside for loops. The indentation dictates which for loop a line is in.

letters = ["a", "b", "c"]
numbers = [1, 2, 3]
for letter in letters:
    for number in numbers:
        print(letter * number)

The order of the for loops matters. Compare the above example with this one:

letters = ["a", "b", "c"]
numbers = [1, 2, 3]
for number in numbers:
    for letter in letters:
        print(number * letter)

Useful functions related to lists and for loops

sorting lists

Use <var>.sort() to sort a list:

names = ["Eliza", "Joe", "Henry", "Harriet", "Wanda", "Pat"]
names.sort()
print(names)

Getting the maximum and minimum values from a list

numbers = [0, 3, 10, -1]
print(max(numbers))
print(min(numbers))

Generating a list of numbers easily with range()

The range() function returns a list of numbers. This is handy for when you want to generate a list of numbers on the fly instead of creating the list yourself.

print(range(5))

Use range when you want to loop over a bunch of numbers in a list:

numbers = range(5)
for number in numbers:
    print(number * number)

We could rewrite the above example like this:

for number in range(5):
    print(number * number)

while loops

Use a while loop to loop so long as a condition is True.

i = 0
while i < 10:
    print(i)
    i = i + 1

break keyword

Use the break keyword to break out of a loop early:

i = 0
while True:
    print(i)
    i = i + 1
    if i > 10:
        break

Get user input with raw_input()

while True:
    t = input("Please type something> ")
    if t == "Quit":
        print("Goodbye!")
        break
    else:
        print("You said: " + t)

Nächste Einheit: 04 Funktionen