1 Star 0 Fork 1

pish7/stdpython

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
bool.py 1.47 KB
Copy Edit Raw Blame History
pish7 authored 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/pish7/stdpython.git
git@gitee.com:pish7/stdpython.git
pish7
stdpython
stdpython
master

Search