PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A developer needs to swap the values of two variables a and b in a single line of code. Which statement correctly accomplishes this?
⚠ Common exam trap
Many candidates confuse the comma-separated assignment syntax with other languages' swap methods (like using a temporary variable or semicolons), leading them to choose Option B, which appears to work sequentially but actually fails due to the overwrite issue.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
a, b = b, a
Python supports tuple unpacking, allowing the values of variables `a` and `b` to be swapped in a single line: `a, b = b, a`. The right-hand side `b, a` creates a tuple of the current values, which is then unpacked and assigned to the left-hand side variables, effectively swapping them without needing a temporary variable.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
a = b, b = a
Why it's wrong here
Invalid syntax. This is not a valid assignment.
- ✗
a = b; b = a
Why it's wrong here
Incorrect. After a = b, the original a is lost, so b = a just sets b to the same value.
- ✓
a, b = b, a
Why this is correct
Correct. This uses tuple unpacking to swap values.
- ✗
a = b; a = b
Why it's wrong here
Incorrect. This assigns b to a twice and does not swap.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 498 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.