删除集合元素remove和discard的区别

setA = set('Hello, World!')
setA.remove("X")
help(set.remove)

Traceback (most recent call last):
File “”, line 2, in
setA.remove(“x”)
KeyError: ‘x’

Help on method_descriptor:

remove(…)
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
集合元素不存在,提示错误(KeyError)

setA = set('Hello, World!')
setA.discard("X")
help(set.discard)

无错误信息输出

Help on method_descriptor:

discard(…)
Remove an element from a set if it is a member.
If the element is not a member, do nothing.集合元素不存在,不显示任何信息