Loops are a pretty fun aspect in any programming language; they are used in all sorts of applications for a variety of reasons. In Python, the two types of loops we utilize are the for and while loop.

For Loops

For loops are meant to iterate over a selected sequence.

x = [1,2,3,4]
for y in x:
    print(x)

A For loop could iterate over a set of numbers by using the range (returns a range object, a type of iterable) and “range” (this function returns the generator object that can be used to display numbers only by looping) functions.

Example:

# Prints out the numbers 0,1,2,3
for x in range(4):
    print(x)

# Prints out 2,3,4,5,6,7
for x in range(2, 8):
    print(x)

# Prints out 3,7
for x in range(3, 8, 4):
    print(x)

While Loops

The second loop is the while loop. A While loop’s role is to repeat as long as a certain, Boolean, condition is met.

Example:

# Prints out 0-9

count = 0
while count < 10:
    print(count)
    count += 1  # This is the same as count = count + 1

“Break” & “Continue” statements

The statements I want to go over, since we are talking about loops, are “break” and “continue.” If I were to have the urge to exit a loop, I would use a “break.” If I were to skip the current block or fragment, I would use “continue.”

“Break” statement:

# Prints out 0-9
count = 0
while True:
    print(count)
    count += 1
    if count >= 10:
        break

“Continue” statement:

# Prints out only odd numbers - 1-13
for x in range(15):
    # Check if x is even
    if x % 2 == 0:
        continue
    print(x)

Else clause

Think of the “else” clause as a backup plan. If you run a loop and run a condition, and say if the criteria was not satisfied, you would want to use the “else” clause. If the “break” statement is present, the “else” condition is skipped; the “else” condition would execute even if there is a “continue” statement.

Example:

# Prints out 0-8 and then it prints "count value reached 10"
x=0
while(x<10):
    print(x)
    x +=2
else:
    print("x value reached %d" %(count))

# Prints out 1-4
for y in range(1, 50):
    if(y%5==0):
        break
    print(y)
else:
    print("this will not print since the 'for' loop is terminated due to break but not due to a fail")

Exercise

We are going to try a loop exercise. What we are going to do is utilize a loop to sift out all the numbers in a data set. The catch is that we want to print out only the even numbers in the order that they came in. The final catch is that we do not want any number after 100.

Exercise:

data = [ 64, 133, 17, 113, 30, 23, 69, 120, 16, 138, 68, 6, 130, 22, 150, 119, 123, 37, 54, 76, 149, 47, 82, 103, 7, 88, 63, 147, 26, 75, 9, 71, 144, 13, 117, 102, 101, 74, 45, 67, 80, 99, 28, 125, 106, 1, 3, 140, 44, 121, 100, 38, 32, 98, 145, 36, 112, 116, 87, 84, 92, 124, 118, 58, 108, 11, 73, 42, 137, 43, 96, 12, 24, 25, 10, 50, 53, 143, 20, 62, 60, 59, 15, 129, 83, 19, 89, 77, 18, 94, 78, 91, 86, 14, 39, 33, 142, 55, 135, 141
]

Solution:

data = [ 64, 133, 17, 113, 30, 23, 69, 120, 16, 138, 68, 6, 130, 22, 150, 119, 123, 37, 54, 76, 149, 47, 82, 103, 7, 88, 63, 147, 26, 75, 9, 71, 144, 13, 117, 102, 101, 74, 45, 67, 80, 99, 28, 125, 106, 1, 3, 140, 44, 121, 100, 38, 32, 98, 145, 36, 112, 116, 87, 84, 92, 124, 118, 58, 108, 11, 73, 42, 137, 43, 96, 12, 24, 25, 10, 50, 53, 143, 20, 62, 60, 59, 15, 129, 83, 19, 89, 77, 18, 94, 78, 91, 86, 14, 39, 33, 142, 55, 135, 141
]

# your code goes here
for x in data:
    if x == 100:
        break

    if x % 2 == 1:
        continue

    print(x)

Output:

<script.py> output:
64
30
120
16
138
68
6
130
22
150
54
76
82
88
26
144
102
74
80
28
106
140
44

For more information

As always, if you ever do get stuck and require more resources, please check out Python’s official documentation.