How to do a For loop in Python

Loops are a very powerful command to understand within Python, they allow us to repeat processes as many times as we want to without explicitly writing out lines of code for each instance. There are 2 main looping command within Python: For loop & While loop, both loops are similar but different, a for loop is used for iterating over a specified iteration (i’ll explain this later) where a while loop is a conditional statement iterating only if the statement is true, let’s go ahead and find out how to do a for loop!

For Loop

for value in dataframe:
do this
else:
do this

Now that is a very basic layout, I tend to use for loops when I want to change something within a data frame, let’s use the following example, let’s make a sample data frame called names and let’s fill it with the following names:

names = ["time", "mike", "larry"]

Awesome, now let’s assume we wanted to print out the names in this data frame, well we could do so pretty easily with this data frame by writing out a print statement like this:

print(names[0])
print(names[1])
print(names[2])

But imagine if we had thousands of records, it would be a waste of time to code out thousands of print statements, instead we can use the for loop as listed above. So, let’s start out our for loop by declaring the value we want to search within the data frame, in this case we’ll use a blank variable “x” as our value (it can be any variable as long as it’s pertinent to the loop), so far we have this:

for x in names:

With that following statement, x is now equal to the name within that data frame, one thing that confused me early on in my career was the placement of the x value, but it can be any string (Ex: “i”, “num”, “name”, etc), once you try it out for yourself a couple times and play around with it you’ll definitely understand it. So all we would have to do is add a print statement for x like this

print(x)

And that’s really it, this is the final code:

for x in names:
print(x)

Now let’s say there’s no more values in “names”, well we would want to make sure theres a “else” statement at the end (else statements aren’t mandatory but I tend to keep them there to help me keep track of my code when i’m running the program). An else statement basically tells the code that: if the for loop is done > do this. Let’s start off by typing out the following line of code:

else:

We would then add whatever statement we wanted when the loop is finished, let’s just have a print out message that says “for loop is done”, this is the final piece of code for that:

else:
print("for loop is done")

Now, our final code looks like this:

for x in names:
print(x)
else:
print("for loop is done")

When we run this code our output should be like this:

Congrats, you have just ran a for loop in Python! Again, this is a pretty basic understanding, but it’s a great start of running more for loops in the future!

Data Scientist / Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store