Python Ranges

Ranges in Python 2 are implemented as a function. In Python 3 as a built-in type. We use ranges to generate a list of numbers that are usually used with the for loops.

The range type uses the following arguments:

range([start], stop, [step])

start – the first number of a sequence.
stop – this is the last number, without including this number.
step – this is the number that is added to the next value.

It will give us the following combinations:

range(stop)
range(start, stop)
range(start, stop, step)

Introduction

The range is 0, index-based, so it means that it starts from 0, not 1. Eg.

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:

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 the ending number as 11.

Now, the following numbers will be displayed:

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.

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

Alternatively, you can display even numbers:

The output is:

0, 2, 4, 6, 8

Negative numbers

Besides positive numbers, you can also use negative ones.

This code will return the following output:

-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.

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)

Comparing ranges

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

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

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

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

That’s because both ranges return the same numbers:

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.

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

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.

The result is

8, 6, 4, 2, 0

Second method

This time the result will be

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 numpy or create your own function.

Creating your own function

Challenge

You have the following string.

Display it as a “Python tutorial”

  1. Display characters in a loop the same way you did for displaying ranges from the highest to the lowest number.
  2. By default, python displays each character in a new line. You can override it by specifying the end character print(i, end=””).