2. Sets

2.2. Creando un set

Para crear un conjunto, basta con encerrar una serie de elementos entre llaves { }, o bien usar el constructor de la clase set()  y pasarle como argumento un objeto iterable.


# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)
# Creating a Set with the use of a String
set1 = set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)
# Creating a Set with the use of Constructor
# (Using object to Store String)
String = 'GeeksForGeeks' #String
set1 = set(String) #agrego string a set
print("\nSet with the use of an Object: " )
print(set1)
# Creating a Set with # the use of a List
set1 = set(["Geeks", "For", "Geeks"]) #agrego lista a set
print("\nSet with the use of List: ")
print(set1)

La Salida de este script sería:



Un conjunto contiene solo elementos únicos, pero en el momento de la creación del conjunto, también se pueden pasar varios valores duplicados. 

El orden de los elementos de un conjunto no está definido y no se puede cambiar. 

No es necesario que el tipo de elementos de un conjunto sea el mismo, también se pueden pasar al conjunto varios valores de tipos de datos mezclados. 

# Creating a Set with a List of Numbers
# (Having duplicate values)
set1 = set([1, 2, 4, 4, 3, 3, 3, 6, 5]) #Lista con valores duplicados
print("\nSet with the use of Numbers: ") #ver que NO existen valores duplicados!
print(set1)
# Creating a Set with
# a mixed type of values
# (Having numbers and strings)
set1 = set([1, 2, 'Geeks', 4, 'For', 6, 'Geeks']) #Lista con valores duplicados
print("\nSet with the use of Mixed Values") #ver que NO existen valores duplicados!
print(set1)>



Nota:
x={}
Esto en Python crea un Diccionario vacio, no un Set.!!