Change language

Python | range () method

| | |

range() is usually used for loops, so knowing the same is key when working with any kind of Python code.

Syntax: range (start, stop, step)

Parameters:
start: Element from which sequence constructed has to start. (default: 0)
stop: Element number at which numbers in sequence have to end (exclusive).
step: Can be + ve or -ve number, denoting the elements need to be skipped during filling of list. (default: 1)

Returns: The list using the formula:
list [n] = start + step * n (for both positive and negative step) where , n" = 0 and list [n] = 0 and list [n]" stop (for negative step)

Returns ValueError if step is 0. Value constraint is checked in case of step, failing to meet returns empty sequence, else returns sequence according to formula.

Code # 1: Demonstration of range () without step parameter

# Python3 demo code
# working range () without step

 
# using range ()

lis1 = list ( range ( 6 ))

lis2 = list ( range ( 3 , 6 ))

lis3 = list ( range ( - 6 , 2 ))

  
# list initialization using a range, 1 parameter
# stop parameter

print ( "List generated using 1 parameter: " + str (lis1)) 

  
# initialize the list using a range, 2 parameters
# only step and stop parameters

print ( "List generated using 2 parameters: " + str (lis2)) 

 
# list initialization using a range, 2 parameters
# only step and stop parameters

print ( "List generated using 2 parameters with negatives:" + str (lis3)) 

Output:

 List generated using 1 p arameter: [0, 1, 2, 3, 4, 5] List generated using 2 parameters: [3, 4, 5] List generated using 2 parameters with negatives: [-6, -5, -4, -3, - 2, -1, 0, 1] 

Code # 2: Demonstration of range () using a step

# Python 3 demo code
# working range () in increments

 
# initializing the list using a range
# using a step

print ( "List generated using step:" +

str ( list ( range ( 3 , 10 , 2 ))) ) 

 
# list initialization using a range
# using a negative step

print ( "List generated using negative step:" +

str ( list ( range ( 10 , - 5 ,  - 3 ))))

 
# initializing the list using a range
# using a negative step
# the value does not match the case

print ( "List generated using step, value contraints fail:" +

str ( list ( range ( 10 , - 5 , 3 ))))

 < / code> 
# initialize the list using a range
# using step 0
# error

print ( "List generated using 0 step:" +

str ( list ( range ( 3 , 7 , 0 ))))

Output:

 List generated using step: [3, 5, 7, 9] List generated using negative step: [10, 7, 4, 1, -2] List generated using step, value contraints fail: [ ] 
Exception:

 Traceback (most recent call last): File "/home/bdae725dff7b38d3681eee38f6a6d434.py", line 23, in print ("List generated using 0 step:" + str (list (range (3 , 7, 0)))) ValueError: range () arg 3 must not be zero 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically