Home > Backend Development > Python Tutorial > The difference between range function python2 and 3

The difference between range function python2 and 3

(*-*)浩
Release: 2019-07-05 10:36:25
Original
3512 people have browsed it

The range function is a general function used to create arithmetic series sequences, returning an integer sequence of [start, start step, start 2 * step, ...] structure;

The difference between range function python2 and 3

Range() function usage in py2: (Recommended learning: Python video tutorial)

range() return is a list

>>> list=range(10)
>>> print list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy after login

The range() function usage in py3:

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

list() function is an object iterator, converting the object into a list. The returned variable type is a list.

>>> range(10)

range(0, 10)

>>> type(range(10))

<class &#39;range&#39;>

>>> list(range(10))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> type(list(range(10)))

<class &#39;list&#39;>
Copy after login

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of The difference between range function python2 and 3. 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