Swapping values

suggest change

Tuples are useful to swap values between 2 (or more) variables without using temporary variables.

Example with 2 variables

Given 2 variables

var a = "Marty McFly"
var b = "Emmett Brown"

we can easily swap the values

(a, b) = (b, a)

Result:

print(a) // "Emmett Brown" 
print(b) // "Marty McFly"

Example with 4 variables

var a = 0
var b = 1
var c = 2
var d = 3

(a, b, c, d) = (d, c, b, a)

print(a, b, c, d) // 3, 2, 1, 0

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents