首页 > 编程语言 > 详细

Python range

时间:2019-05-27 14:02:08      阅读:92      评论:0      收藏:0      [点我收藏+]
"""
range(stop) -> range object
range(start, stop[, step]) -> range object

Return an object that produces a sequence of integers from start (inclusive)
to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.
start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.
These are exactly the valid indices for a list of 4 elements.
When step is given, it specifies the increment (or decrement).
"""
# range 返回的是range 对象,range(stop)
var = range(5)
print(var) # range(0, 5)
print(type(var)) # <class ‘range‘>
print(list(var)) # [0, 1, 2, 3, 4]

# range(start,stop)
var = range(10,15)
for ele in var:
    print(ele)
    # 10
    # 11
    # 12
    # 13
    # 14

# range (start,stop,step)
var = range(10,20,2)
for ele in var:
    print(ele)
    # 10
    # 11
    # 12
    # 13
    # 14
    # 10
    # 12
    # 14
    # 16
    # 18

# range() 和 len() 搭配,对一个序列的索引进行迭代
a = [This , is, a, range , test]
for ele in a:
    print(ele)
# This
# is
# a
# range
# test
for e in range(len(a)):
    print(e,a[e])
# 0 This
# 1 is
# 2 a
# 3 range
# 4 test

 

Python range

原文:https://www.cnblogs.com/pickKnow/p/10930195.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!