1 Star 0 Fork 1

aliao/stdpython

forked from pish7/stdpython 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
bool.py 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
pish7 提交于 2025-11-30 16:41 +08:00 . python
# 值为 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 #异常
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/aliaodc/stdpython.git
git@gitee.com:aliaodc/stdpython.git
aliaodc
stdpython
stdpython
master

搜索帮助