当前位置

首页 > 学习教育 > 详解python的循环

详解python的循环

推荐人: 来源: 秒知社 阅读: 8.1K 次
<link rel="stylesheet" href="https://js.how234.com/6b8361bf84/728963a2874662e9498c933d83c9cb54e4/728474ba834d/72984eb9945b.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/6b8361bf84/728963a2874662e9498c933d83c9cb54e4/728474ba834d/729859be83536fc44b829e2188d5.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><style>pre{overflow-x: auto}</style>

详解python的循环

range函数的使用

作为循环遍历的对象

详解python的循环 第2张

第一种创建方式

r=range(10)print(r)#range(0,10)print(list(r))

默认从零开始,默认步长为1

range(0, 10)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]第二种创建方式

指定了初始值1,到10结束,不包含10,默认步长为1

'''第二种创建方式,给了两个参数(小括号中给了两个数)'''r=range(1,10)print(list(r))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

第三种创建方式

最后一位数为步长

r=range(1,10,2)print(list(r))
[1, 3, 5, 7, 9]

判断指定的数有没有在当前序列中

r=range(1,10,2)print(10 in r)
False

循环结构

详解python的循环 第3张

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注好二三四的更多内容!