Start learning free

LESSON 151 OF 673 · WHILE LOOPS & FOR LOOPS

Range function: start in Python

Lesson

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