Home > Backend Development > Python Tutorial > How to use range in python

How to use range in python

silencement
Release: 2019-06-20 13:39:11
Original
2436 people have browsed it

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])
Copy after login

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))
[]
>>>
Copy after login

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))
[]
>>>
>>>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template