Loops (while and for)#

Education objectives

  • blocks while and for

  • keywords in, break and continue

  • built-in functions range and enumerate

Loops with the keyword while#

i = 0
while i < 4:
    i += 1
print("i =", i)
i = 4
i = 0
while i < 4:
    i += 1
    print("i =", i)
i = 1
i = 2
i = 3
i = 4

Exercise 9

  • Edit a script with Spyder that calculates the average of a set of numbers. For example numbers = [67, 12, 2, 9, 23, 5]

    • using the functions sum and len

    • manually (without sum), using the keyword while

    • check that the 2 methods give the same results with

    assert avg0 == avg1

  • Run the script

    • in Spyder,

    • in a IPython session opened from another terminal,

    • with the command python.

Simulating do while stop_condition construction#

while True:
    if stop_condition:
        break
    # content of the do-while loop

Loops with the keyword for#

values = range(5)
for i in values:
    print("i =", i)
i = 0
i = 1
i = 2
i = 3
i = 4

The build-in function range() is very useful for loops. It creates a range object, which is an iterable, immutable sequence of numbers.

# syntax is range(start, stop, step)
range(0, 4, 1)
range(0, 4)

(default start is 0, default step is 1)

range(4)
range(0, 4)
list(range(1, 8, 2))
[1, 3, 5, 7]

range() is memory efficient because it does not store all the numbers in memory.

range(1_000_000_000_000_000)
range(0, 1000000000000000)

Warning

Do not try list(range(1_000_000_000_000_000)) at home! Can you estimate the order of magnitude of memory it would use?

It is common to use range() directly in for loops:

for idx in range(4):
    print(idx, end=", ")
0, 1, 2, 3,

for loops are of course not limited to integers:

groceries = ["Carrots", "Cabbage", "Milk", "Onions", "Pepper"]

print("Groceries list")
for grocery in groceries:
    print("-", grocery)
Groceries list
- Carrots
- Cabbage
- Milk
- Onions
- Pepper

The built-in function enumerate is very useful to access indices

print("My top 5 groceries:")
for index, grocery in enumerate(groceries):
    print(f"{index}. {grocery}")
My top 5 groceries:
0. Carrots
1. Cabbage
2. Milk
3. Onions
4. Pepper

Loops: keywords continue and break#

  • continue: passes the block in the loop and continues the loop.

for x in range(1, 8):
    if x == 5:
        continue
    print(x, end=", ")
1, 2, 3, 4, 6, 7,
  • break: stop the loop.

for x in range(1, 8):
    if x == 5:
        break
    print(x, end=", ")
1, 2, 3, 4,

Exercise 10

  • Extend your script with another method (using a for loop) to compute the average.

  • In IPython, try to understand how the function enumerate works. Use it in your script.

Exercise 11

We build a list:

from random import randint, shuffle

n = 20
i_removed = randint(0, n - 1)
print("integer remove from the list:", i_removed)
numbers = list(range(n))
numbers.remove(i_removed)
shuffle(numbers)
print(f"shuffled list:\n  {numbers}")
integer remove from the list: 7
shuffled list:
  [15, 4, 5, 18, 6, 8, 3, 1, 0, 12, 16, 11, 14, 9, 13, 17, 19, 10, 2]

One element has been removed:

  • Find this element (given that you can change the ordering of numbers).

  • Find this element (given that you cannot change the ordering of numbers).

list: list comprehension#

They are iterable so they are often used to make loops. We have already seen how to use the keyword for. For example to build a new list (side note: x**2 computes x^2):

l0 = [1, 4, 10]
l1 = []
for number in l0:
    l1.append(number**2)

print(l1)
[1, 16, 100]

There is a more readable (and slightly more efficient) method to do such things, the list comprehension:

l1 = [number**2 for number in l0]
print(l1)
[1, 16, 100]
# list comprehension with a condition
[s for s in ["a", "bbb", "e"] if len(s) == 1]
['a', 'e']
# lists comprehensions can be cascaded
[(x, y) for x in [1, 2] for y in ["a", "b"]]
[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]

Exercise 12 (advanced)

  • Write a function extract_patterns(text, n=3) extracting the list of patterns of size n=3 from a long string (e.g. if text = "basically", patterns would be the list ['bas', 'asi', 'sic', ..., 'lly']). Use list comprehension, range, slicing. Use a sliding window.

  • You can apply your function to a long “ipsum lorem” string (ask to your favorite web search engine).