python循环语句详细讲解
想必大家都知道python循环语句吧,可以python循环语句有多种,比如for循环、while循环、if、else等等,
![](https://upload-images.jianshu.io/upload_images/12900126-d956371fe63d9df6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
我们可以通过设置条件表达式永远不为 false 来实现无限循环,实例如下:
![](https://upload-images.jianshu.io/upload_images/12900126-9553bc05bdae0548.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/12900126-3ff8d368f3c8e4cd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/12900126-aaa71f80814ca4d5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
>>>for i in range(5,9) : print(i) 5678
>>>for i in range(0, 10, 3) : print(i) 0369
>>>for i in range(-10, -100, -30) : print(i) -10-40-70
![](https://upload-images.jianshu.io/upload_images/12900126-5669b4864d465d3a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
0 Google
1 Baidu
2 Runoob
3 Taobao
4 QQ
![](https://upload-images.jianshu.io/upload_images/12900126-5bae6254517f8ee3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/12900126-69bf349fdc0fcb20.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后继续进行下一轮循环
![](https://upload-images.jianshu.io/upload_images/12900126-7c7fd57feb93637a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/12900126-b118f69502527c0f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/12900126-f686e148f8aa18b6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/12900126-f29740cb9d283138.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Python pass是空语句,是为了保持程序结构的完整性。
pass 不做任何事情,一般用做占位语句,如下实例
![](https://upload-images.jianshu.io/upload_images/12900126-b59a6ac59ebd13d5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/12900126-0c30af49fa266ef5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
使用循环嵌套来实现99乘法法则
![](https://upload-images.jianshu.io/upload_images/12900126-5afbb809ad0d80ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
十进制转化
![](https://upload-images.jianshu.io/upload_images/12900126-e2efcc94f94dfae9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/12900126-2f67eed23f5207d0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
冒泡排序很经典的排序方式,从数组中的第0个元素开始,与后面一个元素进行比较,如果前面的元素大于后面的元素,就调换位置,循环到最后(即:a0与a1比较得到结果后,a1与a2比较...),最大的元素被换到数组最末尾,剔除掉最后一个元素,在余下的数组元素中进行上述操作,到最后,整个数组呈现从小到大的排序.
![](https://upload-images.jianshu.io/upload_images/12900126-2edff8884b0840a5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
---------------------喜欢就请留言、点击关注!