INTRODUCTION TO LOOPS!

Hello, my name is Manuel and I run ManyProgramming™. Today we will be Introducing you to Loops. I hope you enjoy this blog.

Loops: Computers are great at doing boring tasks without complaining. The Programmers aren’t, but they are good at getting computers to do repetitive work for them; by using Loops. A loop runs the same block of code over and over again. There are several different types of loops.

For Loops: When you know how many times you want to run a block of code, you can use a ‘for loop’. In the example below, I am telling Python to print “Manuel’s Room – Keep Out!!!” Python knows how many times it should print that statement because I put the range (1, 8). At the beginning of the piece of code it says ‘for counter in range (1, 8)’. I have told you what the range is so now I will tell you what the ‘for counter’ means. The ‘for counter’ is the loop variable and means it is the counter of the loops. You either run it any number of times or for each element in a list or a dictionary.

>>> for counter in range (1, 8):
...    print('Manuel\'s Room - Keep Out!!!')
...
Manuel's Room - Keep Out!!!
Manuel's Room - Keep Out!!!
Manuel's Room - Keep Out!!!
Manuel's Room - Keep Out!!!
Manuel's Room - Keep Out!!!
Manuel's Room - Keep Out!!!
Manuel's Room - Keep Out!!!

Range: In Python code, the word “range” followed by two numbers within brackets stands for “all the numbers from the first number to one less than the second number”. E.g. ‘range (1, 4)’ means the numbers 1, 2 & 3 – but not the fourth number. In my “Keep Out” program, range (1, 11) is the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 but not 11.

Escape Character (\): The Backslash in ‘Manuel \ ‘s room’ tells Python to ignore the apostrophe so that it doesn’t treat it as the quotation mark that closes the whole string. A backslash used like this  is called an Escape Character. It tell Python not to count the next character when working out if the line makes sense or contains errors.

While Loops: What happens if you don’t know how many times you want to repeat the code. Do you need a crystal ball or some other way of seeing into the future? No, it’s okay! You can use a while loop!

Loop Condition: A While Loop doesn’t have a loop variable that’s set to a range of values. Instead it has a loop Condition. This is a Boolean expression that can be either True or False. It’s a bit like a bouncer at a disco asking you if you’ve got a ticket. If you have one (True), head straight for the dance floor; if you don’t (False), the bouncer won’t let you in. In programming, if the loop condition isn’t True, you won’t get into the loop.

Balancing Act: In this example, I have written a program to keep track of how many of his troupe of acrobatic hippopotamuses have balanced on top of each other to make a tower. Read through the code and see if you can figure out how it works.

>>> hippos = 0
>>> answer = 'y'
>>> while answer == 'y'
        hippos = hippos + 1
        print(str(hippos) + ' balancing hippos!')
        answer = input('Add another hippo? (y/n)')

Loops inside Loops: Can the body of a loop have another loop within it? Yes! This is called a Nested Loop. It’s like Russian dolls, where each doll fits inside a larger doll. In a Nested Loop, an inner loop runs inside an outer loop. One Loop inside another: In this example I have changed my “Keep Out” program to a “Three Cheers” program that prints “Hip, Hip Hooray!” three times. Because each cheer includes the word “Hip” twice, I am using a Nested Loop to print it.

>>> for hooray_counter in range(1, 4):
        for hip_counter in range(1, 3):
            print('Hip')
        print('Hooray!')

Super Skills: We learnt about lists last Sunday and so now I will put loops into lists. I call it Super Skills because it is incredible what it can do. As you can see in the examples below I have written ‘x**2’ which is x to the power of 2. This means that if you switch the two with any other number, the power will be to whatever number you switched the two with.

>>> [x**2 for x in range(5)]
[0, 1, 4, 9, 16]

The Next example I will be showing is to repeat a certain word or number.  You always need to use square brackets [] and need to do this [‘anything_you_want’ for i in range(whatever_number_you_want)]. Here is the example I used:

>>> ["abc" for i in range(5)]
['abc', 'abc', 'abc', 'abc', 'abc']

Thank you very much for reading my blog and I hope you enjoyed it; I hope it made your day. If you would like to see my Book Reviews blog then Click Here!

Leave a comment