The Range Function and Indexing in Python

What is the output of this code snippet?

 

print(range(5, 10)[-1])
print(range(0, 10, 3)[2])
print(range(-10, -100, -30)[1])

 

If this book can teach you only one thing, it is a thorough understanding of the most important Python concepts such as indexing and slicing. I can not emphasize enough how important these concepts are for your practical work. The goal of this puzzle is to strengthen your understanding of these. The puzzle consists of three quick tasks about indexing and the range function.
Repetition is a powerful teacher!

The first line prints the last element of the range sequence. A short reminder: the upper bound range parameter is not included in the sequence.
The second line prints the third element (not the second) of the range sequence 0,3,6,9. Thus, the step size is three as defined in the last optional range parameter.
The third line prints the second element of the range sequence -10, -40, -70 with step size -30.

Many Finxters have problems with either the indexing or the range function. They often select the wrong element from the sequence.
It is the proficient use of the basics that differentiates excellent from average programmers.


Are you a master coder?
Test your skills now!

 

Related Video

Solution

9
6
-40

 

Leave a Comment