学无先后,达者为师

网站首页 Python教程 正文

详解python的循环_python

作者:是数学系的小孩儿   更新时间: 2022-03-19 Python教程

range函数的使用

作为循环遍历的对象

第一种创建方式

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

循环结构

总结

原文链接:https://blog.csdn.net/qq_51082388/article/details/122180543

栏目分类
最近更新