说明:这个问题是如何在一些场景下使用断言表达式,通常会有人误用它,所以我决定写一篇文章来说明何时使用断言,什么时候不用。
为那些还不清楚它的人,Python的assert是用来检查一个条件,如果它为真,就不做任何事。如果它为假,则会抛出AssertError并且包含错误信息。例如:
py> x = 23
py> assert x > 0, "x is not zero or negative"
py> assert x%2 == 0, "x is not an even <weixin_38733281> 上传 | 大小:77kb
说明:第一题: give you two var a and b, print the value of a+b, just do it!
根据提议,给出两个变量 a 和 b 并打印出 a+b的值.
a, b = 1, 2
print a + b
当然也可以这么做
a = 1
b = 2
print a + b
第二题: 给你一个list, 如 L = [2, 8, 3, 5], 对L进行升序排序并输出。
L = sorted(L)
print L
#或
# sort() 内置函数会对列表自 <weixin_38589150> 上传 | 大小:50kb