Python for loops iterate over a given sequence.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
primes = [1, 2, 3, 5, 7]
for prime in primes:
print(prime)
The range() function
For loops can iterate over a sequence of numbers using the range()
function. Note that the range function is zero based.
# Prints out the numbers 0,1,2,3,4
print("Prints out the numbers 0,1,2,3,4")
for x in range(5):
print(x)
# Prints out 3,4,5
print("Prints out 3,4,5")
for x in range(3, 6):
print(x)
# Prints out 3,5,7
print("Prints out 3,5,7")
for x in range(3, 8, 2):
print(x)
Looping Through a String
Even strings are iterable objects since they contain a sequence of characters:
# This loops and prints each letter in the word Python
for x in "Python":
print(x)
The break Statement
The break
statement can stop the loop before it has looped through all the items:
# this example exits the loop when x is "Mark"
persons = ["Jeff", "Mark", "Ivan"]
for x in persons:
print(x)
if x == "Mark":
break
The continue Statement
The continue
statement can stop the current iteration of the loop, and continue with the next:
# This will not print "Mark"
persons = ["Jeff", "Mark", "Ivan"]
for x in persons:
if x == "Mark":
continue
print(x)
else in For Loop
The else
keyword in a for loop specifies a block of code to be executed when the loop is finished:
# this example exits the loop when x is "Mark"
persons = ["Jeff", "Mark", "Ivan"]
for x in persons:
print(x)
else:
print("Finished printing persons.")
# when there a break stops the loop, the else block will not be executed.
persons = ["Jeff", "Mark", "Ivan"]
for x in persons:
print(x)
if x == "Mark":
break
else:
print("Finished printing persons.")
Nested Loops
A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each iteration of the "outer loop":
color = ["red", "black", "white", "yellow"]
cars = ["Volkswagen", "Ford", "Nissan"]
for x in color:
for y in cars:
print(x, y)
The pass Statement
for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass statement to avoid getting an error.
for x in [0, 1, 2, 3, 4, 5]:
pass
Exercise
Using the for loop, print out all even numbers from the numbers list in the same order they are received. Don't print any numbers that come after 237 in the sequence.
numbers = [
951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
743, 527
]
# your code goes here
numbers = [
951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
743, 527
]
for number in numbers:
if number == 237:
break
if number % 2 == 1:
continue
print(number)
test_object('numbers', incorrect_msg="Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("402",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("984",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("360",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("408",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("980",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("544",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("390",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("984",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("592",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("236",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("942",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("386",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("462",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("418",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("344",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("236",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("566",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("978",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("328",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("162",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("758",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
test_output_contains("918",no_output_msg= "Don't forget to change `numbers` to the correct value as from the instructions.")
success_msg("Excellent!")