Python Programming 5

Now witness the power of true computing! Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops!Loops! Process finished with exit code 0

Computer loops are one of the most important concepts of computing. So important that the task itself goes back to the necessary components of the first computer. Ever wonder how a computer can do things so fast? It's because of loops. You are looking at your screen right now to read this paragraph. How did the programmers manage to get millions of pixels on screen for high quality video? That's loops too. Ever wonder why Chrome shuts down, games freeze, or computers run really really slow? Well those might be loops too.

Loops allow a computer to just keep running code until a condition becomes false. Have you ever played a video game and lots of stuff is going on and it starts to run slower. You notice the frame rate drops on the screen. That is your game struggling to loop the millions of pixels necessary to give you a decent frame rate to play a game.

This is off topic but frame rate is a cool subject. And while we will discuss compression more later, this video may be of interest now.

Have a look at what is known as a while loop:

x = 0

while x < 5:

____print(x)

____x = x + 1

What will this code do? The code will print the numbers 0,1,2,3,4. We initialized x to be 0. Then we created a while loop with the condition that so long as x is less than 5 we want to print the value of x. Then the last line shows something interesting that we have not come across. In math, x = x + 1 would likely give you an error since it is not an equal expression. However when writing code, this block simply adds 1 to the original value of x. Since x starts at 0, this line makes x = 1. After this line we are at the end of our while loop (as show because of the indenting) so we must go back up to the top where the condition is and run it again to see if the expression is still true. Is x (1) less than 5? Yes. So run the code inside the while loop again.

Now we print 1 and increment x from 1 to 2. We run the while loop again. Is x (2) less than 5? Yes, and so on until x = 5. When x is 5, x is no longer less than 5 therefore the loop ends and the program moves on. This is why we have the numbers 0-4 printed out and not 0-5.

A datatype we have not covered which becomes relevant with loops is something called the boolean. At this point we've learned how numbers and letters are represented in the code (int, strings). How do we represent whether or not something is true or false? Obviously bits are the building blocks of computers at the lowest level, but what about in the code? This is where booleans come in. Booleans just hold True or False. No letters, numbers, or other values. They only signify whether something is True or False. Check out this code but do not run it yourself yet.

notFalse = True

while notFalse == True:

____print("This statement will be printed until the variable notFalse somehow becomes False")

I've committed somewhat of a computer science sin here. This code above will run forever and will ultimately shut down the program since the computer will not be able to keep up. Because I left nothing inside the while loop that has the potential to make notFalse = false the code will force the application to stop. You can try it out for yourself, but it may crash more than you expect. This is why I mentioned loops provide great things but can also be a downfall up top.

notFalse = True

while notFalse:

____print("This statement will be printed until the variable notFalse somehow becomes False")

____notFalse = False

I want you to notice 3 things about the code above. The first is that this program will end (thankfully) after printing once. The second is that any time you are dealing with True or False in booleans, the words must be capitalized otherwise they will not work. The third is that I was able to use the boolean condition in the while loop without saying '== True'. This is because when dealing with boolean values the default question is for the code to ask is this variable set as True (This is true across virtually all languages).

While loops are great for running a loop as long as you don't know when it will end. It reads like a sentence "do this while the condition is true." But what if we want a loop to run a set number of times and then stop? We could just follow the first example and leave an incrementor at the bottom, but that's messy. That's where the python for loop comes in. Check it out:

for i in range(20):

____print(i)

But wait a second...we didn't declare 'i', and what is range? In due time, child.

'i' is basically a placeholder variable for the for loop which will run 20 times. The range 20 acts as a temporary 'list' (we will learn about lists later) that has 20 made up values. The for loop says "for how ever many values are in the variable/range, I will run this code. Because we are using a range list, the 'i' acts as a variable equal to the corresponding number in the list. If we were to run this code, it would print the numbers 0 through 19. When would something like this be useful? Say for instance you are creating a mobile app (You wouldn't with python) and needed to render a ListView. You could use a for loop to render the specific number of rows you need.

for i in range(0,5):

____do_some_action()

Notice this time 0,5 is used instead of just 5. You can use either one. If you wanted to run something just 3 times but from 4-7, you could type in range(4,7).

There are some other things you can do with for loops but until we learn more about advanced concepts, we will skip them for now.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Video tutorials Video 1 Video 2 Scanlan's Loop Algorithm


Make a program that prints out the numbers 1-100. Now make a program that prints out only even numbers 73-196. Make a program that will ask "Are we there yet?" until the user answers "yes". Make a cool program for the subreddit that uses your skills from past lessons (if statements and such) and loops.

results matching ""

    No results matching ""