Python basics
  • Tutorial
  • Exercices
  • About
  1. Fundamentals
  2. 7-Loops
  • Introduction
    • Python
  • Fundamentals
    • 0-Basic Syntax
    • 1-Variables
    • 2-Data types
    • 3-Numbers
    • 4-Strings
    • 5-Lists
    • 6-Booleans
    • 7-Loops
    • 8-Functions
    • 9-Dictionary
  • Advanced
    • 10-Numpy
    • 11-Matplotlib
    • 12-DataFrame
  • CLI
    • Exercices
    • Solutions
  • Git
    • Git introduction
    • Git branch
    • Git exercices
  • SQL
    • SQL Exercices
    • SQL Solutions

On this page

  • While loop
    • Break
    • Continue
  • For loops
    • Break
    • Continue
    • Range
    • Nested loops
    • Enumerate
  1. Fundamentals
  2. 7-Loops

7-Loops

Notebook

Link to the Google Colaboratory notebook: 7-Loops

Python has two primitive loop commands:

  • while loops
  • for loops

While loop

With the while loop we can execute a set of statements as long as a condition is true.

Be careful not to forget the incremental conditions, otherwise the program will not stop.

counter = 0 
while counter < 5: 
    print(counter)
    counter+=1
0
1
2
3
4

Break

With the break statement we can stop the loop even if the while condition is true:

counter = 0 
while counter < 5: 
    if counter == 2: 
        break
    print(counter)
    counter+=1
0
1

Continue

With the continue statement we can stop the current iteration, and continue with the next:

 counter = 0 
 while counter < 5: 
     counter+=1
     if counter == 2: 
         continue
     print(counter)
1
3
4
5

For loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  # Print each fruit in the list
  print(x)
apple
banana
cherry

Break

With the break statement we can stop the loop before it has looped through all the items:

liste = [0, 5, 10, 20]
for element in liste:
    print(element)
    if element == 10:
        break
0
5
10

Continue

With the continue statement we can stop the current iteration of the loop, and continue with the next:

liste = [0, 5, 10, 20]
for element in liste:
    print(element)
    if element == 10:
        continue
0
5
10
20

Range

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

for i in range(5): 
    print(i)
0
1
2
3
4

We can also specify the start, end and the step:

for i in range(70, 80, 2): 
    print(i)
70
72
74
76
78

Nested loops

A nested loop is a loop inside a loop.

The “inner loop” will be executed one time for each iteration of the “outer loop”:

list1 = ["Italy", "France", "Spain"]
list2 = range(len(list1))
for element1 in list1:
    for element2 in list2:
        print(element1, element2)
Italy 0
Italy 1
Italy 2
France 0
France 1
France 2
Spain 0
Spain 1
Spain 2

Enumerate

One can loop through the element of a list AND the indexes it goes through.

list1 = ["Italy", "France", "Spain"]
for index, element in enumerate(list1):
    print(index, element)
0 Italy
1 France
2 Spain
6-Booleans
8-Functions
 

Copyright 2023, Clément Bernard