Thursday , September 19 2024

What will be the output of the following snippet? a = 1 b = 0 a = a ^ b b = a ^ b a = a ^ b print(a, b)

Questions BankCategory: Python Essentials 1What will be the output of the following snippet? a = 1 b = 0 a = a ^ b b = a ^ b a = a ^ b print(a, b)
What will be the output of the following snippet?

a = 1
b = 0
a = a ^ b
b = a ^ b
a = a ^ b
 
print(a, b)
  • 0 0
  • 1 1
  • 0 1
  • 1 0

Explanation: Let’s analyze the code line by line:

  • Line 1: 1 is assigned to variable a
  • Line 2: 0 is assigned to variable b
  • Line 3: the result of the evaluation is 1, so a = 1
  • Line 4: the result of the evaluation is also 1, so b = 1
  • Line 5: the result of the evaluation a = a ^ b (where a = 1 and b = 1) is 0, so a = 0
  • the new values assigned to the a and b variables are then printed to the console.

More Questions: Python Essentials 1 (PE1) Course Final Test