Start learning free

LESSON 152 OF 673 · WHILE LOOPS & FOR LOOPS

Range function: step in Python

Lesson

The range() function defaults to increment the sequence by 1.

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

For example:

range(2, 17, 3) # returns the sequence [2, 5, 8, 11, 14]

Looping through even numbers:

For example, we can use range() and for to loop through the even numbers in some range:

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

The output of this code is:

2
4
6
8
10

Assignment

Let's print all odd numbers from 7 through 23 using a for loop.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check