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 & ModeBus FC=1

Node-Red & ModeBus FC=1 write address=0..7   8bits read address=16..23 8bits  [{"id":"cf7c1253d4d997dd","type&q...