<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/examples/swap-variables](https://www.programiz.com/python-programming/examples/swap-variables) #### 在此示例中,您將學習使用臨時變量(而不使用臨時變量)交換兩個變量。 要理解此示例,您應該了解以下 [Python 編程](/python-programming "Python tutorial")主題: * [Python 數據類型](/python-programming/variables-datatypes) * [Python 輸入,輸出和導入](/python-programming/input-output-import) * [Python 運算符](/python-programming/operators) * * * ## 源代碼:使用臨時變量 ```py # Python program to swap two variables x = 5 y = 10 # To take inputs from the user #x = input('Enter value of x: ') #y = input('Enter value of y: ') # create a temporary variable and swap the values temp = x x = y y = temp print('The value of x after swapping: {}'.format(x)) print('The value of y after swapping: {}'.format(y)) ``` **輸出** ```py The value of x after swapping: 10 The value of y after swapping: 5 ``` 在此程序中,我們使用`temp`變量臨時保存`x`的值。 然后,將`y`的值放在`x`中,然后將`temp`的值放在`y`中。 這樣,就可以交換值。 ## 源代碼:不使用臨時變量 在 Python 中,有一個簡單的結構可以交換變量。 以下代碼與上面的代碼相同,但未使用任何臨時變量。 ```py x = 5 y = 10 x, y = y, x print("x =", x) print("y =", y) ``` 如果變量都是數字,則可以使用算術運算執行相同的操作。 乍一看可能看起來并不直觀。 但是,如果您考慮一下,就很容易弄清楚。 這里有一些例子 **加減法** ```py x = x + y y = x - y x = x - y ``` **乘法和除法** ```py x = x * y y = x / y x = x / y ``` **XOR 交換** 此算法僅適用于整數 ```py x = x ^ y y = x ^ y x = x ^ y ```
                  <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>

                              哎呀哎呀视频在线观看