DAY 10: Python Set

Thus far, we have learned about various data types in Python. We dealt with strings, numbers, lists, tuples.
Today, we will talk about Python set examples and Python Booleans. After that, we will move onto python functions in further lessons. https://twitter.com/RealSaintSteven/status/1245079067600334848
What is Sets?
A set in a maths expression means a group of data & elements that are arranged in a specific order & represented by enclosing the elements in d curly parenthesis

Sets in Python
A set in Python holds a sequence of values
It is sequenced but does not support indexing
Syntax
The syntax contains a set name that is assigned to the set of data or elements enclose between the curly parenthesis and separated by a delimiter, that is comma.

firstset = {"Steven", "Oladeji", "Twitter"}
print(firstset)

The curly braces {} represents set
a. Creating a Python Set
*To declare a set, you need to type a sequence of items separated by commas, inside curly braces
*A set may contain values of different types
*It also cannot contain duplicate elements
*A set is mutable, but may not contain mutable items like a list, set
b. Accessing a Set in Python
Since sets in Python do not support indexing, it is only possible to access the entire set at once

d. Deleting a Set
You can’t delete an element using its index. So for this, you must use one the following methods
*discard()
*remove()
*clear()
*pop()
e. Updating a Set
As stated above, a Python set is mutable. We can’t use indices to reassign it.
We use two methods for this purpose- add() and update(). We have seen the update() method on tuples, lists, and strings.
f. Set Functions
A function is something that you can apply to a Python set, and it performs operations on it and returns a value
Some functions that a set supports

len(): This function returns the length of a set
max(): This returns the item from the set with the highest value
*min(): This function returns the item in the Python set with the lowest value.
*sum(): This function in Python set returns the arithmetic sum of all the items in a set.
*any(): This function returns True even if one item in the set has a Boolean value of True.
*all()
*sorted()
g. Methods on Sets
*union():
*intersection(): This takes as argument sets, & returns the common items in all the sets.
*difference(): It returns the difference of two or more sets.
*symmetric_difference(): returns all the items that are unique to each set.
*copy():
*issuperset()
In Summary,
We learned how to create, access, update, and delete a set. We saw how it is mutable and that is why we can’t use indexing to access, update, or delete it. So, we use certain Python set functions and methods for the same.
You can follow @RealSaintSteven.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: