Python else语句
来源:互联网 发布:方便面怎么煮好吃知乎 编辑:程序博客网 时间:2024/06/06 00:12
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/2except 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")'''结果:01234normal end'''for x in range(5): print(x) breakelse: print("normal end")'''结果:0'''
0 0
- Python 中的else语句
- Python else语句
- Python中else语句整理
- Python中else语句整理
- Python try-except-else 语句
- Python中else语句整理
- Python循环中的else语句
- python学习:for else语句
- Python if elif else语句
- 3.1 Python if else语句
- python while语句 else if 语句
- Python中的条件分支语句,if , else , else if
- [Python]循环语句中的 else 子句
- Python-if-elif-else语句
- python中的try/except/else/finally语句
- python中else语句的用法
- Python else与elif语句语法讲解
- Python条件语句if…else…
- K上升序列总和
- 广义线性模型
- 自己动手搭梯子——从vps到SSR菜鸟教程
- 278. First Bad Version
- POJ 2125 Destroying The Graph 二分图 最小点权覆盖
- Python else语句
- 入门训练 Fibonacci数列
- 【详解】嵌入式开发中固件的烧录方式
- java输入输出(二)
- 内核链
- Web基础之CSS3
- 268. Missing Number
- Hexo 博客空间
- 198. House Robber&&213. House Robber II