What are Sets in Python - Explained with Examples | Python Tutorial for Beginners

Published: 05 August 2023
on channel: Pycent com
28
1

Notes prepared by Pycent.com for Beginners in python course

Basic Characteristics of Sets:
Unordered: The items in a set have no order.
Unchangeable: You cannot change items in a set after it's created.
Do not allow duplicate values: Sets cannot have two items with the same value.

Important Point- in Creating a Set:
Sets are created using curly brackets {} or the set() constructor.


Example:
Using curly brackets
fruits = {"apple", "banana", "cherry"}
print(fruits)

Using set constructor
colors = set(["red", "green", "blue"])
print(colors)

Accessing Items:
You cannot access items in a set by referring to an index, but you can loop through the set items using a for loop.

Example:
for fruit in fruits:
print(fruit)

Checking if an Item Exists:
Example:
if "apple" in fruits:
print("Apple is in the set")

Adding and Removing Items from Sets
add(): Adds one item to a set.
update(): Adds more than one item to a set.

Example:
fruits.add("orange")
print(fruits)

fruits.update(["grape", "mango"])
print(fruits)


Removing Items from sets:
remove(): Removes a specified item from the set. Raises an error if the item does not exist.
discard(): Removes a specified item, but does not raise an error if the item does not exist.
pop(): Removes the last item. As sets are unordered, you won't know which item gets removed.
clear(): Empties the set.

Example:
fruits.remove("banana")
print(fruits)

fruits.discard("apple")
print(fruits)

fruits.pop()
print(fruits)

fruits.clear()
print(fruits)

Operations that can be performed on sets like on sets in mathematics
Set Operations:
Union (| or union() method): Returns a set containing all items from both sets.
Example for Union:
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B)
print(A.union(B))

Example for Intersection
print(A & B)
print(A.intersection(B))

Example for Difference
print(A - B)
print(A.difference(B))

Example for Symmetric difference:
print(A ^ B)
print(A.symmetric_difference(B))


Real-time Examples of Sets in Python:
Removing Duplicate Values from a List:
Example:
keywords_list = ["Python set", "Set methods", "Create set", "Set methods", "Set union", "Python set"]
unique_keywords = set(keywords_list)
print(unique_keywords)

Finding Common Elements between Two Lists:
Example:
list1 = ["Python set", "Set methods", "Set union"]
list2 = ["Set union", "Set difference", "Set methods"]
common_keywords = set(list1).intersection(list2)
print(common_keywords)

Checking Membership:
keywords_set = {"Python set", "Set methods", "Create set"}
if "Python set" in keywords_set:
print("This is a popular keyword!")


Combining Keywords from Multiple Sources without Duplicates
Example:
source1 = {"Python set", "Set methods", "Set union"}
source2 = {"Set union", "Set intersection", "Python sets tutorial"}
master_keywords = source1.union(source2)
print(master_keywords)

Finding Unique Items:
users = ["user1", "user2", "user3", "user1", "user4"]
unique_users = set(users)
print(unique_users)

visit:
https://pycent.com

Like Share and Subscribe to receive more advanced Content in Python Coding


On this page of the site you can watch the video online What are Sets in Python - Explained with Examples | Python Tutorial for Beginners with a duration of hours minute second in good quality, which was uploaded by the user Pycent com 05 August 2023, share the link with friends and acquaintances, this video has already been watched 28 times on youtube and it was liked by 1 viewers. Enjoy your viewing!