# Python Set

Among the four sequences in Python is an important ability to collect unordered data. You might have come across the word set in your mathematical adventure at a point in school, and you’re familiar with the definition that sets are the collection of clearly defined objects or things. Now that knowledge will be needed in this lesson.

• What is a Python Set?
• Types of python set
• Characteristics of the Python Set
• How to create a Python set
• How to modify
• Deleting elements in Python set
• Built-in python set methods

Then off you go to practice some set as I conclude

Contents

## What is a Python Set?

The python set is the unordered collection of comma-separated data enclosed in curly brackets {}. A set is mutable but its items must be immutable. Sets are used to store multiple data in variables, which enables you to carry out similar operations on different items at once. Conclusively, because sets are unordered data it has no business with indexing. It’s worth noting that set is among the four frequently used data types in Python; others are List, tuple, and dictionary. However, there are two types of sets in Python which are:

• The set
• The Frozen set

So, what’s unique about the Python sets? That lands us at the next thought

## Characteristics of the Python Set

All the data types in Python have unique Features that enable a distinct performance and the set is Known for:

• Sets are unordered. This means that any item can come before any.
• The set doesn’t allow repeated elements.
• A set can be mutable but its items must be immutable
• Set can contain any type of elements like float, string, integers, and tuple

With that being said, we must look at some examples in regard to the above characters. However, for all protocols observed, we have to create a set before further progress.

## How To Create a Python Set?

It’s not difficult to create a set. A set can be created by enclosing an unclose item on curly brackets {}. You can as well execute. some sets using the set() function.

Example 1: Using Curly brackets to create sets

Python Unit:

```months = {"January", "February", "march", "April", "May", "July", "August"}
print(months)
print(type(months))
print("looping through the set elements ... ")
for i in months:
print(i)

```
```Output:

{'January', 'February', 'March', 'April', 'May', 'June', 'July'}
<class 'set'>
looping through the set elements ...
May
February
January
June
April
July
March```

Example.2: Creating a set using the set() function

Python Input

```months = (["January", "February", "march", "April", "May", "July", "August"])
print(months)
print(type(months))
print("looping through the set elements ... ")
for I in months:
print(i)

```
```Output:

{'January', 'February', 'March', 'April', 'May', 'June', 'July'}
<class 'set'>
looping through the set elements ...
May
February
January
June
April
July
March```

Note. that the outputs aren’t ordered in the monthly sequence. That proves that sets are unordered

Now that we’ve created a set, it’s time we take off to some important operations on the python set

## How To Modify Python Set?

This example agrees with the fact that the set itself can be modified but its elements are immutable.

Interestingly, Python enables you to modify sets in two methods which are:

• update () function. To add multiple items to the set

Example 3: the add() and update() function

Python Input:

```# initialize my_set
my_set = {January, March}
print(my_set)
# my_set[0]
# if you uncomment the above line
# you will get an error
# TypeError: 'set' object does not support indexing
# Output: {January, February, March}
print(my_set)
# Output: {January, February, March, April}
my_set.update([February, March, April])
print(my_set)
# Output: {January, February, March, April, Mat, June, August}
my_set.update([April, May], {January, June, August})
print(my_set)

```
```Output:

{January, March}
{January, February, March}
{January, February, March, April}
{January, February, March, April, May, June, August}```

## Deleting Elements in Python Set

There are two ways to delete an isolated item in a Python set. They are:

• remove () function

Example 4: to display the discard () and the remove() delete functions.

Python Input

```# Difference between discard() and remove()
# initialize my_set
my_set = {January, March, April, May, june}
print(my_set)
# Output: {January, march, May, june}
print(my_set)
# remove an element
# Output: {January, march, May}
my_set.remove(june)
print(my_set)
# not present in my_set
# Output: {january, march, May}
print(my_set)
# remove an element
# not present in my_set
# you will get an error.
# Output: KeyError
my_set.remove(February)

```
```Output:

{January, March, April, May, June}
{January, March, May, June}
{January, March, May}
{January, March, May}
Traceback (most recent call last):
File "<string>", line 28, in <module>
KeyError: 2```

I want you to note that:

• discard() function won’t do any changes to the set of elements (February is present in the set.
• The remove() will return an error if the item(February) isn’t present in the list.

Before we progress with the basic operations on the Python set, let’s be clear on The two types of sets in Python:

## Set

This is the context of the content you’re reading which you’ve gotten some extent of knowledge on. So said, let’s take off with its contrast

## Frozen set

The frozen set is another type of set in Python that is immutable and it can be used as a key in the python dictionary. A frozen set is a contrast to the normal set that’s not frequently used. Although, it’s important and won’t be neglected.

The frozen set () function is used to create the frozen set data.

Example:

Python Input:

```Frozenset = frozenset([January, February, March, April, May])
print(type(Frozenset))
print("\nprinting the content of frozen set...")
for me in Frozenset:
print(i);
frozenset. add(June) #gives an error since we cannot change the content of Frozenset after creation

```
```Output:

<class 'frozenset'>
printing the content of the frozen set...
January
February
March
April
May```

Traceback (most recent call last):

```  File "set.py", line June, in <module>
frozenset. add(6) #gives an error since we can change the content of Frozenset after creation
AttributeError: 'frozenset' object has no attribute 'add'
```

## Operations On Python Set

The table below contains the basic operator’s inset

## Built-in Python Set  Methods

Python enables the following methods

## Summary

The python set is the unordered collection of comma-separated data enclosed in curly brackets {}. A set is mutable but its items must be immutable. Sets are used to store multiple data in variables, it enables you to carry out similar operations on different items at once

There are two types of sets in Python which are:

• The set
• The Frozen set

The set is Known for:

• Sets are unordered. This means that any item can come before any.
• The set doesn’t allow repeated elements.
• A set can be mutable but its items must be immutable
• Set can contain any type of element like float, string, integers, and tuple

A set can be created by enclosing an unclose item on curly brackets {}. You can as well execute. some sets using the set() function.