LearnFrenzy
Sign in Take a free test

Sets · Predict the Output · Question 14

In-place set operators Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 14 Back to Exercise

What is the output of the following Python program?

main.py
a = {1, 2, 3, 4}
b = a
a &= {2, 4, 6}
a -= {4}
print(a, b, a is b)
  • A {2} {1, 2, 3, 4} False
  • B {2, 6} {2, 6} True
  • C {2, 4} {2, 4} True
  • D {2} {2} True Correct
Correct Answer: Option D

&= and -= modify the set in place, so the alias b sees the change.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links