python3之if语句介绍

获得更多资料欢迎进入我的网站或者 csdn或者博客园

本节主要介绍pythonif条件语句,以及用法。下面附有之前的文章;

if语句有个非常有用的近亲,其工作方式多少有点像下面这样(伪代码):
if not condition:
crash program
这样做是因为与其让程序在晚些时候崩溃,不如在错误条件出现时直接让它崩溃。一般来说,你可以要求某些条件必须为真。语句中使用的关键字为assert。
如果需要确保程序中的某个条件一定为真才能让程序正常工作的话,assert语句就有用了,可以在程序中置入检查点:
条件后可以添加字符串(用逗号把条件和字符串隔开),用来解释断言:

>>> age=-1
>>> assert 0<age<150,"The age must be realistic"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: The age must be realistic
>>> 
>>> age=10
>>> assert 0<age<150,"The age must be realistic"
>>> 

相关链接:

python3入门之类
python3入门之函数
python3入门之循环
python3之if语句
python3入门之赋值语句介绍
python3入门之print,import,input介绍
python3入门之set
python3入门之字典
python3入门之字符串
python3入门之列表和元组
python3入门之软件安装
python3爬虫之入门和正则表达式

文章导航