python笔记-基础概念

Created at 2020-09-07 Updated at 2021-12-12 Category python Tag python

‘’ 与 “”

Python 中单引号与双引号完全相同。

is 和 ==

Python 中的对象都使用引用来表示,is 判断的是两个引用是否指向同一个对象(比较地址),== 判断两个引用指向的具体内容是否相等(比较值)。

None

在 Python 中 None是一个对象,判断一个变量是否是 None 的时候,用 ==也可以,通常使用is

# 变量 a 是 None
if a is None
# 变量 a 不是 None
if a is not None

bool()

bool()函数的用途是判断值是否是空,所有类型的默认空值都会返回 False,否则都是True

bool(None)# => False
bool(0)   # => False
bool("")  # => False
bool([])  # => False
bool({})  # => False
bool(())  # => False

三元表达式

'True' if a > b else 'False'

当 a > b 时返回 True,否则返回 False。

判断变量是否存在

var_exists = 'var' in locals() or 'var' in globals()

False

  1. 布尔型,False表示False,其他为True
  2. 整数和浮点数,0表示False,其他为True
  3. 字符串和类字符串类型(包括 bytes 和 unicode),空字符串表示False,其他为True
  4. 序列类型(包括tuple,list,dict,set等),空表示False,非空表示True
  5. None 永远表示 False

python里None 表示False吗? - 灵剑的回答 - 知乎 https://www.zhihu.com/question/48707732/answer/112233903

'',[],{},0,None # 都是False
a = ''
c = None
d = []
print not a # True
print not c # True
print not d # True
Site by Cellophane using Hexo & Random

Hide