python range实例用法分享
作者:藏色散人 时间:2021-09-28 02:54:46
Python range() 函数用法
python range() 函数可创建一个整数列表,一般用在 for 循环中。
函数语法
range(start, stop[, step])
参数说明:
start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);
stop: 计数到 stop 结束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
step:步长,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
实例
>>>range(10) # 从 0 开始到 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11) # 从 1 开始到 11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5) # 步长为 5
[0, 5, 10, 15, 20, 25]
>>> range(0, 10, 3) # 步长为 3
[0, 3, 6, 9]
>>> range(0, -10, -1) # 负数
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> range(0)
[]
>>> range(1, 0)
[]
以下是 range 在 for 中的使用,循环出runoob 的每个字母:
>>>x = 'runoob'
>>> for i in range(len(x)) :
... print(x[i])
...
r
u
n
o
o
b
>>>
来源:https://www.php.cn/python-tutorials-442186.html
标签:python,range
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python通过配置文件共享全局变量的实例
2023-06-08 03:10:29
python从PDF中提取数据的示例
2021-10-15 21:46:05
![](https://img.aspxhome.com/file/2023/2/99462_0s.png)
解决python中导入win32com.client出错的问题
2023-02-17 11:01:45
DW表格应用之细线框的制作
2008-02-03 19:00:00
![](https://img.aspxhome.com/file/UploadPic/20082/3/20082319946365s.gif)
基于Python编写简易文字语音转换器
2023-12-28 19:24:54
![](https://img.aspxhome.com/file/2023/2/131122_0s.png)
asp查询ip地址源代码
2009-07-27 17:51:00
浅析Python面向对象编程
2023-11-23 07:52:12
Python实现Wordcloud生成词云图的示例
2021-09-06 05:29:15
![](https://img.aspxhome.com/file/2023/4/81874_0s.jpg)
Python面向对象之成员相关知识总结
2022-09-18 22:27:27
python中numpy数组与list相互转换实例方法
2023-11-29 01:48:50
Python实现GPU加速的基本操作
2021-07-30 05:10:34
![](https://img.aspxhome.com/file/2023/6/101136_0s.png)
python实现会员管理系统
2023-11-13 19:44:46
![](https://img.aspxhome.com/file/2023/2/65872_0s.jpg)
跟老齐学Python之dict()的操作方法
2022-05-12 16:54:43
网站设计趋势: iPhones[译]
2009-09-21 12:40:00
![](https://img.aspxhome.com/file/UploadPic/20099/21/iphone-45s.jpg)
从品牌网站看交互设计
2009-08-18 12:39:00
![](https://img.aspxhome.com/file/UploadPic/20098/18/1-31s.jpg)
GC与JS内存泄露
2010-09-25 19:01:00
![](https://img.aspxhome.com/file/UploadPic/20109/25/01-21s.jpg)
一文带你了解Python中的字符串是什么
2021-10-16 06:05:27
![](https://img.aspxhome.com/file/2023/7/88987_0s.png)
Numpy中如何创建矩阵并等间隔抽取数据
2023-01-14 16:21:16
![](https://img.aspxhome.com/file/2023/0/81780_0s.png)
python使用opencv驱动摄像头的方法
2023-08-26 17:00:49
pytorch网络模型构建场景的问题介绍
2022-07-24 22:38:42