python中的range()函数的功能hen强大,所以我觉得很有必要和大家分享一下

就好像其API中所描述的:

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

下面是我做的demo:

 #如果你需要遍历一个数字序列,可以是使用python中内建的函数range()

 #如下面要遍历一个列表test_list
test_list = [1,3,4,'Hongten',3,6,23,'hello',2]
for i in range(len(test_list)):
print(test_list[i],end=',') print()
print('#####################################') #或者用range()函数生成一个列表
for i in range(5):
print(i,end=',') print()
print('#####################################') #python中的内置函数range(10),其中参数'10'代表:从0到10的一个序列
#即长度为10的一个序列
print('range(10)表示:',range(10))
listA = [i for i in range(10)]
print(listA) print('#####################################') #当然,我们可以自定义我们需要的起始点和结束点
#我们定义了一个从5开始的起始点,到100结束的结束点
print('range(5,100)表示:',range(5,100))
listB = [i for i in range(5,100)]
print(listB) print('#####################################') #定义了这些后,我们还可以定义步长
#下面我们定义一个从1开始到30结束,步长为3的列表
print('range(1,30,3)表示:',range(1,30,3))
listC = [i for i in range(1,30,3)]
print(listC)

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
1,3,4,Hongten,3,6,23,hello,2,
#####################################
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]
>>>

========================================================

More reading,and english is important.

I'm Hongten

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

========================================================

python开发_python中的range()函数的更多相关文章

  1. python开发_python中的函数定义

    下面是我做的几个用列: #python中的函数定义,使用和传参 def_str = '''\ python中的函数以如下形式声明: def 函数名称([参数1,参数2,参数3......]): 执行语 ...

  2. python开发_python中str.format()

    格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看python中的字符串格式函数str.format(): 1 #使用str.format()函数 2 3 #使用 ...

  3. python开发_python中字符串string操作

    在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号' ...

  4. python开发_python中的list操作

    对python中list的操作,大家可以参考: Python list 操作 以下是我个人的笔记: ============================================ Add b ...

  5. python开发_python中的Boolean运算和真假值

    python中的真假值: Truth Value Testing Any object can be tested for truth value, for use in an if or while ...

  6. python开发_python中的module

    在python中,我们可以把一些功能模块化,就有一点类似于java中,把一些功能相关或者相同的代码放到一起,这样我们需要用的时候,就可以直接调用了 这样做的好处: 1,只要写好了一个功能模块,就可以在 ...

  7. python开发_python中的变量:全局变量和局部变量

    如果你在为python中的变量:全局变量和局部变量头疼,我想这篇blog会给你帮助 运行效果: 代码部分: #Python中的变量:全局变量和局部变量 #在很多语言中,在声明全局变量的时候,都喜欢把全 ...

  8. python开发_python中for循环操作

    如果你对python中的for循环不是很清楚,请看看这篇文章:”for循环控制语句——菜鸟的Python笔记“ 下面是我做的一些学习记录供大家参考: #基本的for循环语句 test_list = [ ...

  9. python开发_python关键字

    python3.3.2中的关键字如下: The following identifiers are used as reserved words, or keywords of the languag ...

随机推荐

  1. grunt实现修改代码实时刷新浏览器

    grunt例子:https://github.com/Aquarius1993/gruntDemo grunt 实时刷新1:           1.安装chrome浏览器插件:liveReload ...

  2. 并查集实现Tarjan算法

    本文是对http://noalgo.info/476.html的一点理解,特别是对其中 int father[mx]: //节点的父亲 int ancestor[mx]; //已访问节点集合的祖先 这 ...

  3. a链接嵌套无效,嵌套链接最优解决办法

    <a>不支持嵌套.例如: <a href="#1">11111111111<a href="#2">22222222222& ...

  4. 在 Windows 安装期间将 MBR 磁盘转换为 GPT 磁盘

    以 UEFI 启动的 Windows 磁盘必须是 GPT 格式.本文将介绍如何在安装 Windows 期间将磁盘从 MBR 转换成 GPT. 特别注意:操作不慎可能丢失所有数据,如果你懂得安装系统的一 ...

  5. hadoop常见错误总结三

    问题导读:1.... could only be replicated to 0 nodes, instead of 1 ...可能的原因是什么?2.Error: java.lang.NullPoin ...

  6. C#中 this关键字 四种用法

    /// <summary> /// 主程序入口 /// </summary> /// <param name="args"></param ...

  7. tomcat  nginx  证书切换

    1. 导出公钥 keytool -export -alias tomcat -keystore <you jks>wsriakey.keystore -file <outputfil ...

  8. FastAdmin 源码分析:jQuery 含逗号的选择器

    FastAdmin 源码分析:jQuery 含逗号的选择器 在 FastAdmin 你常常会看到以下 jQuery 选择器的代码. if ($(".datetimepicker", ...

  9. oracle之 11g RAC R2 体系结构---Grid

    -- 查看cluster 所维护的资源列表,不包括 OHAS 栈的 daemon [root@node1 bin]# ./crsctl status resource -t-------------- ...

  10. 搭建一个IntelliJ的Spark项目

    之前发现创建一个新项目之后,无法添加scala class 创建新项目 选择maven项目,然后选择simple或者quickstart: 进入项目后,在Project Structure里面,在gl ...