<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python – 數據類型 > 原文: [https://howtodoinjava.com/python/python-data-types/](https://howtodoinjava.com/python/python-data-types/) 數據類型定義變量的類型。 由于所有內容都是 [Python](https://howtodoinjava.com/python-tutorial/) 中的對象,因此**數據類型**實際上是類; 變量是類的實例。 在任何編程語言中,都可以對不同類型的數據類型執行不同的操作,其中某些數據類型與其他數據類型相同,而某些數據類型可能非常特定于該特定數據類型。 ## 1\. Python 中的內置數據類型 Python 默認具有以下內置數據類型。 | 類別 | 數據類型/類名 | | --- | --- | | **文本/字符串** | [`str`](https://howtodoinjava.com/python/python-strings/) | | **數值** | [`int`](https://howtodoinjava.com/python/python-integer-ints/),`float`,`complex` | | **列表** | [`list`](https://howtodoinjava.com/python/python-lists/),[`tuple`](https://howtodoinjava.com/python/python-tuples/),`range` | | **映射** | `dict` | | **集合** | `set`,`frozenset` | | **布爾值** | `bool` | | **二進制** | `bytes`,`bytearray`,`memoryview` | ## 2.詳細的數據類型 #### 2.1 字符串 字符串可以定義為用單引號,雙引號或三引號引起來的字符序列。 三引號(`"""`)可用于編寫多行字符串。 ```py x = 'A' y = "B" z = """ C """ print(x) # prints A print(y) # prints B print(z) # prints C print(x + y) # prints AB - concatenation print(x*2) # prints AA - repeatition operator name = str('john') # Constructor sumOfItems = str(100) # type conversion from int to string ``` #### 2.2 整數,浮點數,復數 這些是數字類型。 它們是在將數字分配給變量時創建的。 * `int`保留不限長度的有符號整數。 * `float`保留浮點精度數字,它們的精度最高為 15 個小數位。 * `complex` – 復數包含實部和虛部。 ```py x = 2 # int x = int(2) # int x = 2.5 # float x = float(2.5) # float x = 100+3j # complex x = complex(100+3j) # complex ``` #### 2.3 列表,元組,范圍 在 Python 中,**列表**是某些數據的**有序序列**,使用方括號(`[ ]`)和逗號(`,`)編寫。 列表可以**包含不同類型**的數據。 **切片運算符**`[:]`可用于訪問列表中的數據。 連接運算符(`+`)和重復運算符(`*`)的工作方式類似于`str`數據類型。 使用切片運算符可以將**范圍**視為`sublist`,從`list`中取出。 **元組**與`list`類似,但`tuple`是只讀數據結構,我們無法修改元組項的大小和值。 另外,項目用括號`(, )`括起來。 ```py randomList = [1, "one", 2, "two"] print (randomList); # prints [1, 'one', 2, 'two'] print (randomList + randomList); # prints [1, 'one', 2, 'two', 1, 'one', 2, 'two'] print (randomList * 2); # prints [1, 'one', 2, 'two', 1, 'one', 2, 'two'] alphabets = ["a", "b", "c", "d", "e", "f", "g", "h"] print (alphabets[3:]); # range - prints ['d', 'e', 'f', 'g', 'h'] print (alphabets[0:2]); # range - prints ['a', 'b'] randomTuple = (1, "one", 2, "two") print (randomTuple[0:2]); # range - prints (1, 'one') randomTuple[0] = 0 # TypeError: 'tuple' object does not support item assignment ``` #### 2.4 字典 **字典**或字典是項的鍵值對的**有序集合**。 鍵可以保存任何原始數據類型,而值是任意的 Python 對象。 字典中的項目用逗號分隔并括在花括號`{, }`中。 ```py charsMap = {1:'a', 2:'b', 3:'c', 4:'d'}; print (charsMap); # prints {1: 'a', 2: 'b', 3: 'c', 4: 'd'} print("1st entry is " + charsMap[1]); # prints 1st entry is a print (charsMap.keys()); # prints dict_keys([1, 2, 3, 4]) print (charsMap.values()); # prints dict_values(['a', 'b', 'c', 'd']) ``` #### 2.5 集,`frozenset` 可以將 python 中的**集**定義為大括號`{, }`內各種項目的**無序集**。 集**的元素不能重復**。 python 集**的元素必須是不可變的**。 與`list`不同,集合元素沒有`index`。 這意味著我們只能遍歷`set`的元素。 **凍結集**是正常集的不變形式。 這意味著我們無法刪除任何項目或將其添加到凍結集中。 ```py digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} print(digits) # prints {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} print(type(digits)) # prints <class 'set'> print("looping through the set elements ... ") for i in digits: print(i) # prints 0 1 2 3 4 5 6 7 8 9 in new lines digits.remove(0) # allowed in normal set print(digits) # {1, 2, 3, 4, 5, 6, 7, 8, 9} frozenSetOfDigits = frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) frozenSetOfDigits.remove(0) # AttributeError: 'frozenset' object has no attribute 'remove' ``` #### 2.6 布爾值 布爾值是兩個常量對象`False`和`True`。 它們用于表示真值。 在數字上下文中,它們的行為分別類似于整數 0 和 1。 ```py x = True y = False print(x) #True print(y) #False print(bool(1)) #True print(bool(0)) #False ``` #### 2.7 字節,字節數組,內存視圖 **字節**和**字節數組**用于處理二進制數據。 **內存視圖**使用緩沖區協議訪問其他二進制對象的內存,而無需進行復制。 字節對象是單個字節的**不變**序列。 僅在處理與 ASCII 兼容的數據時,才應使用它們。 `bytes`字面值的語法與`string`字面值相同,只是添加了`'b'`前綴。 始終通過調用構造器`bytearray()`來創建`bytearray`對象。 這些是**可變的**對象。 ```py x = b'char_data' x = b"char_data" y = bytearray(5) z = memoryview(bytes(5)) print(x) # b'char_data' print(y) # bytearray(b'\x00\x00\x00\x00\x00') print(z) # <memory at 0x014CE328> ``` ## 3\. `type()`函數 `type()`函數可用于獲取任何對象的數據類型。 ```py x = 5 print(type(x)) # <class 'int'> y = 'howtodoinjava.com' print(type(y)) # <class 'str'> ``` 將您的問題留在我的評論中。 學習愉快! 參考: [Python 文檔](https://docs.python.org/3/library/stdtypes.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看