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 lessonTwo variables are given: a = 10 and b = 20. Swap their values without using a temporary variable, then print both.
Expected output:
a = 20
b = 10a = 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
- Create Variables of Each TypeBeginner
- Temperature ConverterBeginner
- Predict the Output: Truthy or Falsy?Beginner
- Fix the Bug: String + Number CrashBeginner
- Type Conversion ChainBeginner
All Python exercises · 50 practice problems · Python Fundamentals quiz