Python videos

Loops: for and while

The for loop

A for loop executes a code block for each element in an iterable, such as a list. Analogous to the if statement, the for statement ends with a colon, and the to-be-looped code block is defined by indentation:

prime_numbers = [1, 3, 5, 7, 11]
for prime in prime_numbers:
    print(prime)

Output:

1
3
5
7
11

If you iterate (loop) through a dict, you iterate through its keys. To iterate through the keys and the values at the same time, you can use dict.items() function, which returns an iterable of (key, value) tuples.

ages = {
    'Jay-Z': 47,
    'Emanuel Macron': 40
}
for name, age in ages.items():
    print('{0} is {1} years old'.format(name, age))

Output:

Jay-Z is 47 years old
Emanuel Macron is 40 years old

The while loop

A while loop executes a code block until a particular condition is no longer True.

user_input = ''
while user_input != 'quit':
    user_input = input('>>> ')
    print('The user said: {0}'.format(user_input))

Output:

>>> Hello!
The user said: Hello!
>>> quit
The user said: quit

continue: abort one iteration

The continue statement, which you can use in for and while loops (but nowhere else), aborts a single iteration of a loop, and continues with the next iteration. To give an example, you can use continue to skip all cities that are not capitals:

cities = ['Berlin', 'São Paulo', 'Tokyo', 'New York']
capitals = ['Berlin', 'Tokyo']
for city in cities:
  # If the current city is not a capital, continue with the next city
  if city not in capitals:
    continue
  print('{0} is a capital'.format(city))

Output:

Berlin is a capital
Tokyo is a capital

break: abort a loop

The break statement is similar to the continue statement, but aborts the loop entirely, rather than just aborting the current iteration. To give an example, you can use break to print out all prime numbers below 10.

for prime in prime_numbers:
  # Abort the loop when we reach a number that is 10 or higher
  if prime >= 10:
    break
  print(prime)

Output:

1
3
5
7

Useful functions

range(): iterate through a range of values

range() corresponds to a range of numbers. The simplest and most common use case is to specify only a stop value, which is exclusive (i.e. range(3) does not include 3!):

for i in range(3):
  print(i)

Output:

0
1
2

However, you can also specify a start value and a step size:

from_value = 1
to_value = 4
step_size = 2
for i in range(from_value, to_value, step_size):
  print(i)

Output:

1
3

enumerate(): iterate and count

enumerate() takes an iterable, and returns another iterable in which each element is paired with a counter variable.

cities = ['Berlin', 'São Paulo', 'Tokyo', 'New York']
for i, city in enumerate(cities):
  print('{0}: {1}'.format(i, city))

Output:

0: Berlin
1: São Paulo
2: Tokyo
3: New York

zip(): iterate through multiple iterables

zip() takes one or more iterables, and returns a zipped iterable in which elements from the original iterables are paired. This allows you to iterate through multiple iterables at the same time. The zipped iterable is as long as the shortest of the original iterables.

artists = 'The Beatles', 'Elvis Presley', 'Michael Jackson', 'Madonna'
sales = 600e6, 600e6, 350e6, 300e6
for artist, sold in zip(artists, sales):
  print('{0} sold {1} records'.format(artist, sold))

Output:

The Beatles sold 600000000.0 records
Elvis Presley sold 600000000.0 records
Michael Jackson sold 350000000.0 records
Madonna sold 300000000.0 records

Exercises

Fibonacci

Calculate the Fibonacci series up to 1,000. Print out each Fibonacci number including its position in the series. Like so:

0: 1
1: 1
2: 2
3: 3
4: 5
etc.

View solution

Best-selling artists

Do the following until the user enters quit:

  • Ask the name of an artist
  • Look up the number of sales of this artist in a dict
  • Print out the result if the number of sales are known
  • If the number of sales are unknown, ask the user to enter the number of sales, and update the dict accordingly

View solution