详解python的循环

来源:趣味经验馆 7.45K
<link rel="stylesheet" href="https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/3479402de34c/34657a2ef45a.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/3479402de34c/34656d29e352b082d56f16af81c7.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张

总结

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

热门标签