Start learning free

LESSON 123 OF 410 · WHILE LOOPS & FOR LOOPS

Range function: start in Python

Lesson Read the full explanation

The range() function defaults to 0 as a starting value.

However, it is possible to specify the starting value by adding a first parameter: range(start, end).

For example:

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

Looping through numbers:

We can use range() and for to loop through the numbers in some range:

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

The output of this code is:

2
3
4
5

Assignment

Let's print all numbers from 3 through 10 using a for loop.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    PYTHON · WHILE LOOPS & FOR LOOPS

    Printing Numbers 3 in Python, explained

    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. Understanding how to use the range() function effectively can significantly enhance your ability to write efficient and readable code.

    In this lesson, we will explore how to specify a starting value for the range() function and use it in loops. This is particularly useful in scenarios where you need to iterate over a specific subset of numbers.

    Understanding the Basics

    The range() function generates a sequence of numbers, starting from a specified start value and ending just before a specified end value. The basic syntax is range(start, end), where start is the first number in the sequence and end is the number that the sequence stops before.

    For example, range(2, 6) generates the sequence [2, 3, 4, 5]. It starts at 2 and ends just before 6.

    Main Concepts

    Let's break down the key concepts and techniques involved in using the range() function:

    • Start Parameter: The first parameter in the range() function specifies the starting value of the sequence.
    • End Parameter: The second parameter specifies the end value, which is not included in the sequence.
    • Looping: The for loop can be used to iterate over the sequence generated by range().

    Here is an example that demonstrates these concepts:

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

    This code will output:

    2
    3
    4
    5
    

    Examples and Use Cases

    Let's look at some examples to see how the range() function can be used in different contexts:

    Example 1: Printing numbers from 3 to 10

    for i in range(3, 11):
        print(i)
    

    This code will output:

    3
    4
    5
    6
    7
    8
    9
    10
    

    Common Pitfalls and Best Practices

    When using the range() function, it's important to remember that the end value is not included in the sequence. A common mistake is to assume that the end value is included, which can lead to off-by-one errors.

    Best practices for using the range() function include:

    • Always double-check the start and end values to ensure they are correct.
    • Use descriptive variable names in loops to make your code more readable.
    • Consider using the step parameter if you need to increment by values other than 1.

    Advanced Techniques

    The range() function also supports a third parameter called step, which specifies the increment between each number in the sequence. For example, range(2, 10, 2) generates the sequence [2, 4, 6, 8].

    Here is an example that uses the step parameter:

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

    This code will output:

    2
    4
    6
    8
    

    Code Implementation

    Let's implement the assignment to print all numbers from 3 through 10 using a for loop:

    # Loop through numbers from 3 to 10
    for i in range(3, 11):
        print(i)
    

    This code will output:

    3
    4
    5
    6
    7
    8
    9
    10
    

    Debugging and Testing

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

    • Print the sequence generated by range() to verify it is correct.
    • Use breakpoints to step through the loop and inspect variable values.

    To test functions or scripts that use the range() function, you can write test cases that check the output for different start, end, and step values.

    Thinking and Problem-Solving Tips

    When approaching problems that involve the range() function, consider the following strategies:

    • Break down the problem into smaller parts and solve each part step-by-step.
    • Write pseudocode to outline the logic before implementing it in Python.
    • Practice with different range() parameters to become familiar with its behavior.

    Conclusion

    In this lesson, we explored the range() function in Python and how to specify a starting value. We discussed its significance, common use cases, and best practices. By mastering the range() function, you can write more efficient and readable code, especially when working with loops.

    Practice using the range() function in different scenarios to solidify your understanding and improve your programming skills.

    Additional Resources

    For further reading and practice problems related to the range() function, consider the following resources:

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free