Editor

Swap Two Variables

Beginner · Write the code · from Python Variables and Data Types: A Practical Introduction

From the lesson: Python Variables and Data Types: A Practical Introduction

Imagine you're shipping a package. You fill out a label that asks for the weight. You write "heavy" instead of a number. The shipping system crashes - it can't calculate a price from the word "heavy."

Read the lesson

Two variables are given: a = 10 and b = 20. Swap their values without using a temporary variable, then print both.

Expected output:

a = 20
b = 10
a = 10
b = 20
# Swap a and b without a temp variable
print(f"a = {a}")
print(f"b = {b}")

More Variables and Data Types exercises

All Python exercises · 50 practice problems · Python Fundamentals quiz