python學習--運算子詳解

來源:互聯網
上載者:User

標籤:檔案   等於   .exe   運算   加法   二進位   htm   運算子   不同   

一、簡介

1.python 運算子分為

  • 算術運算子
  • 比較(關係)運算子
  • 賦值運算子
  • 邏輯運算子
  • 位元運算符
  • 成員運算子
  • 身份運算子
  • 運算子優先順序

 2.運算優先順序

  

**                        指數 (最高優先順序)~  +   -                 按位翻轉, 一元加號和減號 (最後兩個的方法名為 [email protected] 和 -@)*  /   %   //             乘,除,模數和取整除+   -                     加法減法>>    <<                  右移,左移運算子&                         位 ‘AND‘^   |                     位元運算符<=     < >    >=          比較子<>   ==     !=            等於運算子=    %=   /=   //=   -=   +=   *=   **=    賦值運算子is      is not            身份運算子in     not in             成員運算子not    and     or         邏輯運算子                    

3.執行個體詳解

‘‘‘位元運算符 : 按照二進位的每位上的數值進行運算位元運算符: & | ^  ~   <<  >> ‘‘‘print(True & True)# 按位& 將10進位數轉化為二進位  然後按二進位每位元運算   1 1 為  1 其餘都是0  print(‘2:‘,bin(2)) print(‘8:‘,bin(8))#2  0010#8  1000#10 1010#0  0000print(2&8)print(10&8)#|  按位或     只要有1 就為1#10 01010#20 10100#   11110 2+4+8+16print(10|20)#^異或符  不同為1 相同為0#10 01010#20 10100print(10^20)#8  01000#28 11100#20 10100print(8^28)# ~去反 0為1 1為0#001#110#4  0100  #  1011 -5#  -8 +2# print(bin(-5))print(~1)#  0101#    1010print(bin(~5))#010000#101111  -32 +8+4+2+1print(~16)#左移 被移動的數<<移動位元   右移 被移動的數>>移動位元 #1010#左移  1010000print(10<<3)#1010#右移  0001print(10>>3)#2^3 #010#1000print(2<<2)‘‘‘ ------------------------------------------------------------邏輯運算子bool and bool  , bool or bool ,  not bool‘‘‘a=20; b=30fl=a>10 and b<50print(fl)print(a>50 or b>10)print(a>10 or b<50)print(not a>10)print(not a>30 and b>10)print(a>20 and a<30 or a<50 )#邏輯運算子優先順序  not and or print(a<=20 and (a<30 or a<50) )‘‘‘---------------------------------------------------------------運算子擴充‘‘‘a=2b=2.0print(a is b)print(a==b)print("--------------------------------------------------------------------")a=0b=0a=1010888800b=1010888800print(a is b)print(2**8)#2^8    print(2**64)a=18446744073709551618b=18446744073709551618print(a is b )

運行結果:

E:\python_VS_code\directory[目錄]>D://py3.6//python.exe e:/python_VS_code/directory[目錄]/demo0731/py_typeid.pyTrue2: 0b108: 0b100008303020-2-0b110-178018TrueTrueTrueFalseTrueTrueTrueFalseTrue--------------------------------------------------------------------True25618446744073709551616True

註: 運算子中:(取反運算:取何數,數字為負並減一)

在命令列> 兩個整數大於28,也就是二的八次方,‘ is ‘ 為 Flase  ,  ‘ == ‘ 為 True

在指令檔中> 數字無論多大,都為True

【is 指向 記憶體位址

   == 指向 變數值】

python學習--運算子詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.