Python程式設計基礎學習筆記 運算子

2021-08-22 03:31:12 字數 2158 閱讀 3357

#彙總一下

#算術運算子:加號+,減號-,乘號*,除號/,//除號取整,求餘數%,次方**

#賦值運算子:=,+=加法賦值,-=減法賦值,*=,/=,%=,**=,//=

#關係運算子/比較運算子:==等於,!=不等於,>,<,>=,<=

#邏輯運算子:and,or,not

#成員運算子:in,not in

#身份運算子:is,is not

#按位運算子:&,|,^,~,<<,>>

#python裡沒有自加自減運算子a++,a--

#關係運算子

b=1b+=b>=1

print(b)#2

int(true)#1

#字串比較

'a'<'b'#true,ord

ord('a')#97

ord('b')#98

'ade'<'acf'#false,d>c

#列表比較

[1,3,4]<[1,2,9]#false

#元組比較

(1,3,4)<(1,2,9)#false

#邏輯運算子and且,or或,not非,(bool型別)

true and true#true

true and false#false

true or false#true

not true

#int float,0被認為是false,非0被認為是true

#空字串false,字串true

#空列表false,列表true

#tuple,set,dict同上

or [1]#[1]

2 and 1#1,先讀取2true,再讀取1true,判斷true,返回最後讀取值

2 and 3#3,

2 or 1#2,先讀取2true,直接判斷true,返回讀取值

2 or 3#2,

#成員運算子

a=1b=4

a in [1,2,3]#true

b in [1,2,3]#false

a not in [1,2,3]#false

'h' in '121h51'#true

1 in (1,2,3)#true

1 in #true

#key:value,針對key

1 in #false

'a' in #true

#身份運算子

1 is 2#false

2 is 2#true

'xia' is 'xia'#true

'xia' is not 'x'#true

#與==的區別,==比較值是否相等、is比較身份是否相等

1==1.0#true

1 is 1.0#false,因為id(1)不等於id(1.0)

#無序和有序的區別

a=b=

a==b#true

a is b#false,id(a)!=id(b)

c=(1,2,3)

d=(2,1,3)

c==d#false

c is d#false,id(c)!=id(d)

############值判斷,身份判斷,+型別type判斷

############物件的三個特徵:值value>==,身份id>is,型別type>isinstance

#型別type判斷

type('a')==int#false不推薦

type('a')==str#true不推薦

isinstance('xia',str)#true推薦

isinstance('xia',int)#false推薦

isinstance('xia',(int,str,float))#true

isinstance('xia',(int,float))#false

#位運算子

#&按位與|按位或^按位異或~按位取反《左移動》右移動

#都是當作二進位制數進行運算

#按位與

2 & 3#2,二進位制10和11,他們第一位數分別是0和1取0,第二位數分別是1和1取1

9 & 13#9

bin(9)#'0b1001'

bin(13)#'0b1101'

#按位或

9 | 13#13

#位運算子之間規則不同,但是規律都是一樣的

Python程式設計基礎學習筆記

除法運算陷阱 預設為整除,1.0 2 0.51.7 在互動式直譯器idle中執行if語句時,要按兩次回車才能執行 內建函式 如 pow floor 向下取整函式不是內建函式,需要呼叫模組math才能用 匯入模組方法 import math math.floor 3.3 math.floor 3.3 ...

python學習筆記 python程式設計基礎

一.乙個隆重的儀式 我們在學習語言的時候,第乙個寫的程式肯定都是hello world.來寫第乙個程式吧,其實很簡單,python的語法就是簡單 優雅,乙個print就搞定。1 print hello world 二.基本輸入輸出 1.輸入 a input 請輸入資料 請輸入資料 2.輸出 prin...

Python基礎例項程式設計 學習筆記

折騰了乙個下午的 django 和 pycharm 找了半天發現community版本根本不能使用django 後來有想了半天的辦法才折騰好配置的環境,終於要開始python的篇章了。鑑於我的python其實學的也不是很紮實,所以一邊學習django的框架,一邊加固自己python的學習水平。因為這...