What are Loops?

This lesson explains the functionality of loops in Python.

We'll cover the following

Definition

A loop is a control structure that is used to perform a set of instructions for a specific number of times.

Loops solve the problem of having to write the same set of instructions over and over again. We can specify the number of times we want the code to execute.

One of the biggest applications of loops is traversing data structures, e.g. lists, tuples, sets, etc. In such a case, the loop iterates over the elements of the data structure while performing a set of operations each time.

Just like conditional statements, a loop is classified as a control structure because it directs the flow of a program by making varying decisions in its iterations.

Loops are a crucial part of many popular programming languages such as C++, Java, and JavaScript.

Loops in Python

There are two types of loops that we can use in Python:

  1. The for loop
  2. The while loop

Both differ slightly in terms of functionality.

Get hands-on with 1200+ tech skills courses.