Python学习笔记(3)range的用法
2015-11-08 10:55
295 查看
The Python
rang()函数用于方便地产生一个表,用例:
三种使用方法:
range(stop)
range(start, stop)
range(start, stop, step)
In all cases, the
stop. Each item increases by step.
如果缺省start 和step 则默认start=0,step=1
例(1):
结果:
例(2):
以上代码等价于:
结果:
range()function is just a shortcut for generating a list, so you can use ranges in all the same places you can use lists.
rang()函数用于方便地产生一个表,用例:
range(6) # => [0,1,2,3,4,5] range(1,6) # => [1,2,3,4,5] range(1,6,3) # => [1,4]
三种使用方法:
range(stop)
range(start, stop)
range(start, stop, step)
In all cases, the
range()function returns a list of numbers fromstart up to (but not including)
stop. Each item increases by step.
如果缺省start 和step 则默认start=0,step=1
例(1):
def my_function(x): for i in range(0, len(x)): x[i] = x[i] * 2 return x print my_function(range(3)) # Add your range between the parentheses!
结果:
[0, 2, 4]
例(2):
n = [3, 5, 7] def total(numbers): result=0 for item in range(len(numbers)): result+=numbers[item] return result print total(n)
以上代码等价于:
n = [3, 5, 7] def total(numbers): result=0 for item in numbers: result+=numbers[item] return result print total(n)
结果:
15
相关文章推荐
- [转]python os模块 常用命令
- Python入门篇之函数
- Python 基本类型转换
- Quant 应该学习哪些 Python 知识?
- python十分钟入门
- 量化分析师的Python日记【第6天:数据处理的瑞士军刀pandas下篇
- Python ConfigParser的使用
- 量化分析师的Python日记【第5天:数据处理的瑞士军刀pandas】
- 量化分析师的Python日记【第4天:一大波金融Library来袭之scipy篇】
- 浅析Python中的struct模块
- Python实现控制台中的进度条
- 4.2.3 标准编码
- [Python爬虫] scrapy爬虫系列 <一>.安装及入门介绍
- leetcode find median sorted arrays python
- NumPy-快速处理数据--ndarray对象--多维数组的存取、结构体数组存取、内存对齐、Numpy内存结构
- 开启Python
- [Python] Numpy学习笔记(二)
- python模拟登录
- Python实现模拟时钟代码推荐
- python 文件操作与函数式编程