python range, break, continue, assert and loop

@amitmund June 17, 2026

Python Range, Break, Continue, Assert & Loop Practice Exercises

Introduction

When working with loops, Python provides several useful tools:

  1. range() → Generate sequences of numbers
  2. break → Stop a loop immediately
  3. continue → Skip the current iteration
  4. assert → Check assumptions during program execution

These are commonly used in:

  • Loops
  • Data processing
  • Validation
  • Automation scripts
  • Testing and debugging

The range() Function

What is range()?

The range() function generates a sequence of numbers.

It is commonly used with for loops.

Example:

for i in range(5):
    print(i)

Output:

0
1
2
3
4

Why Use range()?

Without range():

print(1)
print(2)
print(3)
print(4)
print(5)

With range():

for i in range(1, 6):
    print(i)

Output:

1
2
3
4
5

Less code and easier maintenance.


Syntax of range()

range(stop)

or

range(start, stop)

or

range(start, stop, step)

range(stop)

Starts from:

0

Stops before:

stop

Example:

for i in range(5):
    print(i)

Output:

0
1
2
3
4

range(start, stop)

Example:

for i in range(1, 6):
    print(i)

Output:

1
2
3
4
5

range(start, stop, step)

Example:

for i in range(0, 11, 2):
    print(i)

Output:

0
2
4
6
8
10

Negative Step

Example:

for i in range(10, 0, -1):
    print(i)

Output:

10
9
8
7
6
5
4
3
2
1

Counting Backward

for i in range(5, -1, -1):
    print(i)

Output:

5
4
3
2
1
0

Converting range() to List

numbers = list(range(1, 6))

print(numbers)

Output:

[1, 2, 3, 4, 5]

Checking Length of range()

r = range(1, 11)

print(len(r))

Output:

10

Accessing Values from range()

r = range(10)

print(r[0])
print(r[5])

Output:

0
5

Using range() with Multiplication Table

for i in range(1, 11):
    print(5 * i)

Output:

5
10
15
20
25
30
35
40
45
50

Common range() Mistakes


Mistake 1: Stop Value Included

❌ Wrong Expectation

range(1, 5)

Expected:

1 2 3 4 5

Actual:

1 2 3 4

The stop value is excluded.


Mistake 2: Wrong Step Direction

❌ Wrong

range(10, 1, 1)

Output:

Nothing

✅ Correct

range(10, 1, -1)

Mistake 3: Forgetting range() in for Loop

❌ Wrong

for i in 5:
    print(i)

Output:

TypeError

✅ Correct

for i in range(5):
    print(i)

Break Statement

What is break?

The break statement immediately terminates a loop.

When Python encounters break:

Loop Stops Immediately

Basic Example

for i in range(10):

    if i == 5:
        break

    print(i)

Output:

0
1
2
3
4

How break Works

Loop Running
      |
      V
Condition True?
      |
     Yes
      |
    break
      |
 Loop Ends

Real-Life Example: Search Operation

numbers = [10, 20, 30, 40]

for num in numbers:

    if num == 30:
        print("Found")
        break

Output:

Found

No need to continue searching.


Password System Example

attempts = 0

while True:

    password = input("Password: ")

    if password == "python123":
        print("Access Granted")
        break

    attempts += 1

Loop stops when password is correct.


Nested Loop Example with break

for row in range(3):

    for col in range(3):

        if col == 1:
            break

        print(row, col)

Output:

0 0
1 0
2 0

Break only affects the inner loop.


Continue Statement

What is continue?

continue skips the current iteration and moves to the next one.


Basic Example

for i in range(5):

    if i == 2:
        continue

    print(i)

Output:

0
1
3
4

How continue Works

Current Iteration
        |
Condition True?
        |
       Yes
        |
    continue
        |
Skip Remaining Code
        |
Next Iteration

Example: Print Odd Numbers

for i in range(1, 11):

    if i % 2 == 0:
        continue

    print(i)

Output:

1
3
5
7
9

Example: Skip a Character

for char in "PYTHON":

    if char == "H":
        continue

    print(char)

Output:

P
Y
T
O
N

Difference Between break and continue

Feature break continue
Stops Loop Yes No
Skips Iteration No Yes
Ends Loop Completely Yes No
Continues Loop No Yes

Example Comparison

break

for i in range(5):

    if i == 2:
        break

    print(i)

Output:

0
1

continue

for i in range(5):

    if i == 2:
        continue

    print(i)

Output:

0
1
3
4

Common break & continue Mistakes


Mistake 1: Unexpected Loop Exit

for i in range(10):
    break

Output:

Loop stops immediately

Mistake 2: Infinite Loop with continue

❌ Wrong

i = 1

while i <= 5:

    if i == 3:
        continue

    print(i)

    i += 1

Output:

Infinite Loop

Reason:

i += 1 never executes when i == 3.


✅ Correct

i = 1

while i <= 5:

    if i == 3:
        i += 1
        continue

    print(i)

    i += 1

Assert Statement

What is assert?

The assert statement is used to test assumptions.

Syntax:

assert condition

If condition is:

True  → Program Continues
False → AssertionError

Basic Example

age = 20

assert age >= 18

print("Eligible")

Output:

Eligible

Assertion Failure

age = 15

assert age >= 18

Output:

AssertionError

Assert with Message

age = 15

assert age >= 18, "Must be 18 or older"

Output:

AssertionError: Must be 18 or older

Real-Life Example

balance = 1000
withdraw = 500

assert withdraw <= balance, "Insufficient Funds"

print("Transaction Successful")

Output:

Transaction Successful

Another Example

username = "admin"

assert username != "", "Username cannot be empty"

Why Use assert?

Useful for:

  • Testing
  • Debugging
  • Verifying assumptions
  • Detecting bugs early

Common Assert Mistakes


Mistake 1: Using assert for User Validation

❌ Avoid

assert password == "admin"

Assertions can be disabled in some Python execution modes.

Use:

if password != "admin":
    print("Invalid Password")

for user validation.


Mistake 2: Wrong Condition

❌ Wrong

assert age < 18

When requirement is:

age >= 18

Practice Exercises on Looping

Exercise 1

Print numbers from 1 to 20 using a for loop.


Solution

for i in range(1, 21):
    print(i)

Exercise 2

Print even numbers between 1 and 50.


Solution

for i in range(2, 51, 2):
    print(i)

Exercise 3

Print odd numbers between 1 and 50.


Solution

for i in range(1, 51):

    if i % 2 == 0:
        continue

    print(i)

Exercise 4

Print multiplication table of 9.


Solution

for i in range(1, 11):
    print(9 * i)

Exercise 5

Calculate sum of numbers from 1 to 100.


Solution

total = 0

for i in range(1, 101):
    total += i

print(total)

Output:

5050

Exercise 6

Find first number divisible by both 7 and 11 between 1 and 100.


Solution

for i in range(1, 101):

    if i % 7 == 0 and i % 11 == 0:
        print(i)
        break

Output:

77

Exercise 7

Count vowels in a word.


Solution

word = "education"

count = 0

for char in word:

    if char.lower() in "aeiou":
        count += 1

print(count)

Output:

5

Exercise 8

Reverse Countdown


Solution

for i in range(10, 0, -1):
    print(i)

print("Launch!")

Exercise 9

Print a Triangle Pattern


Solution

for row in range(1, 6):

    for col in range(row):
        print("*", end=" ")

    print()

Output:

*
* *
* * *
* * * *
* * * * *

Exercise 10

Find factorial of 5.


Solution

factorial = 1

for i in range(1, 6):
    factorial *= i

print(factorial)

Output:

120

Quick Revision

Concept Purpose
range() Generate numbers
break Stop loop
continue Skip iteration
assert Verify assumptions
Nested Loop Loop inside loop
range(start, stop, step) Custom sequences

Summary

Statement Description
range() Creates number sequences
break Terminates loop immediately
continue Skips current iteration
assert Tests conditions during execution
AssertionError Raised when assert fails
Negative Step Used for reverse counting
Loop Practice Improves logic-building skills

Master range(), break, continue, and assert before moving to Functions because these concepts appear frequently in real-world Python code, coding interviews, automation scripts, testing, and software development.

0 Likes
26 Views
0 Comments

Filters

No filters available for this view.

Reset All