Python else语句
2017-02-26 12:44
323 查看
Python else语句
python的else和其他语言不通,通常if-else是一起使用的,但是python中的else语句还有其他几种用法。用法1 if-else
若果if条件为真则运行
if中的语句,如果为假则运行
else中的语句。
if True: print('True') else: print('False') ''' 结果:True '''
用法2 try-except-else
如果try中的语句有异常则运行
except中的代码,没有异常则运行
else的代码
try: 1/2 except Exception as e: print(e) else: print('else') ''' 结果:else '''
用法3 for else
如果for正常运行完毕则会执行
else,如果
for因为
break跳出循环则
else不会执行。
for x in range(5): print(x) else: print("normal end") ''' 结果: 0 1 2 3 4 normal end ''' for x in range(5): print(x) break else: print("normal end") ''' 结果:0 '''
相关文章推荐
- Python条件语句if…else…
- Python开发:关于Else语句的总体理解
- Python3基础 else 语句 与try-except配合
- Python 中的else语句
- Python中在for循环中嵌套使用if和else语句的技巧
- Python入门--19--else语句、with语句
- Python中else语句整理
- Python中的条件分支语句,if , else , else if
- Python语法基础(四):if-else语句
- Python 3从入门到精通6-if,elif,else语句
- Python中的if、else、elif语句用法简明讲解
- python中的try/except/else/finally语句
- Python中在for循环中嵌套使用if和else语句的技巧
- Python中if-else语句的多种写法
- Python 条件判断语句(if ,elif, else)
- python流程控制语句 ifelse - 4
- Python--小甲鱼学习笔记--第34课:else语句及with语句
- python中的try/except/else/finally语句
- Python中else语句整理
- Python的for和break循环结构中使用else语句的技巧