当前位置:懂科普 >

学习教育

> 详解python的循环

详解python的循环

<link rel="stylesheet" href="https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/260993986ac4/2615a99b7dd2.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/260993986ac4/2615be9c6ada531262c882c854df.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张

总结

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

标签: Python
  • 文章版权属于文章作者所有,转载请注明 https://dongkepu.com/xuexijiaoyu/vpowvz.html