• Python中range()函数的用法


    Python中range()函数的用法

    1、函数原型:range(start, end, scan):

    参数含义:

    start:计数从start开始。默认是从0开始。例如range(5)等价于range(0, 5);

    end:技术到end结束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5

    scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)

    2、python中的range()函数的功能很强大,所以我觉得很有必要和大家分享一下,就好像其API中所描述的:

    If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy. It generates arithmetic progressions

    --有道翻译的结果:如果确实需要迭代一组数字,那么内置函数range()就派上用场了。它生成算术级数。

    3、实例调用

    复制代码
     1 #如果你需要遍历一个数字序列,可以使用内置函数range()
     2 
     3 #1、下面遍历一个列表
     4 the_count=[1,2,3,4,5,6]
     5 for number in the_count:
     6     print("this is count %d" % number)
     7 print("--------------------")
     8 
     9 #2、遍历一个混合列表
    10 list=[1,2,3,4,"zll",5,6,"hello",7,8.9]
    11 for i in range(len(list)):
    12     print (list[i],end="、")
    13 print("
    --------------------")
    14 
    15 #3、用range()函数生成一个列表
    16 for i in range(5):
    17     print(i,end="、")
    18 print("
    --------------------")
    19 
    20 #4、range(10),其中参数10代表:从0到10的一个序列,当然不包含10
    21 print("range(10)表示:" ,range(10))
    22 listA=[i for i in range(10)]
    23 print(listA)
    24 print("--------------------")
    25 
    26 #5、我们也可以自定义起始点和结束点
    27 #我们定义了一个从5开始的起始点,到100结束的结束点
    28 print("range(5,100)表示",range(5,100))
    29 listB=[i for i in range(5,100)]
    30 print(listB)
    31 print("--------------------")
    32 
    33 #6、定义了这些后,我们还可以定义步长
    34 #我们定义一个从1开始到30结束,步长为3的列表
    35 print('range(1,30,3)表示:',range(1,30,3))
    36 listC = [i for i in range(1,30,3)]
    37 print(listC)
    复制代码

    4、结果输出

    this is count 1
    this is count 2
    this is count 3
    this is count 4
    this is count 5
    this is count 6
    --------------------
    1、2、3、4、zll、5、6、hello、7、8.9、
    --------------------
    0、1、2、3、4、
    --------------------
    range(10)表示: range(0, 10)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    --------------------
    range(5,100)表示 range(5, 100)
    [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
    --------------------
    range(1,30,3)表示: range(1, 30, 3)
    [1, 4, 7, 10, 13, 16, 19, 22, 25, 28]

    ---------------------------------------------------------------------Take more responsibility!------------------------------------------------------------------
  • 相关阅读:
    git push提交出现Everything up-to-date提示问题
    启动Dubbo项目注册Zookeeper时提示zookeeper not connected异常原理解析
    linux环境搭建mysql5.7总结
    Hadoop学习笔记:运行wordcount对文件字符串进行统计案例
    kafka3.0创建topic出现zookeeper is not a recognized option
    sql_waf绕过
    win11环境映像劫持
    vulnhub靶场—devguru
    vulhub-Presidential靶场解题过程
    php命令执行无回显判断及利用方法
  • 原文地址:https://www.cnblogs.com/zide/p/13055393.html
Copyright © 2020-2023  润新知