Previous Lesson: Lesson 21: For Loops in Python

While loops in Python are almost the same as for loops. But, in this lesson, you’ll know their big difference and you’ll also get some warnings for its use.

While loops are almost the same as for loops. Except that there are some cases that it is better to use for loops instead of while. In either case, we still need while loops. Create a new file: ex19.py. Type this in:

So as you can see, the value of our variable (age) is equal to 0. We use for to start a for loop; we use while to start a while loop. Line 3 states that while age‘s value is less than 18 (and then we don’t forget our colon), the value of age will keep on printing.

Do you remember what += means? Okay, it’s just like this: age = 0 + 1 and then age = 1. Then if we keep going up, age = 1 + 1 and then age = 2. While the value of age is still less than 18 (so that’s 1 up to 17), age will keep on adding up. But if age becomes equal or over 18, our code will stop running. Let’s run this:

Alright!

Warning on While Loops

We don’t want to forget Line 5:

Note: I put an octothorpe (#) on Line 5. It’s just the same as deleting it because Python will ignore it. Besides, we’re gonna need this line for later.

What do you think would happen? Let’s see:

Oh, so our code will never stop running apparently until the end of the world. But, no worries; if you fall to this trap, simply hit Ctrl + C or control + C, and everything will be fine:

Fortunately and hopefully, our computer won’t crash once we’ve used a very large number for this one:

We all understand that one should give this loops a limit value, or else, it would run forever. It’s fun to watch our code run that many times, though.

I’ll show you something for you to realize why while loops are awesome. But type this in first:

What do you think would be the result? Let’s see:

I will not be discussing what just happened and how things came out like that. Rather, I’ll be giving you this opportunity to think and squeeze your brains a little to figure this one out. I dare you to post your explanation for this on the Comments section below and we’ll see if your answer is correct.

In the meantime, you may be asking these questions too:

What’s the difference between a for-loop and a while-loop?
A for-loop can only iterate (loop) “over” collections of things. A while-loop can do any kind of iteration (looping) you want. However, while-loops are harder to get right and you normally can get many things done with for-loops.

Loops are hard. How do I figure them out?
The main reason people don’t understand loops is because they can’t follow the “jumping” that the code does. When a loop runs, it goes through its block of code, and at the end it jumps back to the top. To visualize this, put print statements all over the loop, printing out where in the loop Python is running and what the variables are set to at those points. Put prints before the loop, at the top of the loop, in the middle, and at the bottom. Study the output and try to understand the jumping that’s going on.

So that’s it with while loops in Python! I hope you understood everything that we’ve discussed, except for the last one of course (that’s for your assignment). Don’t move on to the next lesson until you’ve understood the code above because things are gonna get difficult as you move on without understanding these. Wish you the best!