Python establece el método add ()
Ejemplo
Agregue un elemento al fruits
conjunto:
fruits =
{"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)
Definición y uso
El add()
método agrega un elemento al conjunto.
Si el elemento ya existe, el add()
método no agrega el elemento.
Sintaxis
set.add(elmnt)
Valores paramétricos
Parameter | Description |
---|---|
elmnt | Required. The element to add to the set |
Más ejemplos
Ejemplo
Intenta agregar un elemento que ya existe:
fruits = {"apple", "banana", "cherry"}
fruits.add("apple")
print(fruits)