Python Set Explain Briefly Details With Examples
Python Set Explain Briefly Details With Examples


Python Set Explain Briefly Details With Examples

Python Set: A set is everything about Python sets, how they are created adding or removing elements from them, and all operations performed on sets in Python. 

Some Others Languages Topic

👉Array 


👉Union


A set is an unordered collection of items. Every set element is unique and must be immutable (cannot be changed).

However, a set itself is mutable. We can add or take away things from it.

Sets may also be accustomed to performing mathematical set operations like union, intersection, radially symmetrical distinction, etc.


How To Create Set in Python?

str_set={"Python","Java","Programming"} 

int_set={80,77,14,78,96} 

float_set={4.3,8.6,7.4,5.6} 

mix_set={"Icoderweb",8077,35.3}

print("String  Set:",str_set)

print("Integer Set:",int_set)

print("Floating Set:",float_set)

print(Mixed  Set:",mix_set)

*****OUTPUT*****

String  Set: {'Java', 'Python', 'Programming'}

Integer Set: {96, 77, 78, 14, 80}

Floating Set: {5.6, 4.3, 7.4, 8.6}

Mixed  Set: {35.3, 8077, 'Icoderweb'}

Access values of setA set can not be access items of the set using index value because it is an unordered collection of data.

Set can not be sure in which order the items will appear because sets are unordered.


How To use Order Unordered Set in Python?

data={"Python","Java","Programming","PHP","HTML"}

print("Unordered Data:",data)

*****OUTPUT*****

First Run

Unordered Data: {'HTML', 'Python', 'Programming', 'Java', 'PHP'}

Second Run

Unordered Data: {'Programming', 'Python', 'PHP', 'HTML', 'Java'}


Update item of set: The python set can not change the value of the set. Because set immutable means set can not be changed.


How To Update Set in Python?

data={"Python","Java","Programming","PHP","HTML"}

data[1]="Mysql"

print("Set Can Not be Updated:",data)

*****OUTPUT*****

Traceback (most recent call last):

TypeError: 'set' 


Length of set: Python Lenght() function is used to get the length of the set. When data are very large no find the length .it can be used Length function in python Language.


How To Length Set in Python?

data={"Python","Java","Programming","PHP","HTML"}

print("Lenght of Data:",len(data))

*****OUTPUT*****

Lenght of Data: 5


Add Data set: Python adds () function can be used to add a new item in a set. python adds function using any data added in the set.


How To Add Set in Python? 

data={"Python","Java","Programming","PHP","HTML"}

print("ProgrammingLanguage:",data)

data.add("MySQL")

print("Update Programming:",data)

*****OUTPUT*****

ProgrammingLanguage: {'HTML', 'Python', 'Programming', 'Java', 'PHP'}

Update Programming: {'HTML', 'Python', 'Programming', 'Java', 'MySQL', 'PHP'}


Add items any more used update function

update() function is used to add more than one item to a set.

update() and add() function discard the duplicate elements.


How To Update Set in Python?

data={"Python","Java","Programming","PHP","HTML"}

print("ProgrammingLanguage:",data)

data.update(["MySQL","Learn","icoderweb"])

print("Update Programming Language:",data)

*****OUTPUT*****

ProgrammingLanguage: {'Java', 'Python', 'Programming', 'HTML', 'PHP'}

Update Programming Language: {'Java', 'Learn', 'MySQL', 'Python', 'Programming', 'HTML', 'icoderweb', 'PHP'}


Delete item from set using remove() function

Python removes () function can be used to remove a specified item from a set.

Delete items from set using discard() function

discard() function is also used to remove the specified items from a set.


How To discard Set in Python?

data={"Python","Java","Programming","PHP","HTML"}

print("ProgrammingLanguage:",data)

data.discard("PHP")

print("Updated Programming Language:",data)

*****OUTPUT*****

ProgrammingLanguage: {'Python', 'Programming', 'HTML', 'PHP', 'Java'}

Updated Programming Language: {'Python', 'Programming', 'HTML', 'Java'}


How To Delete Set in Python?

data={"Python","Java","Programming","PHP","HTML"}

print("ProgrammingLanguage:",data)

data.remove("PHP")

print("Updated Programming Language:",data)

*****OUTPUT*****

ProgrammingLanguage: {'Python', 'Programming', 'HTML', 'PHP', 'Java'}

Updated Programming Language: {'Python', 'Programming', 'HTML', 'Java'}


Join two sets: Python can be used to join two sets using the union() function.it is marge the data two different items.


How To Join Set in Python?

data1={"Python","Java","Programming","PHP"}

data2={"Database"}

data3=data1.union(data2)

print("Join Function Used:",data3)

*****OUTPUT*****

Join Function Used: {'Python', 'Programming', 'Database', 'Java', 'PHP'}


Clear set: Python clear() function can be used to clear or empty the set.


How To Clear Set in Python?

data={"Python","Java","Programming","PHP"}

print("Before Printed Data:",data)

data.clear()

print("After Printed Data:",data)

*****OUTPUT*****

Before Printed Data: {'PHP', 'Python', 'Java', 'Programming'}

After Printed Data: set()

Delete set: del keyword is also used to delete the set completely.


How To Del Set in Python?

data={"Python","Java","Programming","PHP"}

print("Before Printed Data:",data)

del data

print("Data Deleted Successfully")

*****OUTPUT*****

Before Printed Data: {'Java', 'Python', 'PHP', 'Programming'}

Data Deleted Successfully