Python-10-条件和条件语句

num = int(input('Enter a number: '))
if num > 0:
    print('The number is positive')
elif num < 0:
    print('The number is negative')
else:
    print('The number is zero')
 
x == y x 等于y
x < y x小于y
x > y x大于y
x >= y x大于或等于y
x <= y x小于或等于y
x != y x不等于y
x is y x和y是同一个对象
x is not y x和y是不同的对象
x in y x是容器(如序列) y的成员
x not in y x不是容器(如序列) y的成员
同时使用多个比较运算符,如0 < age < 100
布尔运算符and,or,not
 
断言
工作原理类似这样:
if not condition:
    rash program
在错误条件出现时就崩溃胜过以后再崩溃
可以添加assert语句充当检查点
>>> age = 10
>>> assert 0 < age < 100
>>> age = -1
>>> assert 0 < age < 100
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError
还可以在后面加一个字符串,对断言加以说明
>>> age = -1
>>> assert 0 < age < 100, 'The age must be realistic'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError: The age must be realistic
 
 

猜你喜欢

转载自www.cnblogs.com/swefii/p/10828464.html