Loops

For Loops

Write a for loop

1
2
for i in range(10):
    print("do stuff here")

Use the for loop’s loop variable

1
2
3
4
for i in range(10):
    new_number = i * 100
    print("The loop variable is i.  It equals {}".format(i))
    print("I used it to make a new number. That number is {}".format(new_number))

Use range inside a for loop

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
start = 3
stop = 10
step = 2

for i in range(stop):
    print(i)

for i in range(start, stop):
    print(i)

for i in range(start, stop, step):
    print(i)

Use a list inside a for loop

1
2
3
4
my_pets = ['euclid', 'leta']

for pet in my_pets:
    print("One of my pets: {}".format(pet))

Nest one for loop inside another for loop

1
2
3
4
for i in range(4):
    for j in range(4):
        result = i * j
        print("{} times {} is {}".format(i, j, result))

While Loops

Use a comparison

1
2
3
4
5
response = ""

while response != "exit":
    print("Inside the loop!")
    response = input("Please provide input: ")

Use a boolean variable

1
2
3
4
5
6
7
done = False

while not done:
    print("Inside the loop!")
    response = input("Please provide input: ")
    if response == "exit":
        done = True

Loop forever

1
2
while True:
    print("Don't do this!  It is a bad idea.")

Special Loop Commands

Skip the rest of the current cycle in the loop

1
2
3
4
5
for i in range(100):
    if i < 90:
        continue
    else:
        print("At number {}".format(i))

Break out of the loop entirely

1
2
3
4
while True:
    response = input("Give me input: ")
    if response == "exit":
        break