设为首页 加入收藏

TOP

Python 循环中的else
2014-11-24 03:14:27 来源: 作者: 【 】 浏览:1
Tags:Python 循环 else

众多语言中都有if else这对条件选择组合,但是在python中还有更多else使用的地方,比如说循环for,或者while都可以和else组合。


下面简单介绍一下for-else while-else组合


循环组合中的else执行的情况下是循环正常结束(即不是使用break退出)。如下列代码:


numbers = [1,2,3,4,5]
for n in numbers:
if (n > 5):
print('the value is %d '%(n))
break
else:
print('the for loop does not end with break')

i = 0
while(numbers[i] < 5):
print('the index %d value is %d'%(i, numbers[i]))
if (numbers[i] < 0) :
break
i = i + 1
else:
print('the loop does not end with break')


执行结果如下:


C:\Python27>python.exe for_else.py
the for loop does not end with break
the index 0 value is 1
the index 1 value is 2
the index 2 value is 3
the index 3 value is 4
the loop does not end with break


推荐阅读:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇使用Python批量删除文件列表 下一篇Java中数组的比较的真相

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python中文网 - 人生 (2025-12-24 18:49:47)
·【整整648集】这绝对 (2025-12-24 18:49:44)
·Python超详细一条龙 (2025-12-24 18:49:42)
·【超详细】JDK 下载 (2025-12-24 18:19:32)
·Java_百度百科 (2025-12-24 18:19:29)