Python

How to execute a for in Python (loop structure)

March 18, 2022

How to execute a for in Python (loop structure)

In this article you will learn how to execute a for in Python, this is a known loop structure, that is present in many programming languages

execute a for in Python thumb

What’s up programmer, how are you? Let’s learn more about repetition structures in Python!

Actually in Python we don’t have a for structure, just while and for with range

And both looping structures achieve the same result as for, so it’s okay to use them, and you won’t have any difficulties either.

Let’s see both approaches:

list = [1, 2, 3, 4, 5]

i = 0

while(i < len(list)):
 print(list[i])
 i += 1

for i in range(len(list)):
 print(list[i])

These two looping structure approaches will loop through the list variable, which is a list (data type) in its entirety.

So now that you know how to use these two repetition structures, just make your choice

Note that in the while approach you must increment the variable you use for iteration, otherwise you will have an infinite loop in the code

Which is when the structure repeats endlessly

Conclusion

In this article we learned how to execute a for in Python – which doesn’t actually have a for structure at all

But the ones that the language brings are sufficient for what is needed, such as what was shown is the iteration in a list

Click here to learn other Python techniques.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x