SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
Sets And Dictionaries
Exercise:
Check Point
Question:12 | ISBN:978013274719 | Edition: 6

Question

Will the following code have a runtime error?
students = {"peter", "john"}
students.remove("johnson")
print(students)

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

students.remove("johnson")

It gives runtime error because we are removing "johnson" from a set.

set does  not contain element called  "johnson" so if we tried to remove "johnson" it gives an error.

we can execute other statements without any errors.

students={"peter","john"}

print(students)

if we want to perform removing element from a set we can perform like this 

students.remove("peteer")

 

0 0

Discussions

Post the discussion to improve the above solution.