06

Phase 2 - Level 1: Basic Looping

Chapter 6 • Beginner

60 min

Phase 2 - Level 1: Basic Looping

Introduction to Loops

Loops allow you to repeat code multiple times. They are essential for processing collections, iterating through data, and automating repetitive tasks.

Types of Loops

For Loop

Used when you know the number of iterations:

python.js
for i in range(start, end, step):
    # Code to repeat

While Loop

Used when iteration depends on a condition:

python.js
while condition:
    # Code to repeat

Range Function

  • range(n): 0 to n-1
  • range(start, end): start to end-1
  • range(start, end, step): with step size

Common Patterns

  • Print numbers in sequence
  • Calculate sums and products
  • Iterate through collections
  • Count occurrences

Key Concepts

  1. Iteration: Repeating code multiple times
  2. Loop Variable: Variable that changes each iteration
  3. Range: Sequence of numbers to iterate over
  4. Accumulation: Building up values (sum, product)

Hands-on Examples

Print Numbers 1 to 10

# Print numbers 1 to 10
for i in range(1, 11):
    print(i)

range(1, 11) generates numbers from 1 to 10. The loop variable i takes each value and prints it.