009 python语法_类 range

 

 

'''
时间:2018/10/28
目录: 
  一: 概述
        1 help查看
        2 文件查看
        3 类型查看
  二: 使用
        1 说明
        2 举例
'''

 

一: 概述
  1 help查看

# coding:utf-8

help(range)
Help on class range in module builtins:

class range(object)
 |  range(stop) -> range object
 |  range(start, stop[, step]) -> range object

 

  2 文件查看

    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).

 

  3 类型查看

# coding:utf-8

print(type(range))
<class 'type'>

 

二: 使用
  1 说明

'''
创建一个整数列表,一般用在 for 循环中。

range(start, stop, step)
    start: 开始数,默认是0。
    stop: 结束数。
    step:变化数,默认为1。
    
例如: range(5), 等价于range(0,5);
例如: range(0, 5), 内容是是[0, 1, 2, 3, 4]没有5
例如: range(0, 5), 等价于range(0, 5, 1)

'''

 

  2 举例

# coding:utf-8

print(range(10))
print(type(range(10)))
range(0, 10)
<class 'range'>

 

# coding:utf-8

for nLoop in range(5):
    print("%d " %nLoop, end="")
print()

for nLoop in range(1, 5):
    print("%d " %nLoop, end="")
0 1 2 3 4 
1 2 3 4

 

# coding:utf-8

for nLoop in range(0, 30, 5):
    print("%d " %nLoop, end="")
print()

for nLoop in range(0, 10, 3):
    print("%d " %nLoop, end="")
print()

for nLoop in range(0, -10, -1):
    print("%d " %nLoop, end="")
0 5 10 15 20 25 
0 3 6 9 
0 -1 -2 -3 -4 -5 -6 -7 -8 -9 

 

# coding:utf-8

strString = "runoob"
for nLoop in range(len(strString)):
    print("%s" %(strString[nLoop]), end="")
runoob

 

posted @ 2018-10-28 22:31  火焰马  阅读(231)  评论(0编辑  收藏  举报