LearnFrenzy
Sign in Take a free test

Sets · Predict the Output · Question 5

add() an existing item and discard() a missing one Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

What is the output of the following Python program?

main.py
s = {1, 2}
s.add(2)
s.add(3)
s.discard(10)
print(s, len(s))
  • A {1, 2, 2, 3} 4
  • B KeyError
  • C {1, 2, 3, 10} 4
  • D {1, 2, 3} 3 Correct
Correct Answer: Option D

Adding an existing element changes nothing, and discard() ignores a missing one.

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