<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://www.programiz.com/python-programming/type-conversion-and-casting](https://www.programiz.com/python-programming/type-conversion-and-casting) #### 在本文中,您將了解類型轉換的用法。 在學習 Python 中的類型轉換之前,您應該了解 [Python 數據類型](https://www.programiz.com/python-programming/variables-datatypes)。 * * * ## 類型轉換 將一種數據類型(整數,字符串,浮點數等)的值轉換為另一種數據類型的過程稱為類型轉換。 Python 有兩種類型的類型轉換。 1. 隱式類型轉換 2. 顯式類型轉換 * * * ## 隱式類型轉換 在隱式類型轉換中,Python 自動將一種數據類型轉換為另一種數據類型。 此過程不需要任何用戶參與。 讓我們看一個示例,其中 Python 促進將較低數據類型(整數)轉換為較高數據類型(浮點數)以避免數據丟失。 ### 示例 1:將整數轉換為浮點型 ```py num_int = 123 num_flo = 1.23 num_new = num_int + num_flo print("datatype of num_int:",type(num_int)) print("datatype of num_flo:",type(num_flo)) print("Value of num_new:",num_new) print("datatype of num_new:",type(num_new)) ``` 當我們運行上面的程序時,輸出將是: ```py datatype of num_int: <class 'int'> datatype of num_flo: <class 'float'> Value of num_new: 124.23 datatype of num_new: <class 'float'> ``` 在上面的程序中 * 我們添加兩個變量`num_int`和`num_flo`,將值存儲在`num_new`中。 * 我們將分別查看所有三個對象的數據類型。 * 在輸出中,我們可以看到`num_int`的數據類型是`integer`,而`num_flo`的數據類型是`float`。 * 另外,我們可以看到`num_new`具有`float`數據類型,因為 Python 總是將較小的數據類型轉換為較大的數據類型,以避免數據丟失。 * * * 現在,讓我們嘗試添加一個字符串和一個整數,看看 Python 如何處理它。 ### 示例 2:字符串(較高)數據類型和整數(較低)數據類型的加法 ```py num_int = 123 num_str = "456" print("Data type of num_int:",type(num_int)) print("Data type of num_str:",type(num_str)) print(num_int+num_str) ``` 當我們運行程序時,輸出將是: ```py Data type of num_int: <class 'int'> Data type of num_str: <class 'str'> Traceback (most recent call last): File "python", line 7, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str' ``` 在上面的程序中, * 我們添加了兩個變量`num_int`和`num_str`。 * 從輸出中可以看到`TypeError`。 在這種情況下,Python 無法使用隱式轉換。 * 但是,Python 針對此類情況提供了一種解決方案,稱為“顯式轉換”。 * * * ## 顯式類型轉換 在“顯式類型轉換”中,用戶將對象的數據類型轉換為所需的數據類型。 我們使用預定義的函數,例如`int()`,`float()`,`str()`等來執行顯式類型轉換。 這種轉換類型也稱為類型轉換,因為用戶強制轉換(更改)對象的數據類型。 句法 : ```py <required_datatype>(expression) ``` 可以通過將所需的數據類型函數分配給表達式來完成類型轉換。 * * * ### 示例 3:使用顯式轉換將字符串和整數相加 ```py num_int = 123 num_str = "456" print("Data type of num_int:",type(num_int)) print("Data type of num_str before Type Casting:",type(num_str)) num_str = int(num_str) print("Data type of num_str after Type Casting:",type(num_str)) num_sum = num_int + num_str print("Sum of num_int and num_str:",num_sum) print("Data type of the sum:",type(num_sum)) ``` 當我們運行程序時,輸出將是: ```py Data type of num_int: <class 'int'> Data type of num_str before Type Casting: <class 'str'> Data type of num_str after Type Casting: <class 'int'> Sum of num_int and num_str: 579 Data type of the sum: <class 'int'> ``` 在上面的程序中, * 我們添加`num_str`和`num_int`變量。 * 我們使用`int()`函數將`num_str`從字符串(高位)轉換為整數(低位)類型以執行加法。 * 將`num_str`轉換為整數后,Python 可以將這兩個變量相加。 * 我們將`num_sum`的值和數據類型設為整數。 * * * ## 要記住的要點 1. 類型轉換是對象從一種數據類型到另一種數據類型的轉換。 2. 隱式類型轉換由 Python 解釋器自動執行。 3. Python 避免了隱式類型轉換中的數據丟失。 4. 顯式類型轉換也稱為類型轉換,對象的數據類型由用戶使用預定義的函數進行轉換。 5. 在類型轉換中,當我們將對象強制為特定數據類型時,可能會發生數據丟失。
                  <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>

                              哎呀哎呀视频在线观看