Control Flow - For Loops

The for statement iterates over a sequence.
Sequence - an object that has items with a certain order (list, tuple, dictionary, set, string)
A for statement gets a sequence and in every iteration, refers only to one item in the sequence. The following iteration refers to the following item in the sequence.

Iteration means executing the same block of code over and over. A code that implements iteration is called a loop. Python has two primitive loop commands:

while loops Indefinite iteration The numbegr of times the loop is executed isn’t specified explicitly in advance
for loops Definite iteration The number of times the designated block will be executed is specified explicitly at the time the loop starts

Here is an example:

#For my_list = ["table", "chair", "shelf", 1, 4] for n in my_list: print(n) print("loop ended")

Here is a diagram describing the process:

Responsive image

Here is one more example:

#For my_list = ["table", "chair", "shelf", 1, 4] for n in my_list: if type(n) == int: print(n) print("loop ended")

The range() Function

It is a very common need to iterate over a sequence of numbers. To do so, one can write the sequence itself. This solution becomes unpractical when the sequence becomes too long. Another solution is to use a for loop (to iterate) along with a while loop (to create the sequence). While it is a much better idea than writing the sequence, it is stiil not a good solution
This is why the range() function was invented! range() is a built-in python function that let us iterate over a sequence of numbers.

The syntax:
range(start, end, step)

  1. Start - (optional) an integer that indicates the start value. If not given the default is 0.
  2. End - an integer that indicates the end value.
  3. Step - (optional) the difference between two items in the list. If not given the default is 1.

Here is an example:

#range for i in range(5): print(i) for i in range(10, 18, 3): print(i)

The continue Statement

Using the continue statement we can skip the current iteration, and continue to the next one:

#continue my_list = ["table", "chair", "shelf", 1, 4] print(my_list) for n in my_list: if n == "chair": continue print(n)

The break Statement

Using the break statement we can exit the loop (not just one iteration):

#break my_list = ["table", "chair", "shelf", 1, 4] print(my_list) for n in my_list: if n == "chair": break print(n)

The else Statement

Using the else statement we can run code when the loop is finished:

#else my_list = ["table", "chair", "shelf", 1, 4] print(my_list) for n in my_list: print(n) else: print("The loop is finished")

Nested for Statement

It is possible to use a nested for statement:

#nested for my_list = ["table", "chair", "shelf", 1, 4] your_list = ["1, 2, 3"] print(my_list) print(your_list) for n in my_list: print(n) for m in your_list: print(m) else: print("The loop is finished")

Exercise

Create a for loop that iterates over a list of numbers.
Print out each number if it is odd.

#Write your code here: