The range() function in Python


In this lesson, we will focus on iterating over sequences of numbers using the range() function.


The range() function

The range() function returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and ends at a specified number. For example:

range(5) # returns the sequence [0, 1, 2, 3, 4]

Notice that the argument is an exclusive upper bound. The function stops when hitting that value, without appending it to the sequence. That's why range(5) is not the values of 0 to 5, but the values 0 to 4.


Looping over a range:

Now, we can use a for loop along with the in keyword to iterate over a range, like this:

for i in range(4):
	print("For loops are cool!")

The output of this code is:

For loops are cool!
For loops are cool!
For loops are cool!
For loops are cool!

Assignment

Let's print "I promise to learn coding." 5 times using a for loop.


Hint
Look at the examples above if you get stuck.


Introduction

The range() function in Python is a powerful tool for generating sequences of numbers. It is commonly used in loops to iterate over a sequence of numbers, making it a fundamental concept in programming. Understanding how to use range() effectively can simplify many coding tasks, such as iterating over arrays, generating indices, and more.

Understanding the Basics

The range() function generates a sequence of numbers. By default, it starts from 0, increments by 1, and stops before a specified number. Here is a simple example:

range(5) # returns the sequence [0, 1, 2, 3, 4]

In this example, range(5) generates numbers from 0 to 4. The number 5 is not included because the range is exclusive of the upper bound.

Main Concepts

The range() function can take up to three arguments:

For example:

range(2, 10, 2) # returns the sequence [2, 4, 6, 8]

In this example, the sequence starts at 2, ends before 10, and increments by 2.

Examples and Use Cases

Let's look at some practical examples:

# Example 1: Simple range
for i in range(5):
    print(i)
# Output: 0 1 2 3 4

# Example 2: Specifying start and stop
for i in range(2, 6):
    print(i)
# Output: 2 3 4 5

# Example 3: Specifying start, stop, and step
for i in range(1, 10, 3):
    print(i)
# Output: 1 4 7

These examples demonstrate how to use range() with different arguments to generate various sequences.

Common Pitfalls and Best Practices

Here are some common mistakes to avoid:

Best practices include:

Advanced Techniques

Advanced usage of range() includes generating sequences in reverse order and using it in list comprehensions:

# Reverse order
for i in range(10, 0, -1):
    print(i)
# Output: 10 9 8 7 6 5 4 3 2 1

# List comprehension
squares = [x**2 for x in range(10)]
print(squares)
# Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

These techniques can be very useful in more complex scenarios.

Code Implementation

Let's implement the assignment to print "I promise to learn coding." 5 times using a for loop:

# Assignment solution
for i in range(5):
    print("I promise to learn coding.")

This code will output the desired message five times.

Debugging and Testing

When debugging code that uses range(), consider the following tips:

For testing, you can write test cases to ensure the function generates the correct sequences:

# Example test case
def test_range():
    assert list(range(5)) == [0, 1, 2, 3, 4]
    assert list(range(2, 6)) == [2, 3, 4, 5]
    assert list(range(1, 10, 3)) == [1, 4, 7]
    assert list(range(10, 0, -1)) == [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
    print("All tests passed.")

test_range()

This function tests various range() sequences to ensure they are correct.

Thinking and Problem-Solving Tips

When approaching problems involving range():

Conclusion

Mastering the range() function is essential for efficient looping and sequence generation in Python. By understanding its basics, common pitfalls, and advanced techniques, you can write more effective and readable code. Practice using range() in various scenarios to solidify your understanding.

Additional Resources

For further reading and practice, consider the following resources: