Python bootcamp · Lab 39

Swap with a tuple

easyTuples8 minLesson: Tuples

Read the question, write Python on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read two integers (one per line) and print them swapped: the second, then the first.

Python tuple unpacking makes the swap a one-liner: a, b = b, a.

Input. Two lines, each one integer.

Output. Two integers: b then a, space-separated.

Examples

Example 1
Input
3
5
Output
5 3
Example 2
Input
0
-1
Output
-1 0
Hint
  1. a, b = b, a swaps without a temp variable.
  2. print(b, a) also works — try the unpack so you see the idiom.
Show correct code

Peek only after you have tried. You can still Check your own version.

a = int(input())
b = int(input())
a, b = b, a
print(a, b)
main.pyPython · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.