<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Python 數據類型 > 原文: [https://www.programiz.com/python-programming/variables-datatypes](https://www.programiz.com/python-programming/variables-datatypes) #### 在本教程中,您將學習可在 Python 中使用的不同數據類型。 ## Python 中的數據類型 Python 中的每個值都有一個數據類型。 由于在 Python 編程中一切都是對象,因此數據類型實際上是類,而變量是這些類的實例(對象)。 Python 中有多種數據類型。 下面列出了一些重要的類型。 * * * ## Python 數字 整數,浮點數和復數屬于 [Python 數字](https://www.programiz.com/python-programming/numbers)類別。 在 Python 中,它們被定義為`int`,`float`和`complex`類。 我們可以使用`type()`函數來了解變量或值屬于哪個類。 同樣,`isinstance()`函數用于檢查對象是否屬于特定類。 ```py a = 5 print(a, "is of type", type(a)) a = 2.0 print(a, "is of type", type(a)) a = 1+2j print(a, "is complex number?", isinstance(1+2j,complex)) ``` **輸出** ```py 5 is of type <class 'int'> 2.0 is of type <class 'float'> (1+2j) is complex number? True ``` 整數可以是任意長度,僅受可用內存的限制。 浮點數最多可精確到 15 個小數位。 整數和浮點由小數點分隔。 1 是整數, 1.0 是浮點數。 復數以`x + yj`的形式編寫,其中`x`是實數部分,`y`是虛數部分。 這里有些例子。 ```py >>> a = 1234567890123456789 >>> a 1234567890123456789 >>> b = 0.1234567890123456789 >>> b 0.12345678901234568 >>> c = 1+2j >>> c (1+2j) ``` 請注意,`float`變量`b`被截斷了。 * * * ## Python 列表 [列表](https://www.programiz.com/python-programming/list)是項目的有序序列。 它是 Python 中最常用的數據類型之一,非常靈活。 列表中的所有項目都不必是同一類型。 聲明列表非常簡單。 用逗號分隔的項目括在方括號`[ ]`中。 ```py a = [1, 2.2, 'python'] ``` 我們可以使用切片運算符`[ ]`從列表中提取一個項目或一系列項目。 索引在 Python 中從 0 開始。 ```py a = [5,10,15,20,25,30,35,40] # a[2] = 15 print("a[2] = ", a[2]) # a[0:3] = [5, 10, 15] print("a[0:3] = ", a[0:3]) # a[5:] = [30, 35, 40] print("a[5:] = ", a[5:]) ``` **輸出**: ```py a[2] = 15 a[0:3] = [5, 10, 15] a[5:] = [30, 35, 40] ``` 列表是可變的,也就是說,列表元素的值可以更改。 ```py a = [1, 2, 3] a[2] = 4 print(a) ``` **輸出**: ```py [1, 2, 4] ``` * * * ## Python 元組 [元組](https://www.programiz.com/python-programming/tuple)是與列表相同的項的有序序列。 唯一的區別是元組是不可變的。 元組一旦創建就無法修改。 元組用于寫保護數據,通常比列表快,因為它們不能動態更改。 它在括號`()`中定義,其中各項之間用逗號分隔。 ```py t = (5,'program', 1+3j) ``` 我們可以使用切片運算符`[]`提取項目,但不能更改其值。 ```py t = (5,'program', 1+3j) # t[1] = 'program' print("t[1] = ", t[1]) # t[0:3] = (5, 'program', (1+3j)) print("t[0:3] = ", t[0:3]) # Generates error # Tuples are immutable t[0] = 10 ``` **輸出**: ```py t[1] = program t[0:3] = (5, 'program', (1+3j)) Traceback (most recent call last): File "test.py", line 11, in <module> t[0] = 10 TypeError: 'tuple' object does not support item assignment ``` * * * ## Python 字符串 [字符串](https://www.programiz.com/python-programming/string)是 Unicode 字符序列。 我們可以使用單引號或雙引號來表示字符串。 可以使用三引號`'''`或`"""`表示多行字符串。 ```py s = "This is a string" print(s) s = '''A multiline string''' print(s) ``` **輸出**: ```py This is a string A multiline string ``` 就像列表和元組一樣,切片運算符`[ ]`可以與字符串一起使用。 但是,字符串是不可變的。 ```py s = 'Hello world!' # s[4] = 'o' print("s[4] = ", s[4]) # s[6:11] = 'world' print("s[6:11] = ", s[6:11]) # Generates error # Strings are immutable in Python s[5] ='d' ``` **輸出**: ```py s[4] = o s[6:11] = world Traceback (most recent call last): File "<string>", line 11, in <module> TypeError: 'str' object does not support item assignment ``` * * * ## Python 集 [集](https://www.programiz.com/python-programming/set)是唯一項的無序集合。 集由用大括號`{ }`內的逗號分隔的值定義。 集合中的項目不排序。 ```py a = {5,2,3,1,4} # printing set variable print("a = ", a) # data type of variable a print(type(a)) ``` **輸出**: ```py a = {1, 2, 3, 4, 5} <class 'set'> ``` 我們可以在兩個集合上執行集合操作,例如聯合,相交。 集具有唯一值。 他們消除重復。 ```py a = {1,2,2,3,3,3} print(a) ``` **輸出**: ```py {1, 2, 3} ``` 由于集是無序集合,因此索引沒有意義。 因此,切片運算符`[]`不起作用。 ```py >>> a = {1,2,3} >>> a[1] Traceback (most recent call last): File "<string>", line 301, in runcode File "<interactive input>", line 1, in <module> TypeError: 'set' object does not support indexing ``` * * * ## Python 字典 [字典](https://www.programiz.com/python-programming/dictionary)是鍵值對的無序集合。 當我們擁有大量數據時,通常使用它。 字典針對檢索數據進行了優化。 我們必須知道檢索值的鍵。 在 Python 中,字典在花括號`{}`中定義,每一項都是一對,形式為`key:value`。 鍵和值可以是任何類型。 ```py >>> d = {1:'value','key':2} >>> type(d) <class 'dict'> ``` 我們使用鍵來檢索相應的值。 但并非相反。 ```py d = {1:'value','key':2} print(type(d)) print("d[1] = ", d[1]); print("d['key'] = ", d['key']); # Generates error print("d[2] = ", d[2]); ``` **輸出**: ```py <class 'dict'> d[1] = value d['key'] = 2 Traceback (most recent call last): File "<string>", line 9, in <module> KeyError: 2 ``` * * * ## 數據類型之間的轉換 我們可以使用`int()`,`float()`,`str()`等不同的類型轉換函數在不同的數據類型之間進行轉換。 ```py >>> float(5) 5.0 ``` 從`float`到`int`的轉換將截斷該值(使其接近零)。 ```py >>> int(10.6) 10 >>> int(-10.6) -10 ``` 字符串之間的轉換必須包含兼容的值。 ```py >>> float('2.5') 2.5 >>> str(25) '25' >>> int('1p') Traceback (most recent call last): File "<string>", line 301, in runcode File "<interactive input>", line 1, in <module> ValueError: invalid literal for int() with base 10: '1p' ``` 我們甚至可以將一個序列轉換為另一序列。 ```py >>> set([1,2,3]) {1, 2, 3} >>> tuple({5,6,7}) (5, 6, 7) >>> list('hello') ['h', 'e', 'l', 'l', 'o'] ``` 要轉換為字典,每個元素必須成對: ```py >>> dict([[1,2],[3,4]]) {1: 2, 3: 4} >>> dict([(3,26),(4,44)]) {3: 26, 4: 44} ```
                  <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>

                              哎呀哎呀视频在线观看