Home  >  Article  >  Backend Development  >  What does python range mean?

What does python range mean?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-20 11:54:057591browse

Detailed usage of python range() function

Function prototype: range(start, end, scan):

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

end: counting to the end of end, but not including end. For example: range (0, 5) is [0, 1, 2, 3 , 4] No 5

scan: The distance between each jump, the default is 1. For example: range(0, 5) is equivalent to range(0, 5, 1)

What does python range mean?

##Basic usage example:

Python
>>> for i in range(1,5):   
print(i)
1
2
3
4
>>> for i in range(5):    
print(i)
0
1
2
3
4
>>> for i in range(0,5,2):    
print(i)
0
2
4
>>> for i in range(0,-5,-2):
print(i)
0
-2
-4

Related recommendations:《

Python video tutorial

Note:

The difference between python2 and python3:

python3 returns an iteration value:

Python3

print(range(1,5))
range(1, 5)
# 正确打印方法
list(range(1,5))
[1, 2, 3, 4]

python2 returns a list:


Python2

>>> range(1,5)
[1, 2, 3, 4]

The above is the detailed content of What does python range mean?. 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