PyGuide

Learn Python with practical tutorials and code examples

Python range() Function: Complete Guide with Examples

The range() function is one of Python's most fundamental tools for creating sequences of numbers and controlling loop iterations. This comprehensive guide covers everything you need to know about using range() effectively.

Table of Contents #

  1. Basic range() Syntax
  2. Single Parameter Usage
  3. Two Parameter Usage
  4. Three Parameter Usage
  5. Advanced Techniques
  6. Common Use Cases
  7. Performance Considerations
  8. Best Practices

Basic range() Syntax #

The range() function can take 1, 2, or 3 parameters:

  • range(stop) - generates numbers from 0 to stop-1
  • range(start, stop) - generates numbers from start to stop-1
  • range(start, stop, step) - generates numbers from start to stop-1 with step increment

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Single Parameter Usage #

Basic Counting #

Using range with a single parameter:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Loop Control #

Control the number of iterations:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Two Parameter Usage #

Custom Start and Stop #

Specify both start and stop values:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Practical Applications #

Real-world uses of two-parameter range:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Three Parameter Usage #

Custom Step Values #

Use the step parameter for custom increments:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Reverse Counting #

Use negative step for counting backwards:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Complex Step Patterns #

Advanced stepping techniques:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Advanced Techniques #

Range with enumerate #

Combine range with enumerate for complex patterns:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Range in List Comprehensions #

Powerful combinations with list comprehensions:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Common Use Cases #

Array/List Processing #

Efficient array manipulation with range:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Mathematical Sequences #

Generate mathematical sequences:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Game Development Patterns #

Common game programming uses:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Performance Considerations #

Memory Efficiency #

Range is memory-efficient compared to lists:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Performance Tips #

Optimize range usage for better performance:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Best Practices #

1. When to Use range() #

Use range when:

  • Iterating a specific number of times
  • Generating sequential numbers
  • Accessing list elements by index
  • Creating numerical sequences

2. Prefer range over manual lists #

Don't:

# Creating unnecessary lists
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for num in numbers:
    process(num)

Do:

# Use range directly
for num in range(10):
    process(num)

3. Use enumerate when you need both index and value #

Don't:

items = ['a', 'b', 'c']
for i in range(len(items)):
    print(i, items[i])

Do:

items = ['a', 'b', 'c']
for i, item in enumerate(items):
    print(i, item)

4. Common range patterns #

# Basic patterns
range(n)              # 0 to n-1
range(1, n+1)         # 1 to n
range(0, n, 2)        # Even numbers
range(1, n, 2)        # Odd numbers
range(n, 0, -1)       # Countdown
range(len(items))     # List indices

Summary #

The range() function is essential for:

  • Loop control - Iterate specific number of times
  • Sequence generation - Create number sequences efficiently
  • Index access - Work with list/array indices
  • Mathematical patterns - Generate arithmetic progressions
  • Memory efficiency - Lazy evaluation saves memory

Conclusion #

Mastering the range() function is fundamental to writing efficient Python code. Its memory efficiency, flexibility, and simplicity make it indispensable for loops, sequences, and numerical operations. Practice these patterns to become proficient in Python iteration and sequence generation.