Loops

Python - A Quick Start for existing Programers

2 min read

Published Sep 16 2025, updated Sep 30 2025


21
0
0
0

Python

While loops

While loops run the indented code repeatedly while the condition is True:

# i variable as a counter
i = 0
# loop 5 times when i is 0,1,2,3,4
while i < 5:
    print(i)
    # increment the i counter by 1
    i += 1

To skip to the next condition check you can use the continue statement:

i = 0
while i < 5:
    i += 1
    if i == 3:
        # continue back up to the chile condition check
        continue
    print(i)

So this example won't print out 3 as the continue statement will take the flow back to the condition check and then increment i to 4.


To break out a while loop you can use the break statement:

i = 0
while i < 5:
    print(i)
    if i == 3:
        # Break out of the while loop and resume executing code below it
        break
    i += 1

So this example, the last number printed will be 3 and then the while loop is ended.


To run code when the while loop finishes and the condition changes to False, use the else: statement:

i = 0
while i < 5:
    print(i)
    i += 1
else:
    # This else block executes when the while loop condition becomes false
    # It is not executed if the loop is exited via a break statement
    print("Natural end of while loop")

# This block executes after the while loop, even if the loop is exited via a break statement
print("After while loop")

The else section will only be ran if the loop naturally gets to the end and the condition changes to False. If the while loop has a break statement ran, then the else section is skipped.




For loops

To loop through 10 iterations from 0 -> 9:

for x in range(10):
    print(x)

To loop through 1 -> 10:

for x in range(1, 11):
    print(x)


To loop through 2 -> 10, with a 2 step, so every other number:

for x in range(2, 11, 2):
    print(x)

You can use the break, continue and else: statements in a for loop, in the same way you can in a while loop.


To loop through a list:

# Sample list of values
my_list = [10, 20, 30, 40, 50]

# Iterate through the list and print each value
for x in my_list:
    print(x)

To loop through a list and have the elements index:

# Sample list of values
my_list = [10, 20, 30, 40, 50]

# Iterate through the list and print each value and its index
for i, x in enumerate(my_list):
    print(i, x)

The first variable after the for is for the index, and the second is for the value. You need to use the enumerate function to wrap the list.


Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact