Python Datatype - Part 4 (Sets)

Python Datatype - Part 4 (Sets)

Python Sets

As we learnt earlier Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary.

  • A set is a collection which is unordered, unchangeable*, and unindexed.
  • But you can remove items and add new items.
  • Sets are written with curly brackets.

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

print(myset)        

Note: Sets are unordered, so you cannot be sure in which order the items will appear when printed.

  • Sets do not allow duplicate values.
  • Set items can appear in a different order every time you use them, and cannot be referred to by index or key.
  • Sets cannot have two items with the same value, duplicate values will be ignored:

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM", "Ford"}

print(myset)        

  • The values True and 1 are considered the same value in sets, and are treated as duplicates
  • The values False and 0 are considered the same value in sets, and are treated as duplicates

myset = {"Toyota", True, "Mercedes", 0, 1, 2, False, "Ford", "Honda", "IVM", "Ford"}

print(myset)        

Get the Length of a Set

len() function is use to determine the number of items in a set

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

print(len(myset))        

Set Items - Data Types

Items store in a set can be of any data type. it can even be a combination of multiple data type

set1 = {"apple", "banana", "cherry"}

set2 = {1, 5, 7, 9, 3}

set3 = {True, False, False}

set4 = {"abc", 34, True, 40, "male"}        

Access Set Items

Set item can not be accessed by index or key but looping through the set, items can be getting and you can also check if a value in present in the set by using the "in" keyword

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

for x in myset:
  print(x)        
myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

print("Ford" in myset)        

Change Items

Set item can only be created, they can not be change by any means, but you can add new items to the set.

Add Set Items

The add() method is use to add item to a set

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

print(myset)

myset.add("Tesla")

print(myset)        

Add a Set to a Set

update() method is use to add items from another set to a set

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

myset2 = {"Tesla", "Nissan"}
myset.update(myset2)

print(myset)        

update() can be use on any other iterable object to a set, like lists, tuples, dictionaries

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

mylist = ["Volkswagen", "Tesla", "Nissan"]
myset.update(mylist)

print(myset)        

Remove Item

remove() or discard() method can both be used to remove item from a set

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

myset.remove("Toyota")

print(myset)        

Note: If the item to remove does not exist, remove() will raise an error.

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

myset.discard("Toyota")

print(myset)        

Note: If the item to remove does not exist, discard() will NOT raise an error.

pop() can also be use to remove item from a set, but it will remove any random item, no way to be sure which item will get removed

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

x = myset.pop()
print(x)

print(myset)        

clear() method use to empty the set

del keyword use to delete the set completely from memory

Join Sets

joining of sets can be done in several ways:

- Using union() method: returns a new set containing all items from both sets

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

myset2 = {"Tesla", "Nissan"}
myset3 = myset.union(myset2)

print(myset3)        

- Using update() method: inserts all the items from one set into another

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}

myset2 = {"Tesla", "Nissan"}
myset.update(myset2)

print(myset)        

Note: Both union() and update() will exclude any duplicate items.

- Using intersection_update() method: method will keep only the items that are present in both sets (keep only duplicate)

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset2 = {"Tesla", "Nissan", "Ford", "Honda"}

myset.intersection_update(myset2)

print(myset)        

- Using intersection() method: return a new set, that only contains the items that are present in both sets.

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset2 = {"Tesla", "Nissan", "Ford", "Honda"}

myset3 = myset.intersection(myset2)

print(myset3)        

- Using symmetric_difference_update() method: keep only the elements that are NOT present in both sets

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset2 = {"Tesla", "Nissan", "Ford", "Honda"}

myset.symmetric_difference_update(myset2)

print(myset)        

- Using symmetric_difference() method: return a new set, that contains only the elements that are NOT present in both sets

myset = {"Toyota", "Mercedes", "Ford", "Honda", "IVM"}
myset2 = {"Tesla", "Nissan", "Ford", "Honda"}
myset3 = myset.symmetric_difference(myset2)

print(myset3)        


Muhammad Imran

Software Engineer

1 年

Please tell me about Python programming learning

回复

要查看或添加评论,请登录

Abdulmutalib Idris的更多文章

  • Improving Nigerian Education Through School Management Systems

    Improving Nigerian Education Through School Management Systems

    The Nigerian education sector faces numerous challenges, particularly at the higher institution level. These include…

  • Python Lambda

    Python Lambda

    Is a function that take as many arguments as possible but can only have one expression. They are also consider…

  • Python Functions

    Python Functions

    A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a…

    2 条评论
  • Python Conditional and Loop Statements

    Python Conditional and Loop Statements

    If ..

    1 条评论
  • Python Datatype - Part 5 (Dictionary)

    Python Datatype - Part 5 (Dictionary)

    As we learnt earlier Dictionary is one of 4 built-in data types in Python used to store collections of data, the other…

    2 条评论
  • Python Datatype - Part 3 (Tuples)

    Python Datatype - Part 3 (Tuples)

    Python Tuples As we learnt in the last class there are datatype use to store multiple items in single variable. Python…

  • Python Datatype - Part 2 (Lists)

    Python Datatype - Part 2 (Lists)

    Python Lists There are datatype use to store multiple items in single variable. Python have 4 built-in datatype that…

    1 条评论
  • Python Datatype - Part 1

    Python Datatype - Part 1

    In the last class we talked about variables and we said variable data type is whatever values are assigned to the…

  • Python Variables

    Python Variables

    In python variables are the reserved memory locations used for storing values. In python a built-in id() function…

  • Python Indentation, Multi-line Statements, and Quotations

    Python Indentation, Multi-line Statements, and Quotations

    Python Indentation The spaces left at the beginning of a code line is called indentation. In other languages indenting…

    1 条评论

社区洞察

其他会员也浏览了