Python運算符

is 和 is not 運算符

作用:
	判斷兩個對象是否是同一個對象,當是同一個對象時
	返回True,否則返回False

 	is not 與 is 的返回值相反
語法:
		x is y
		x is not y

id 函數:

作用:
 	返回一個對象在內存中的地址
說明:
	is運算符是根據id來進行判斷的
格式:
	id(對象)
**小整數對象池:**
	Python中,整數-5至 256 永遠存在於小整數對象池中
	不會釋放,並可重複使用

布爾運算:

運算符:
	not   and    or
	非     與     或

布爾非操作 not
	語法
		not x
作用:
	對x進行布爾取非,如bool(x) 爲True,則返回False,
	否則爲True
布爾與操作 and
	語法:
		x and y
	注: x,y 代表表達式

作用:
	優先返回假值對象,當x的布爾值爲False時,返回x
	否則返回y
示例:
	True and True    # True
	True and False   # False
	False and True   # False
	False and False  # False
	100 and 0.0      # 0.0
	0 and 0.0        # 0

布爾或操作 or
	語法:
	x or y
作用:
	優先返回真值對象,當x爲True時返回x,否則返回y
示例:
	True or True    # True
	True or False   # True
	False or True   # True
	False or False  # False
	3.14 or 100     # 3.14
	0 or 0.0        	 # 0.0

正負號運算符
	+(正號)  -負號
注: 一元運算符(一個元素參加運算)

語法:
	+ 表達式
	- 表達式
示例:
 	a = 5
 	b = -a  # -負號
	c = +a  # 正號
	print(a, b, c)  # 5 -5 5

in, not in 運算符

作用:
	in 用於序列,字典,集合中,用於判斷某個值是否存
	在於容器中,如果存在則返回True,則返回False
	not in 與 in 運算符的返回結果相反
格式:
	對象 in 序列
示例:
	x = 'welcome to tarena!'
	'to' in x     # True
	'hello' in x  # False
	'e t' in x    # True
	'abc' not in x  # True
發佈了32 篇原創文章 · 獲贊 5 · 訪問量 3153
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章