Python - Variables and Dynamic Typing
You want to swap the values of two variables
a and b in Python. Which of the following is the correct way to do it?a and b in Python. Which of the following is the correct way to do it?a gets b's value and b gets a's value.b to a, then a to b, losing original a. temp = a
b = temp
a = b incorrectly assigns b before a. a = a + b
b = a - b
a = a - b uses arithmetic swap which works but is less clear and can cause errors with non-numbers. a, b = b, a uses Python's tuple unpacking to swap values correctly and clearly.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions