Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Master Python Loops: For, While, Break, Continue, and Enumerate Explained

Master Python Loops: For, While, Break, Continue, and Enumerate Explained

Loops are the backbone of iterative programming. They let you execute a block of code repeatedly until a condition is met. Python offers two primary looping constructs: for (iterators) and while (condition‑based).

What Is a Loop?

A loop repeatedly runs a set of statements. In Python, loops are used to process collections, generate sequences, or run tasks until a stopping criterion is reached.

For Loops (Iterators)

A for loop iterates over items of an iterable—lists, tuples, strings, or ranges. It stops automatically when the iterable is exhausted, making it safer than manually tracking indices.

x = 0
for x in range(2, 7):
    print(x)

Output:
2
3
4
5
6

While Loops (Condition‑Based)

A while loop repeats as long as a given boolean expression remains True. It’s ideal when the number of iterations isn’t known beforehand.

x = 0
while x < 4:
    print(x)
    x += 1

Output:
0
1
2
3

Looping Over Non‑Numeric Collections

For loops work seamlessly with strings and other collections:

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
for m in months:
    print(m)

Output:
Jan
Feb
Mar
Apr
May
Jun

Breaking Out of a Loop

The break statement terminates the loop immediately. It’s useful for early exit when a condition is met.

for x in range(10, 20):
    if x == 15:
        break
    print(x)

Output:
10
11
12
13
14

Skipping an Iteration

The continue statement skips the remainder of the loop body for the current iteration and proceeds to the next cycle.

for x in range(10, 20):
    if x % 5 == 0:
        continue
    print(x)

Output:
11
12
13
14
16
17
18
19

Using enumerate() for Indexed Loops

The built‑in enumerate() function adds a counter to an iterable, returning index–value pairs.

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
for i, m in enumerate(months):
    print(i, m)

Output:
0 Jan
1 Feb
2 Mar
3 Apr
4 May
5 Jun

Repeating a Statement Multiple Times

You can use a for loop over a string of digits or a range to repeat a message:

for i in '123':
    print('guru99', i)

Output:
guru99 1
guru99 2
guru99 3

Comprehensive Example Table

Loop Type Code
While loop
x = 0
while x < 4:
    print(x)
    x += 1
For loop (numeric)
for x in range(2, 7):
    print(x)
For loop (string)
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
for m in months:
    print(m)
Break in for loop
for x in range(10, 20):
    if x == 15:
        break
    print(x)
Continue in for loop
for x in range(10, 20):
    if x % 5 == 0:
        continue
    print(x)
Enumerate in for loop
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
for i, m in enumerate(months):
    print(i, m)

Python 2 Compatibility

The examples above are Python 3 syntax. For Python 2, replace print() with print statements and adjust the range behavior accordingly.

# While loop
x = 0
while x < 4:
    print x
    x += 1

# For loop
for x in range(2, 7):
    print x

# For loop over string
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
for m in months:
    print m

# Break example
for x in range(10, 20):
    if x == 15:
        break
    print x

# Continue example
for x in range(10, 20):
    if x % 5 == 0:
        continue
    print x

# Enumerate example
for i, m in enumerate(months):
    print i, m

Output:
0
1
2
3

2
3
4
5
6

Jan
Feb
Mar
Apr
May
Jun

10
11
12
13
14

11
12
13
14
16
17
18
19

0 Jan
1 Feb
2 Mar
3 Apr
4 May
5 Jun

For more in‑depth references, consult the official Python tutorial on control flow.

Python

  1. Mastering Python For Loops: Syntax, Examples, and Advanced Patterns
  2. Mastering Python's While Loop: Syntax, Examples, and Best Practices
  3. Mastering Python Loop Control: break & continue
  4. Java Continue Statement – Mastering Loop Control with Examples
  5. C# Control Flow: IF, SWITCH, FOR, WHILE – Practical Examples & Explanations
  6. Master C Loops: For, While, and Do‑While Explained with Practical Examples
  7. Python Loop Control: break, continue, and pass Statements Explained with Practical Examples
  8. Mastering C Loops: A Comprehensive Guide to Repetition Control
  9. Master C# Loops: Efficient Code Repetition Techniques
  10. Master Python Loops: Control Flow & Repetition Techniques