** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
.add
and .remove
>>> s = {1, 2, 3, 4, 5}
>>> s.add(6)
>>> s
{1, 2, 3, 4, 5, 6}
>>> s.add(6)
>>> s
{1, 2, 3, 4, 5, 6}
>>> s.remove(6)
>>> s
{1, 2, 3, 4, 5}
Adding elements to a set with .add
You can remove an element from a set with .remove
.
By the way, it doesn't make sense to .add
an existing element.
(In the collective type, the same elements are grouped together.)
◆.clear
>>> s.clear()
>>> s
set()
With .clear
, all the elements in the set disappear.
At this time, why is it displayed as set ()
instead of {}
?
In Python, {}
is an empty dictionary type.
>>> d = {}
>>> d
{}
>>> type(d)
<class 'dict'>
It is written separately so that these are not covered.
Recommended Posts