如何在 Python 中使用 else 条件语句和 for 循环?
循环中的 else 块(for 以及 while)在循环的所有迭代完成后以及程序流程退出循环主体之前执行。语法如下 -
语法
while expr==True: #statements to be iterated while expr is true. else: #this statement(s) will be executed afteriterations are over
#程序退出循环主体后会执行此语句
示例
for x in range(6):
print (x)
else:
print ("else block of loop")
print ("loop is over")输出
输出如下 -
0 1 2 3 4 5 else block of loop loop is over
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP