Skip to content

Python for loop – Python Beginners Series

Welcome to another Python 3 tutorial, in the previous one we talked about the Python if else statement. This new article is going to be covering the Python for loop.

Python For Loop vs While Loop

The for loop is used for pretty much the same tasks as the while loop, they’re completely interchangeable. Typically you’re going to see the while loop being used for sounds kind of funny. But I suppose I would say finite or infinite tasks. The for loop is usually used to be a little bit more variable, for uncertain kind of time frames and stuff like that.

The for loop can actually be used for the same tasks as the while loop, and the while loop can do all of the things a for loop can do. I mean they’re completely interchangeable performance wise, action wise, all that

For this reason, I would just say that it just comes down to personal preference. I prefer the for loop myself a lot of times. A for loop is much more condensed or condensable than the while loop. But again it really just comes down to personal preference, t just doesn’t matter. What I’d like to do now is show you some examples of the for loop and where you’re going to most often see it. Because some people really like myself included I do actually use both it just depends on what the situation is.

Let’s go ahead and get started. First what we’re going to want is generally you’re going to see the for loop being used iterate through a list. What iterate means is just go through a list. So let’s make an example list so we’re going to name this example list and I’m just going to type in random values. You can do the same you can try and copy my values if you want but it’s not necessary.

Python for Loop Example

exampleList = [20, 5, 12, 7, 11, 32, 75]

You’ll love this Python Beginners crash course! check it out now.

I’m just putting some variables in here some values. Now what we’re going to do is we’re going to say

for eachNumber in exampleList:

What it’s doing is it’s creating a variable here, eachNumber now becomes a variable that each step of the way these numbers are being assigned to this variable.

If you noticed already by now with the while loop, or when you make one of these loops and you hit enter, Python interpreter automatically tabs it over for you. So if you’re coming from maybe another language, or not at all from any programming language you have to usually do this whereas you don’t have to really do it in Python.

for loop indentation in Python
for loop indentation in Python

In other languages typically you’ve got like curly braces and you come down and you put all you want it inside those. Most people say you should have good standards and you should indent it yourself. But it is necessary for other languages; in Python, it’s going to automatically indent for you. That makes your code pretty automatically standardized.

You must have that indentation, and this indentation will apply throughout the entire loop in order to get out of that loop. I’ll show you what you would need to do. For now, let’s just say print each number, we’ll save and run that and as you can see it just iterated through the entire list in order.

for loop with print function
for loop with print function

Try it yourself, Learn by coding!

exampleList = [20, 5, 12, 7, 11, 32, 75]

for eachNumber in exampleList:

print (eachNumber)

Now what you can do to is then you come down. We’ll just say print continue program and you’ll see that it continues the program after the for loop because it’s not under the same indentation.

for loop with print function not indented
for loop with print function not indented

Indentation matters…

But if you hit tab and you indent the print function along with this for loop you’re going to see continue program for every number. When we run this you can see that at each step of the way it prints continue the program

for loop with print function indented
for loop with print function indented

For Loop as a counter with the range() Function

Just keep that in mind that’s how your indentation really matters. You can use a for loop much like you use while loop as a counter and it’s much better as a counter and here’s why. With the for-loop giving you;for x in range(1; 11) and range is a built-in function in Python. And all we want to do is just print X for now

for x in range(1; 11)

print (x)

Let’s run that and you’ll see that what it did. Obviously our previous program but then it did 1 2 3 4 5 6 7 8 9 10, not 11. The way range works it starts at the first number goes all the way up to the ending number.

 range function
for loop as a counter with range function

One thing to note range is a built-in Python function so you don’t have to import it and it will literally make that range of numbers for you in Python. In Python 3 threre is this version of range that has actually been back ported to Python 2.7 andit’s known as Xrange in Python 2.7

Outside the box!

What is beneficial about this range function over the Python 2.7 xrange function is when you do something in Python 3.3 and you say range (1, 1000000000000000000000000000000000000000000)

What it’s going to do in Python 2.7 is literally generated a list that is this from 1 to this number or whatever it is. Obviously that’s going to use a lot of your virtual memory; your RAM; and it’s probably going to max out your RAM before it completes the operation. In Python 3.3 or if you use the range function it turns this into what’s known as a generator and eventually we’ll talk more about generators later on.

I hope this article was helpful for you to understand the basics of the Python for loop. Remember that my Python Beginners course is hugely discounted for astateofdata.com readers only for a few days, so make sure your reserve your spot today. And if you get a chance to follow me on twitter that would be great.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)