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.
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.
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.
Let's break down the key concepts and techniques involved in using the range()
function:
range()
function specifies the starting value of the sequence.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
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
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:
step
parameter if you need to increment by values other than 1.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
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
When debugging code that uses the range()
function, consider the following tips:
range()
to verify it is correct.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.
When approaching problems that involve the range()
function, consider the following strategies:
range()
parameters to become familiar with its behavior.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.
For further reading and practice problems related to the range()
function, consider the following resources: