Home  >  Article  >  Backend Development  >  How to use range in python

How to use range in python

silencement
silencementOriginal
2019-06-20 13:39:112285browse

How to use range in python

Python3 range() function returns an iterable object (type is object), not a list type, so the list will not be printed when printing.

Python3 list() function is an object iterator, which can convert the iterable object returned by range() into a list, and the returned variable type is a list.

Python2 range() function returns a list.

Function syntax

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

Parameter description:

start: Counting starts from start. The default is to start from 0. For example, range(5) is equivalent to range(0, 5);

stop: counts to the end of stop, but does not include stop. For example: range (0, 5) is [0, 1, 2, 3, 4] without 5

step: step size, default is 1. For example: range(0, 5) is equivalent to range(0, 5, 1)

Instance

>>>range(5)
range(0, 5)
>>> for i in range(5):
...     print(i)
... 
0
1
2
3
4
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(0))
[]
>>>

has two parameters or three parameters (the second construction method) ::

>>>list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 2))
[0, 2, 4, 6, 8]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(1, 0))
[]
>>>
>>>

The above is the detailed content of How to use range in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn