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

详解python的循环

   来源:人文屋    阅读: 8.04K 次
字号:

用手机扫描二维码 在手机上继续观看

手机查看
<link rel="stylesheet" href="https://js.how234.com/9f362dbada/863c2fa7d91986d66756678ecdc06c33c7/863138bfdd12/862d02bcca04.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/9f362dbada/863c2fa7d91986d66756678ecdc06c33c7/863138bfdd12/862d15bbdd0c8bfb65586a92c6dc.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张

总结

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

学习教育
IT科技
财经金融
旅游出行
生活妙招
美食美味
健康养生
体育健身