關於簡單的運算、二進制運算、邏輯運算符、成員運算符、身份運算符

簡單的運算

>>> 1+1 

2

>>> 2-1

1

>>> 2*3

6

>>> 2/1

2.0

>>> 5%4     <====返回除法的餘數

1

>>> 2**2    <====冪次方

4

>>> 5//2    <====關於取商,只取整數部分

2

>>> 5==5

True

>>> 5!=4    <====關於不等於

True

>>> 5>4

True

>>> 4<5 

True

>>> 5>=5

True

>>> 4<=4

True


關於二進制運算

運算符     描述       實例

&      按位與運算符   10&50得出結果爲2  假如A爲00001010 B爲00110010 就是說A和B中相同的位都爲真才爲真就是1

|      按位或運算符   10|50得出結果爲58 假如A爲00001010 B爲00110010 就是說A和B中相同的位只要有一位爲真才爲真就是1

^      按位異或運算符  10^50得出結果爲1=56 假如A爲00001010 B爲00110010 就是說A和B中相同的位只要有一位爲真才爲真就是1,都爲真的話就是假代表0,都爲假的話就是假代表0

>>     右移動運算符   假如A爲00001010按照右移一位的話結果就是00000101代表5

<<     左移動運算符   假如A爲00001010按照左移一位的話結果就是00010100代表5


邏輯運算符

運算符   描述  

and    布爾"與",如果x爲false,x and y返回false

or     布爾"或",如果x爲true,它就返回true

not    布爾"非",如果x爲true,它就返回false


成員運算符

運算符    

in       

not in

舉例

the number 1:

name=['alex','rain']
if 'jack' in name:
    print("pengchun is handsome")
else:
    print("pengchun is very handsome")
得出結果pengchun is very handsome

the number 2:

name=['alex','rain']
if 'alex' in name:
    print("pengchun is handsome")
else:
    print("pengchun is very handsome")

 得出結果pengchun is handsome


身份運算符

is  

is not

舉例:

the number 1

if type(3) is int:
  print("pengchun is handsome")
得出結果pengchun is handsome

the number 2 
 if type(3) is not list:
    print("pengchun is handsome")
 得出結果pengchun is handsome
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章