DEV Community

Shahzoda D.
Shahzoda D.

Posted on • Updated on

Python Loops Cheat Sheet

Coming from C++, Python loops confused me--I'm talking about for loops, in specific. In C++, you tell a loop exactly how to behave from the initializer, condition, to the increment. In Python you write it in English. That is if you simply want it to print the items (and only the items) in the order it's in. This isn't a blog post for beginner coders, only new pythoners (pythonists? snake charmers??)

I. simply iterate through the list and print the values:

arr2 = [10, 20, 30, 40, 50, 60]
for i in arr2:
    print(i)

II. simply iterate through list and print the values and the indexes:

arr2 = [10, 20, 30, 40, 50, 60]
for i, num in enumerate(arr2):
    print(i, num)

III. iterate backwards:

arr2 = [10, 20, 30, 40, 50, 60]
for val in reversed(arr2):
    print(val)

IV. iterate with the indexes alone:

arr2 = [10, 20, 30, 40, 50, 60]
for i in range(len(arr2)):
    print(i) 

V. Iterate the index backwards:

arr2 = [10, 20, 30, 40, 50, 60]
for i in range(len(arr2), 0, -1):
    print(i)

VI. iterate with the index and value backwards:

arr2 = [10, 20, 30, 40, 50, 60]
for i in range(len(arr2), 0, -1)::
    print(i, arr2[i])

VI. iterate from the index 3 to the index 5:

arr2 = [10, 20, 30, 40, 50, 60]
for val in arr2[3:6]:
    print(val)

VII. iterate from the index 3 to the index 5 with the index:

arr2 = [10, 20, 30, 40, 50, 60]
for i in range(3,6):
    print(i)

IX. iterate from the indexes backwards with access to the index (not just the value):

arr2 = [10, 20, 30, 40, 50, 60]
for i in range(5,0,-1):
    print(i)

X. looping using enumerate:

# this one is probably my favorite
arr2 = [10, 20, 30, 40, 50, 60]
for i, val in enumerate(arr2):
    print(i, val)

This is literally copied and pasted from my notes. Hope it makes sense.

Did anyone else think Python loops were weird? Do you loop any of these differently in Python? Comment to let me know.

Special thanks to Ben Sinclair and rhymes for their contributions to this article.

Latest comments (6)

Collapse
 
dillman_kevin profile image
Kevin Dillman

Very nice, concise examples.

Collapse
 
shahzzoda profile image
Shahzoda D.

thank you!!!!

Collapse
 
rhymes profile image
rhymes • Edited

Hi Shahzoda! Welcome to DEV and thanks for your first post. C++ and Python have a long standing relationship and it's nice to see new Pythonistas coming from there ;-)

A couple of tips:

IV. iterate with the indexes alone:

your example can become:

>>> arr2 = [10, 20, 30, 40, 50, 60]
>>> for i, _ in enumerate(arr2):
...     print(i)
...

also, if you add python to the ticks you can enable syntax highlighting:

syntax highlighted python block

Can't wait for you to discover list comprehensions ;-)

Collapse
 
shahzzoda profile image
Shahzoda D.

You're incredible!! Thank you for the syntax highlight tip. I learned list comprehensions already, they're really cool but the "dont care" variable is a new one too me. Really cool stuff.

Collapse
 
moopet profile image
Ben Sinclair

Instead of doing range(1,6)[::-1] you can pass a negative step into the function itself like range(5,0,-1) which might be more readable depending on the context.

Collapse
 
shahzzoda profile image
Shahzoda D.

That is much more readable. Thank you for your insight!