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

>>> 

沒有留言:

張貼留言

Cargo Transfer Process Simulation 「貨物轉移過程模擬」

Cargo Transfer Process Simulation「貨物轉移過程模擬」 https://www.youtube.com/watch?v=Ic9PpPG-_NU   以下是操作步驟: 打開 EasyBuilder Pro,選擇「新建專案」(New Project),...