分支语句:也就是我们所说的 if-else 语句
- 对于一个含三元的代码,我们可以将其压缩成一行语句,如下:(三元操作符)
>>> a,b = 1,10
>>> if a > b:max = a
else:max = b
>>> max
10
压缩成一条语句:
>>> max = a if a>b else b
>>> max
10
- 断言(assert):学过 Java 的同学很好理解,就是抛出异常
举个例子:3 > 4 在我们正常情况下看来是错的,assert 就会抛出异常
如果是正确的,就会跳过,继续...
>>> assert 3 > 4
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
assert 3 > 4
AssertionError
>>> assert 3 < 4
>>>