In this tutorial we are going to learn :

  • What is a Set and How They are Created
  • Accessing Items
  • Adding Items
  • Removing Items
  • Using Mathematical Operators in Set

 

 

 

What is a Set and How They Are Created ?

 

Set is an unordered and unindexed collection of items.

  • Every set item must be unique.
  • We use curly brackets to create a set.

 

Example :

first_set = {"James", "John", "Adam"}
second_set = {1, 2, 3, 4}
third_set = {"David", 1, 2.7, "a"}


print(type(first_set))
print(type(second_set))
print(type(third_set)) 
 
  • We can also use the set() function to create a set.

Example :

# we can use set() function to create set

first_list = [1, 1, 1, 2, 3, 3, 4, 5]
second_list = ["a", "b", "b", "c", "b", "d"]
my_tuple = (1, 2, 2, 2, 3, 2, 3, 4, 5)

first_set = set(first_list)
second_set = set(second_list)
third_set = set(my_tuple)

print(first_set)
print(second_set)
print(third_set) 

 

 

 

Accessing Items

 

Sets are unordered the items have no index. We cannot access items in a set by referring to an index number .

But We can use the in keyword to learn if the item we are looking for is in our set.

Example :

my_set = {1, 2, 3, 4, 5, 6, 7, 8, 9}

if 1 in my_set:
    print("exist")
    
    
# or we can check for True, False value
print(1 in my_set)
print(0 in my_set) 

 

 

 

Adding Items

 

There are two ways to add item to a set.

  • To add just one item , you can use the add() method.
  • To add more than one item to a set , you can use the update() method.

Example: 

my_set = {1, 2, 3, 4, 5}

print(my_set)

# we use add() method to add one item
my_set.add(6)
print(my_set)

# we use update() method to add multiple items
my_set.update([7, 8, 9])
print(my_set) 

 

 

 

Removing Items

 

To remove item, we have some methods :

  • remove () : method removes the item but if the item to remove doesn’t exist, remove () will raise an error.
  • discard () : method removes the items and if the item to remove doesn’t exist , discard will not raise an error.
    del : method removes all set items.

Example :

my_set = {1, 2, 3, 4, 5, 6, 7, 8, 9}

print(my_set)

my_set.remove(1)
print(my_set)

#if item does not exist, remove() method will raise an error. We use discard() method
my_set.discard(2)
print(my_set)

# We can use clear() method to delete all items
my_set.clear()
print(my_set) 




Using Mathematical Operators in Set


The operators we use in set :

  • |      : will return a new set that includes all items.
  • &     : will return a new set that includes both items.
  • –       : difference between two set.
  • ^      : symmetric difference between two set.


Example :

first_set = {1, 1, 2, 3, 4, 5}
second_set = {1, 6}

print(first_set | second_set)   # this will return a new set that include all items
print(first_set & second_set)   # this will return a new set that include both  items
print(first_set - second_set    # difference between two sets
print(first_set ^ second_set)   # symmetric difference between two sets 
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments