Fetch the repository succeeded.
# 值为 False 的表达式包括:
# False None 0 "" () [] {}
# 其他值都视为 True
# True 和 False 不过是 0 和 1 的别名
print(bool(None)) # False
print(True == 1) # True
print(True == 2) # False
print(False == 0) # True
print(False + True + 2) # 3
# 条件表达式,类似于三目运算符
a = True
b = "a" if a else "b"
print(b) # a
if True:
"""""" # 用一个字符串来起到空语句的作用
if True:
pass # 空语句
x = [1, 2, 3]
y = [1, 2, 3]
z = x
# 相等
if x == y:
print("x=y") #
else:
print("x!=y")
# x和y是同一个对象
if x is y:
print("x is y")
else:
print("x is not y") #
# x和y不是同一个对象
if x is not z:
print("x is not z")
else:
print("x is z") #
# 1是容器(如序列)x的成员
if 1 in x:
print("1 in x") #
else:
print("1 not in x")
# 2不是容器(如序列)x的成员
if 2 not in x:
print("2 not in x")
else:
print("2 in x") #
x = "abc"
y = "def"
z = x or y
print(z) # abc
x = "abc"
y = "def"
z = x and y
print(z) # def
x = None
y = "def"
z = x and y
print(z) # None
y = "def"
z = not y
print(z) # False
# 链式比较
a = 3
if 0 <= a <= 9:
print("ok")
# 断言
age = 10
assert 0 < age
# 断言测试不通过时将抛出异常
try:
age = -1
assert 0 < age, "年龄必须为整数"
except Exception as e:
print(e) # 年龄必须为整数
x = 0
y = 1
assert x < y # 通过
assert x or y # 通过
# assert x and y #异常
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。