WissensdatenbankPrüfungszentrum
DEDeutsch
Sprache wählen13

CKTIS / Python

set.add

Fügt ein einzelnes hashbares Element ein und liefert None.

Version: Python 3.14; repository image, production runtime not attested

Syntax

s.add(element, /)

Fügt ein einzelnes hashbares Element ein und liefert None.

Zweimaliges Hinzufügen von 4 vergrößert die Menge {2, 3} nur um ein Element.

Eine Liste ist nicht hashbar und löst als Argument TypeError aus.

s = {1}
assert s.add(True) is None and len(s) == 1

Beispiele

s = {2, 3}
s.add(4)
s.add(4)
print(sorted(s))

Ergebnis

[2, 3, 4]

Eine Liste ist nicht hashbar und löst als Argument TypeError aus.

Quellen