Q. What does `dict.setdefault('key', default)` do?

  • (A) Sets a new key only if it exists
  • (B) Returns the value if key exists; otherwise sets it to default
  • (C) Raises an error if key doesn’t exist
  • (D) Deletes the key
πŸ’¬ Discuss
βœ… Correct Answer: (B) Returns the value if key exists; otherwise sets it to default
Explanation: `setdefault()` returns the value if the key exists; if not, it inserts the key with the default value.
Explanation by: Mr. Dubey
`setdefault()` returns the value if the key exists; if not, it inserts the key with the default value.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
87
Total Visits
πŸ“½οΈ
1 y ago
Published
πŸŽ–οΈ
Mr. Dubey
Publisher
πŸ“ˆ
95%
Success Rate