Introduction

The range is 0 index based, so it’s mean that it starts from 0, not 1. Eg.

1

2

3

range(10)

foriinrange(10):

print(i)

It will display the following numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

Alternatively, you can display a range as a list:

1

print(list(range(10)))

When it comes to memory usage, it doesn’t matter whether you use range(10) or range(1000), but if you use list(range(10)) you have to assign this memory to a list. So the larger the list, the more memory it will use.

As you can see, the number 10 is not displayed. Let’s display numbers from 1 to 10. In this case, we have to specify starting number as 1 and ending number as 11.

1

range(1,11)

Now, the following numbers will be displayed:

1

1,2,3,4,5,6,7,8,9,10

Odd and even numbers

In the previous examples, the loop iterated numbers by 1. You can change it by adding the third parameter.

1

range(1,10,2)

This code will display the odd numbers: 1, 3, 5, 7, 9

Alternatively, you can display even numbers:

1

range(0,10,2)

The output is:

1

0,2,4,6,8

Negative numbers

Beside positive numbers, you can also use negative ones.

This code will return the following output:

1

range(-10,10,2)

1

-10,-8,-6,-4,-2,0,2,4,6,8

The first occurrence of a character in a range

If you want to access individual elements of a range, you can do it using one of these two methods:

The first example will return the index of number 4.

1

print(range(0,10,2).index(6))

1

0,2,4,6,8

Because the counting starts from 0 it returns 3 as the fourth element (0, 1, 2, 3).

The next example returns 8 as the fifth element (0, 2, 4, 6, 8)

1

print(range(0,10,2)[4])

Comparing ranges

You can compare ranges by using the comparison operator (==).

1

r=range(0,10)

If you write the following code, the interpreter returns True.

1

print(r==r)

Look, what will happen if you create these two ranges.

1

2

r1=range(0,7,2)

r2=range(0,8,2)

At first, it seems that there are two different ranges. But when you compare them the result is True.

1

print(r1==r2)

That’s because both ranges return the same numbers:

1

0,2,4,6

Display range from the highest to the lowest number

In order to display a range from highest to lowest number you can’t do the following thing:

range(10, 0)

But it doesn’t mean that you cannot achieve this using a different approach.

First method

Take a look at the following code.

1

2

3

r=range(0,10)

foriinr[::-1]:

print(i)

Execute the code. This time the numbers are displayed from the highest to the lowest.

1

9,8,7,6,5,4,3,2,1

Similarly, you can do this using steps. But be careful because these two methods will give you different results.

1

2

3

r=range(0,10,2)

foriinr[::-1]:

print(i)

The result is

1

8,6,4,2,0

Second method

1

2

3

r=range(0,10)

foriinr[::-2]:

print(i)

This time the result will be

1

9,7,5,3,1

Ranges for float values

Ranges can only be used with integers. If you try to use float numbers. the interpreter will return an error. If you want to use floats, you will have to install numpyor create your own function.

1

2

3

4

5

import numpy asnp

r=np.arange(0,1,0.2)

foriinr:

print(i)

Creating your own function

1

2

3

4

5

6

7

8

def frange(start,stop,step):

i=start

whilei<stop:

yieldi

i+=step

foriinfrange(0,1,0.2):

print(i)

Challenge

You have the following string.

1

my_string="lairotuT nohtyP"

Display it as “Python tutorial”

hint

Display characters in a loop the same way as you did for displaying ranges from the highest to the lowest number.

By default, python displays each character in a new line. You can override it by specifying the end character print(i, end=””).