- if 语句
最让人耳熟能详的应该是 if 语句。例如:
>>>
>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print('Negative changed to zero')
... elif x == 0:
... print('Zero')
... elif x == 1:
... print('Single')
... else:
... print('More')
...
More
if 语句包含零个或多个 elif 子句及可选的 else 子句。关键字 ‘elif’ 是 ‘else if’ 的缩写,适用于避免过多的缩进。if … elif … elif … 序列可以当作其他语言中 switch 或 case 语句的替代品。
如果要把一个值与多个常量进行比较,或者检查特定类型或属性,match 语句更实用。



