Iteration (or "looping") is the act of repeating something. In code, a program loop will execute the same block of statements more than one time. Loops can let you avoid cut-and-pasting many duplicate lines of code that all do similar things. Loops can also let you continue to do the same things until certain conditions are met.

"for" Loop Syntax
A "for loop" is commonly used to repeat a block of code a specific number of times. You can use a "for" loop to iterate over all of the characters in a string, over all of the numbers in a range, or over all of the elements in other data collections. Each "for" loop will follow the same basic pattern.

for <loop variable> in <data>:
statement 1
statement 2
statement 3
print("After loop ends")
Copy
Three indented statements forming loop bodyWe start with the "for" keyword, followed by the name of a variable we want to use inside the loop. Each time the loop runs, that variable will be automatically updated with the next value in the string, numeric range, or data set. Next, we write the "in" keyword, followed by some data over which the loop will iterate. You can specify a string, numeric range, or list of data. The "for" statement ends with a colon (:).

You can then write one or more statements that will go inside the loop body. The loop body is a block of statements that will repeat inside the loop. You identify the statements in the body by indenting them to the right by any number of characters (as long as the indentation is the same for each statement). Our example shows where three different statements would go.

The loop variable will contain a value from your specified data each time through the loop. If you have string data, the loop variable will contain a character. If you specify a numeric range, the loop variable will hold an integer. You can do anything you like with the loop variable, depending on the needs of your program!

The loop body ends at the first statement that is indented back to the left to match the initial "for" keyword. Our example shows a print() statement that will not run until the loop is completely done iterating over the data.

"for" Loops to Iterate over Strings
Strings are basically just a collection of individual letters, and you can use a "for" loop to walk through a string, one character at a time. Just place some string data after the "in" keyword in the "for" statement.

The following code asks the user to enter a word. It then loops over each character in that word, counting the number of "e" characters found. After the loop ends, the final count is printed. Run the code yourself to see how it works.

Notice there is a second level of indentation in the "for" loop above. The print() and "if" statements are both indented by the same amount because both statements will be executed normally as part of the loop body. However, the "if" statement itself has another "body" formed by the indented "count = count + 1" statement. That statement is indented to the right of the "if" to show it is part of the "if" body and will only run when the "if" expression is True.

"for" Loops to Iterate over a Range
Programmers in other languages will frequently use "for" loops to iterate a specific number of times. Python supports this as well using ranges. Let's say you want a numeric index variable to start at 1 and iterate up to 4 (1, 2, 3, 4). You could define this range using "range(1,5)" after the "in" keyword. The first number in a range is the starting point, and the last number is one more than the ending value.

for index in range(1,5): # index will become 1, 2, 3, 4 inside the loop
Copy
The numeric index variable can be used for any kind of calculation or task that your program demands. For example, let's say your program needed to add up all the numbers between 1 and 100. We could write a "for" loop with a range to get each of those numbers and add them to a running total.

The variable name "i" is often used in loops when iterating over a numeric sequence. "i" doesn't follow the normal best practice rules for making descriptive variable names. However, "i" is well-understood by programmers to represent a numeric index variable in a loop.

Try changing the range in the example above and running the code to confirm the results. Can you add up the numbers between 2 and 6 or between 1 and 10? You might want to add a print() statement inside the loop to show you the value of the index variable on each pass through the loop.

You don't need to hard-code the starting or ending range number! You can use variables or any expression that evaluates to an integer, as demonstrated below.

lower = 5
upper = 100

for i in range (lower, upper): # loop from 5 up through 99
print(i)
for i in range(1,upper + 1): # loop from 1 up through 100
print(i)

1. When creating a "for" loop, how do you identify the statements that belong to the body of that loop?
Select one:

a.
Add a colon (:) at the end of every statement in the body

b.
Add a "body" comment (# body) at the end of each statement in the body

c.
Place them inside opening and closing square brackets [ ]

d.
Indent them the same number of spaces underneath the "for" statement

2. What is the purpose of loop variable declared after the "for" keyword?
Select one:

a.
The variable specifies how many times the loop will run

b.
The variable will be automatically updated to hold a value from the data set each time through the loop

c.
The variable will hold any error messages generated inside the loop

d.
The variable holds the name of the data set over which the loop will iterate

3. What is the output of the following code?
count = 0
hobby = "marbles"
for letter in hobby:
count = count + 1
print(str(count))
Select one:

a.
7

b.
5

c.
0

d.
6

4. What is the output of the following code?
hobby = "marbles"
for letter in hobby:
print("#" + letter,end="")
Select one:

a.
marbles

b.
Syntax error; you cannot iterate over the letters in a string using a "for" loop

c.
#m#a#r#b#l#e#s

d.
#######

5. Which of the following "for" loops will successfully iterate with an index variable that ranges from 0 up through (and including) 4?
Select one:

a.
for i in [0,5]

b.
for i range(1,4):

c.
for i in range(0:4):

d.
for i in range(0,5):

1. d. Indent them the same number of spaces underneath the "for" statement

2. b. The variable will be automatically updated to hold a value from the data set each time through the loop

3. b. 5

4. c. #m#a#r#b#l#e#s

5. d. for i in range(0,5)