Python range() Function Tutorial with Examples

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

Python programming language provides range() function in order to create a sequence of numbers in different start number, increments etc. range() function provides lightweight creation of sequences during run-time which makes this type of execution faster.

Syntax and Parameters

range() function has the following syntax where 3 parameters can be accepted but not all of them are mandatory. Only stop parameter is mandatory.

  • START is used to specify the start number of the sequence. This number can be a positive or negative value like 4, -8 etc.
  • STOP specifies the number we want to end the sequence. STOP is a must for range() function usage.
  • STEP is used to specify the increment value. If the step is not specified the default value will be 1 . We can also specify the STEP negative in order to decrement from given START number to the STOP number.

Create Simple Sequence From 1 to 10

We will start using range() function in order to create a simple sequence. We will just specify the START and STOP. We will start at 1 and increment to the 10 with one by one.

Create Simple Sequence From 1 to 10

Create Simple Sequence From 1 to 10

Create Sequence Which Increments 2

In the previous example, we will use 1 as an increment value. We can specify the increment value explicit the value we want. We will just add the increment value as 3rd parameter to the range() function. In this example, we will start at 0 and count to 10 by incrementing 2.

LEARN MORE  For Loops In Windows With MS-DOS Command Prompt

Create Sequence Which Increments 2

Create Sequence Which Increments 2

Create List From range()

As stated previously range() function is computed during executions of the code which means if we do not run range() and set to a different type like list it will be just a text. Let’s look below code which is just printed the range(0,10) not the sequence.

Create List From range()

Create List From range()

So we need to create new data structures from the range() function. range() function will literally create a list where we can use the created sequence as a list like below.

Create List From range()

Create List From range()

Iterate or Loop with range() Function

range() function creates sequences and these sequences generally used to loop or iterate. We can use range() function inside a loop statement like for, while etc to iterate over elements of the sequence. In this example, we will iterate from 1 to the 20 with range function in a for loop.

Iterate or Loop with range() Function

Iterate or Loop with range() Function

Default Start Number Of range() Function

As stated in the syntax part only STOP number is must in range() function. So we do not have to provide the start number to the range() function. The default start number is 0 . In this example, we will use the default start number and only specify the end number as 20.

Default Start Number Of range() Function

Default Start Number Of range() Function

Create Sequence Only Setting End Number

We can create a sequence with the range() function by only setting the stop or end number. In this example, we will start by default start number which 0 and increment to the 20.

LEARN MORE  Python Type Function with Examples

Create Sequence Only Setting End Number

Create Sequence Only Setting End Number

Create Backward Sequence or Negative Step

Up to now, we have look how to create sequences with forwarding or positive steps. range() function also supports negative steps where the sequence will start from start number and take negative steps or decrement to the stop number. In this example, we will start from -2 and decrement to the -10 one by one. We have to also specify the decrement value which is -1

Create Backward Sequence or Negative Step

Create Backward Sequence or Negative Step

range() vs xrange()

range() is a function provided in Python3 but in Python version 2 xrange() was used popularly. xrange() function is eliminated in Python3 so we can not use it. range() function also provides very efficient executaion to the xrange() function.

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

You may also like...

Leave a Reply

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

Open

Enjoy this blog? Please spread the word :)