Allowing our program to do things over and over is extremely powerful. It will let us run complex calculations, repeating operations, and processing of related items.


This post is part of #100DaysOfPython, check out yesterday's post here if you haven't already. Or go to the index of all 100 days.


An iterable in Python is a type of entity that you can iterate over.

To iterate means to go over each element of an entity.

For example, if you have a pack of cards, you could iterate over them. That would mean you'd go over each card. Potentially you could do something with each card (e.g. look at them, fold them in half, or anything you'd choose).

Iterating over strings

The simplest iterable in Python is the string. That means that we can iterate over a string's elements.

The individual elements of a string are each of the characters in the string. Take, for example, the following:

my_string = 'hello'

We can iterate over each of the elements using a for loop in Python:

my_string = 'hello'

for character in my_string:
    print(character)

In the example above, we are using a for loop to go over each of the elements of my_string. Every time we move onto the next element, we call it character.

Inside the body of the loop we can do something with each character. In this example, we print the character out. Notice how the body of the loop is indented more. Indentation means that it has more white space in front than the line which includes the colon at the end (:).

Note that the variable doesn't have to be called character, it can be anything:

for c in my_string: ...
for character in my_string: ...
for asdf in my_string: ...

But you always have to refer to the correct name inside the body of the loop.

Lists

The next common iterable is the list in Python. It allows us to have more than one element under the same variable name:

my_list = [1, 2, 3]

The snippet above defines a variable, called my_list, whose value is a list of three items.

We can do this with many iterables:

my_list = [1, 2, 3]

print(my_list[0])

In computing, we start counting at 0. With the code above, we would print 1, because when we access the zeroth element of a list, we are accessing the first element.

We can naturally also use a for loop to iterate over this iterable:

my_list = [1, 2, 3]

for num in my_list:
    print(num)

That would go over each number, and in the body of the loop we print the number out.

Another example, doing something a bit more interesting:

my_list = [1, 2, 3]

for num in my_list:
    print(num ** 2)

The ** operator in Python is used for powers; so here we would print the numbers squared:

1
4
9

while loops

The while loop in Python is different from the for loop in that it doesn't iterate over an iterable. Instead it runs for as long as a boolean condition is True.

Don't run the following code:

while True:
    print("Hi")

Because True is always True, this loop would be an infinite loop. There's nothing in that code to tell us when to stop, so it would print "Hi" forever. Not the best thing to be doing!

Instead, we could ask the user whether we should continue printing or not:

should_continue = True

while should_continue:
    print("Hi")
    
    user_input = input("Should we continue? (y/n) ")
    if user_input == 'n':
        should_continue = False

In this snippet of code, we define a variable to store whether we should continue printing, and run the loop based on it. As long as the variable is True, the loop will run again and again.

Every time the loop runs, we print "Hi", and then we ask the user (with the input function) to type y if we should continue, or n if we should stop.

If the user types n, we set should_continue to False, which then means our loop will not run again—and resulting in the termination of our program.


This was an introduction to loops! Experiment and play around with them, and ask us anything you need on Twitter @TecladoCode.

The best way to learn and get more familiar with these constructs is to use them! Keep programming, and things will click together and make more sense as you go.

I'll see you on the next one!