Learn about loops and their syntax in Python.

Introduction to loops

Computers are great for doing similar tasks many times—they’re very quick when compared to humans with calculators.

Let’s see if we can get a computer to print the first 10 squared numbers, starting with 0 squared, 1 squared, 2 squared, and so on. We expect to see an output of something like 0, 1, 4, 9, 16, 25, and so forth.

We could just do the calculation ourselves, and have a set of instructions like print(0), print(1), print(4), and so on. This would work, but we would have failed to get the computer to do the calculation for us. More than that, we’d have missed the opportunity to have a generic set of instructions to print the squares of numbers up to any specified value.

Coding examples

To do this, we need to cover a few more new ideas, so we’ll take it slowly.

Let’s run the code below:

Get hands-on with 1200+ tech skills courses.