QuestenaПрактика, после которой понятно, что повторить
Вопрос 3

After `a = 2`, `b = 5`, and `a, b = b, a`, what are the values of `a` and `b`?

  1. `a` is 2 and `b` is 2.
  2. `a` is 5 and `b` is 5.
  3. `a` is 5 and `b` is 2.Правильный ответ
  4. The swap raises an assignment error.

Объяснение

Python obtains both right-hand values before rebinding either target, so the old value of `b` goes to `a` and the old value of `a` goes to `b`. A left-to-right update would lose one old value, but that is not how multiple assignment is evaluated.

Продолжить работу с тестом

Начать тренировку
Вопрос 3: After `a = 2`, `b = 5`, and `a, b = b, a`, what are the… · Python Fundamentals · Questena