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

>>> 

沒有留言:

張貼留言

Exploring Node-Red, Dashboard 2.0, and MQTT

  Exploring Node-Red, Dashboard 2.0, and MQTT 參考來源 https://www.donskytech.com/node-red-dashboard-2-0-mqtt/#google_vignett e #include < A...