Python Sets

Published by StudyMuch on

Python Sets

Understanding Python Sets

Python, a versatile and powerful programming language, offers a wide range of data structures that make it a favourite among developers. Set is a fundamental data structure used for storing collections of unique elements. In this blog post, we will learn deeper into Python Sets, exploring their definition, operations, and usage with various code examples.

What is a Python Set?

A set is an unordered collection of unique elements in Python. Unlike other data structures like lists or tuples, a set does not allow duplicate values. Sets are highly useful when you need to store distinct items or perform set-related operations such as unions and intersections.

Python Sets

Creating a Set

You can create a set using curly braces {} or by using the set() constructor. The objects are placed inside those brackets and are separated by comma(,).

# Using curly braces
my_set = {1, 2, 3}
print(my_set)

# Using set() constructor
my_sets = set(["Laptop", "Mobile", "Watch"])
print(my_sets)

Accessing Items in a Set

Sets are unordered collections, meaning they do not have indices like lists or tuples. Therefore, you cannot access set items by their index. However, you can use for loop to access all its items one-by-one.

my_sets = set(["Laptop", "Mobile", "Watch"])

for items in my_sets:
    print(items) #Output (Mobile Watch Laptop)

Adding Items to a Set

You can add items to a set using the add() or update() method. The add() method adds one item to a set.

my_sets = {"Laptop", "Mobile", "Watch"}
my_sets.add("SmartWatch")
print(my_sets)

Changing Items in a Set

Sets are unordered and immutable, meaning you cannot change individual items in a set. To update a set, you can remove the old item and add the new one. To remove an item from a set, use the remove() method. You should specify the value of the item you want to remove.

my_sets = {"Laptop", "Mobile", "Watch"}
#Removing "Watch"
my_sets.remove("Watch")

#Adding "SmartWatch"
my_sets.add("SmartWatch")
print(my_sets)



Getting the length of Set

To get the length of the number of items in a set, use the len() method.

my_set = {1, 2, 3, 4, 5}
length = len(my_set)
print(length)

Checking if an item Exists

You can check if an item exists in a set using the “in” keyword. It returns a Boolean value, True if the item is in the set, and False otherwise.

my_sets = {"Laptop", "Mobile", "Watch"}

print("Mobile" in my_sets) #True
print("SmartWatch" in my_sets) #False

Combining Two Sets

Python sets support various set operations like union, intersection, and difference. To combine two sets into one, you can use the union() or update() method. update() method exclude duplicate items.

set = {"Laptop", "Mobile", "Watch"}
set1 = {"SmartWatch", "Ipad"}

combine = set.union(set1)
print(combine)

Combining Two sets using update() method.

set = {"Laptop", "Mobile", "Watch"}
set1 = {"SmartWatch", "Ipad"}

set.update(set1)
print(set)

These are some of the most common operations when working with sets in Python. Sets offer a convenient way to work with unique collections of data, making them a valuable tool in many programming scenarios.


Conclusion;

In summary, Python sets are a versatile data structure for working with collections of unique elements. They are useful for various tasks like removing duplicates, set operations, and membership testing. By understanding how to create, manipulate, and utilize sets, you can write more efficient and concise code in your Python projects. I hope you have understood this tutorial (Python Sets), but if you have any doubt don’t hesitate to ask.

Learn More;


4 Comments

https://68.183.225.142/ · January 23, 2024 at 3:23 pm

I?ve recently started a site, the info you offer on this web site has helped me greatly. Thank you for all of your time & work.

Casinosite · February 5, 2024 at 5:05 pm

Nice post. I used to be checking constantly this weblog and I’m impressed! Extremely helpful information specially the ultimate phase 🙂 I handle such info much. I was looking for this certain info for a very lengthy time. Thank you and good luck.

situs bokep · April 9, 2024 at 1:03 pm

Holy cow! I’m in awe of the author’s writing skills and capability to convey complicated concepts in a concise and concise manner. This article is a true gem that earns all the praise it can get. Thank you so much, author, for offering your wisdom and offering us with such a precious treasure. I’m truly appreciative!

bokep jepang · April 9, 2024 at 2:44 pm

Thanks for the several tips shared on this website. I have observed that many insurance providers offer consumers generous savings if they prefer to insure many cars with them. A significant amount of households currently have several vehicles these days, especially those with elderly teenage youngsters still dwelling at home, plus the savings for policies may soon begin. So it pays to look for a good deal.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *