Python – Loops

Links to: Python editor // Variables & strings // Collections // Conditions // Working with text files // Functions // Working with libraries // Exercises


In programming, it is often very useful do carry out a the same action for a series of different items. You might, for instance, want to go through a list of words and count and print the number of characters in each word. Now, you could do this for each word individually and access every word, one item at a time

fruits = ["apple", "pear", "peach", "banana", "peach", "cherry", "orange", "kiwi"]

This is a lot of repetitive typing. Luckily, Python provides the so-called for-statements for this. The for loop allows us to iterate through any iteratable object, such as a list, and do the same thing with each of its elements. The basic format of a for-statement is:

for a_single_item in an_iterable_something:

We can print all letters of the word wonderful as follows:

for letter in "wonderful":

Likewise, we can print all the items that are contained in the fruit list:

for fruit in fruits:
Exercise 1: Remix the song using STRING, LIST and FOR, IN
# Prince, Purple Rain (using a hashtag = making a comment)
song = “I never meant to cause you any sorrow\n \
I never meant to cause you any pain\n \
I only wanted to one time to see you laughing\n \
I only wanted to see you\n \
Laughing in the purple rain.”

Steps: transform string in list of words, declare a new list, use the for statement: for each word, double the word, save the new word in a variable, append all doubled words to the new list, transform this new list into a string.


The for loop allows you to create a counter, that helps you define the position of the elements in the list.

fruits = ["apple", "banana", "lemon", "orange", "pear"]
position = 0
for element in fruits:
    print(element, position)
    position += 1


Note that you can combine IF and FOR. We rewrite the song with the words that count more than 4 letters.

remix_2 = []
songlist = song.split()
for word in songlist:
    if len(word)<= 4:

There are different operators possible: larger than, smaller than, equal to, larger than and equal to, smaller than and equal to.

Note that you will often encounter a more compact way to write the same.

remix_2 = [word for word in songlist if len(word) <= 4]
Exercise 2: print a list of words of the song + next to each word its position in the sentence
Exercise 3: rewrite song by placing words with r to the beginning of the song
Exercise 4: create an Anaerobe of the song (remove all r's)

Solutions: Link to script


A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true. The risk of using ‘while’ is the possibility of triggering infinite loops.

For example, let’s print our song 9 times:

amount = 0
while (amount < 10):
    print(' '.join(songlist))
    amount = amount + 1
    print("Python can be a printing factory!")
What you’ve learned
  • for
  • while
  • different operators