2018年1月9日 星期二

Python: Swap two variables

Python: Swap two variables

Python Basic: Exercise-91 with Solution

Write a Python program to swap two variables.
Python: swapping two variables
Swapping two variables refers to mutually exchanging the values of the variables. Generally, this is done with the data in memory.
The simplest method to swap two variables is to use a third temporary variable :
define swap(a, b)
    temp := a
    a := b
    b := temp
Python: swap two variables
Sample Solution:-
a = 30
b = 20
print("\nBefore swap a = %d and b = %d" %(a, b))
a, b = b, a
print("\nAfter swaping a = %d and b = %d" %(a, b))
print()

=========== RESTART: F:/Python_APSC/py-ex-basic-91.py =============

Before swap a = 30 and b = 20

After swaping a = 20 and b = 30

>>> 

沒有留言:

張貼留言

Node-Red --> MQTT --> Fuxa 開源碼網頁式圖控平台

Node-Red --> MQTT --> Fuxa      FUXA(一個開源的 Web HMI / SCADA 自動化監控軟體)的專案設定檔 。 這份設定檔完整定義了 HMI 監控畫面的 後端通訊(MQTT 連線、點位標籤) 與 前端網頁圖形介面(SVG 畫布...