python資料類型、操作符

來源:互聯網
上載者:User

標籤:

python中資料類型包含:int,float,boolean,string,list(列表),set(集合),dictionary(字典)

 

資料類型轉換:

①字串 轉 int:>>>string =‘123‘  >>>int(string)  輸出123

②int 轉 字串:>>>str(520)  輸出‘520’

③int 轉 浮點型:>>>float(99)  輸出99.0

④浮點 轉 int:>>>int(99.5)  輸出99

⑤擷取字串中字元的ASCII碼:>>>ord(‘A‘)  輸出65

⑥擷取ASCII碼:>>>chr(65)  輸出‘A’

 

資料類型驗證:type(object) 用於顯示object的資料類型

①整型:type(1)   輸出<class ‘int‘>

②浮點型:type(1.0)  輸出<class ‘float‘>

③布爾型:type(False)  輸出<class ‘bool‘>

④複數:type(12j+1)  輸出<class ‘complex‘>

⑤字串:type(‘xuexi‘)  輸出<class ‘str‘>

集合:type({1,2})  輸出<class ‘set‘>

列表:type([1,2,3])  輸出<class ‘list‘>

元組:type((1,2,3))  輸出<class ‘tuple‘>

字典:type({1:‘i‘,2:‘k‘})  輸出<class ‘dict‘>

 

特殊化:

①字串:type(‘‘)  輸出<class ‘str‘>

②元組:type(())  輸出<class ‘tuple‘>  x=(1,2,3)

③列表:type([])  輸出<class ‘list‘>  x=[‘alice‘,‘beth‘]

④集合:type(set())  輸出<class ‘set‘>  x={1,2}

⑤字典:type({})  輸出<class ‘dict‘>  x={‘alice‘ : ‘2341‘ , ‘beth‘ : ‘9109‘}

====================================================

1、in ,not in 操作符:

in 操作符可用於判斷所給資料是否包含於指定的數字序列、字串、列表、元組、集合中,或者判斷所給值是否包含於所給字典鍵中,not則相反

      eg1:>>>1 in range(0,5)  True

      eg2:>>>‘w‘ in {1 : ‘y‘ , 2 : ‘e‘ , 3 : ‘w‘}  False

        >>>1 in {1 : ‘y‘ , 2 : ‘e‘ , 3 : ‘w‘}  True

        以上說明對字典來說,僅可用於判斷所給值是否包含在集合的鍵中,即判斷鍵是否存在

      eg3:>>>‘mark‘ not in [‘t‘ , ‘ke‘]

 

2、is,is not操作符:是比較兩個對象是否是相同的對象

    eg:list1=[ ‘i‘ , ‘j‘ ]  list1=[ ‘i‘ , ‘j‘ ] 

      若list1==list2  True

      若list1 is list2  False  因為不在同一個列表中,儘管列表是相同的,而is是判斷是否為同一個對象

 

3、and、or、not操作符:分別代表邏輯 與,或,非;;and和or就是所謂的短路運算子,參數總是從左往右算,結果確定就停止。

  and運算:如果左邊的運算、值、或者對象為True,才會對右邊求值並返回右側計算結果,否則就停止運算並輸出運算式結果、值、對象

      eg1: >>>False and True  False

      eg2: >>>1 and 9+10  19  註:輸出結果為右側的值

      eg3: >>>5<2 and True  False

      eg3: >>>0 and True  0

  or運算:對or左邊的運算式求值,如果左邊的運算、值或者對象為True,輸出運算式結果,停止對右邊運算式求值;若果左邊為False,繼續對右邊的運算式求值並輸出結果

      eg1: >>>5+1 or False  6

      eg2: >>>False or 7+5  12

  not操作符:

      eg1: >>>not False  True

      eg2: >>>not 3<5  False

 

4、位元運算操作符

  ~取反操作符:對每一個二進位位+1取反,如果二進位為1,則結果為0

  |或操作符:兩個二進位,或運算,只要其中有一個為1則結果為1

  &與操作符:兩個二進位位,與運算

  ^異或

 

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.