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 is usually used with for loops.

The range type uses the following arguments:

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

 

start –first number of a sequence.
stop – this is the last number, without including this number.
step – this is the number which 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’s mean 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 ending number as 11.

Now, the following numbers will be displayed:

 

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:

 

Negative numbers

Beside positive numbers, you can also use negative ones.

This code will return the following output:

 

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.

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:

 

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.

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

The result is

 

Second method

This time the result will be
 

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 “Python tutorial”

hint
  1. Display characters in a loop the same way as 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=””).
solution

Leave a Reply

Your email address will not be published. Required fields are marked *