Sets are used to store multiple items in a single variable.

A Set is a collection which is Unordered , unchangeable, and does not allow duplicate (unindexed) items. Set items can appear in a different order every time we use them, and cannot be referred to by index or key.

Sets are written with curly brackets .

	
	my_Set = {'iphone','samsung','lg'}
	
	print(my_Set)
	

create an empty Set:

	
	my_Set= {}
	
	len(my_Set)  => 0
	

The set() Constructor :

	
	my_Set =set(('iphone','samsung','lg')) 		# Note a double rounded brackets
	
	print(my_Set)
	

Sets Properties :

Set Length

len() function is use to determine the length of a Set.

	
	my_Set = {'iphone','samsung','lg'}
	
	print(len(my_Set))
	

Access Set Items:

We cannot access items in a set by referring to an index or a key. To access an item we can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.

	
	for x in my_Set:			# Loop through the set, and print the values.
		
		print(x)
	
	print('samsung' in my_Set)		# check if 'samsung' is present in the set.
	
	

Change Set Items

Once a Set is created, you cannot change its items. But we can add new items.


Add Items to Set

Remove Items

To remove an item in a set, we can use the remove(), or the discard() method.

Loop Through a Set

using for loop:

	
	for x in my_Set:
	
		print(x) 
	

Join Sets