Skip to content
Home » PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 12 – Loops

PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 12 – Loops

Spread the love

We use loops if some code must be repeated multiple times. It can be either a fixed number of times or an indefinite number of times, until a condition is met. There are two types of loops: for loops and while loops. Let’s talk about for loops first.

for Loops

We can use the for loop to iterate over the elements of a collection. The collection may be a sequence (like a string), a set, or a dictionary.

The syntax is: After the for keyword we use a loop variable that will be set to each of the elements iterated over during the loop execution in turn and after that we use the name of the collection and finally there’s a colon. After the colon, in the next line, the block of code begins with instructions to be executed on each loop run. This repeated code is called the body of the loop.

for loop

Here’s a simple example with a string collection:

Output:

while Loops

The while loop is used if we don’t know how many times the code should be repeated, but usually the loop runs as long as there’s a condition that is met. So, this is a condition-controlled loop.

Right after the while keyword we write the condition. It will be checked on each loop run. As long as it’s met, the loop will run. As soon as the condition is no longer met, the loop stops. After the condition we use a colon, and in the next line we start writing the code that will be repeated – the body of the loop. Naturally, this block of code is indented appropriately.  The variables that we’re using have to be initialized before.

while loop

Here’s an example:

Possible output:

Infinite loops

It may happen that the condition in the while statement is always true. Then the loop never stops. We call such loops infinite loops. Oftentimes they’re a result of our mistake. If we happen to be trapped in such a loop, we can use the Keyboard Interrupt combination (Ctrl + C) to get out of it. Here’s an example of such an infinite loop:

As number is equal to 1 all the time (its value isn’t changed inside the loop), the condition is always true and the loop is infinite.

QUIZ

1. In Python we don’t have the … loop:
    A) for
    B) foreach
    C) while
 

TRUE OR FALSE?

1) After the for keyword we use a loop variable that will be set to each of the elements iterated over during the loop execution in turn.
2) In infinite loops the condition in the while statement is always true.  

WHAT’S THE OUTPUT?

1)

2)

SOLUTION

QUIZ

1. In Python we don’t have the … loop:
    A) for
    B) foreach
    C) while
 

TRUE OR FALSE?

1) After the for keyword we use a loop variable that will be set to each of the elements iterated over during the loop execution in turn. True
2) In infinite loops the condition in the while statement is always true. True  

WHAT’S THE OUTPUT?

1)

2)

PROJECT

PROJECT SOLUTION


Spread the love

Leave a Reply