Python Range With Steps

Python is a famous programming language because it supports different kinds of modules and functions that perform various tasks. For instance, the inbuilt “range()” function is used in Python script to create a sequence of numbers. The “for loop” is normally used along with the combination of the “range()” function to iterate over the sequence and produce the desired result.

In this write-up, the Python range with steps will briefly explain with multiple examples. The following points are discussed in this tutorial:

So let’s begin our tutorial!

What is Python Range With Steps?

The “range()” function in Python is used to create the sequence of numbers. The range() function accepts three parameters a starting value, an ending value, and step size. The syntax of the “range()” function is shown below:

range(start, stop[, step])

In the above syntax:

  • The parameter “start” indicates the starting number of the range. The “start” parameter default value is equal to “0”.
  • The parameter “stop” is mandatory to define. It indicates the ending number of the range where the range value ends.
  • The “step” parameter default value is “1”. So, if you omit the step parameter, the step size will be incremented by 1 in each iteration. Or else you can specify the step of your own choice. In case you specify a negative value, the step will be decremented according to the specified step value.

Note: To iterate over the sequence of range, the “for” loop is normally used along with the “range()” function.

To begin, let’s take a look at the following example:

Example 1: Python Range Without Steps

In the example given below, the “range()” function is used along with “for loop” without any step value:

Code:

for number in range(1, 10):
    print(number)

In the above code, the “range()” function is used without the “steps” parameter. The “for” loop iterates over the range() function and prints all the numbers within the specified range.

Output:

The above Output is displayed on the screen that shows the numbers in the given range.

Example 2: Python Range With Positive Steps

In the example given below, a positive value is passed as a “step” parameter to the range() function as an argument.

Code:

for number in range(1, 10, 2):
    print(number)

In the above code, the “for” loop iterates over the range sequence with a positive steps value of “2”. The range starts from “1” and ends at “10”, and the values will be incremented by “2” in each iteration.

Output:

Based on the specified step value, range() retrieves the sequence of numeric values within the given range.

Example 3: Python Range With Negative Steps

Let’s pass a negative value as a step argument to the range() function and see how it works:

Code:

for number in range(30, 0, -3):
    print(number, end=" ")

In the above code:

  • The “for” loop iterates over the range sequence according to the specified step argument value.
  • As the step size is initialized with a “negative number”. So in each iteration, the number will be decremented by 3.

Note: The starting limit of a number is always greater than the end limit in the decrement scenario.

Output:

As seen from the above output, the values have decremented within the given range.

That’s it for this guide!

Conclusion

In Python, the “range()” function creates the sequence of numbers by specifying the upper and lower limit by adding step size value. The step size value can be defined as a positive or negative number. The positive value increments the value, and the negative value decreases within a given range. The “for loop” is used with the range() function to iterate over the range sequence. This Python guide presented a detailed overview of the Python range() function.