Contents

## Python – Check if Sets are Equal

To check if given two sets are equal in Python, you can use Equal-to operator **==**. Use Equal-to operator, and pass the given two sets as operands to the operator. The operator returns True if the sets are equal, or False otherwise.

The two sets are said to be equal, if they contain same items.

The expression to check if the sets: **set_1** and **set2** are equal is

`set_1 == set_2`

This expression returns **True** if the sets are equal, or **False** otherwise.

Since this expression returns a boolean value, we can use this expression as a condition in if, if else, or if elif statements.

## Examples

### 1. Checking if given sets are equal in Python

In this example, we take two sets in **set_1** and **set_2** with some initial values, and check if they are equal using Equal-to operator.

**Python Program**

```
set_1 = {"apple", "banana", "cherry"}
set_2 = {"apple", "banana", "cherry"}
if set_1 == set_2:
print("The two sets are EQUAL.")
else:
print("The two sets are NOT EQUAL.")
```

Run Code Copy**Output**

`The two sets are EQUAL.`

Since the two sets have same items, the equal-to operator returned True, and the if-block is executed.

### 2. Example with unequal sets

In this example, we take two sets in **set_1** and **set_2** with some initial values, such that the elements in the sets are not same, and check if they are equal using Equal-to operator.

**Python Program**

```
set_1 = {"apple", "banana", "cherry"}
set_2 = {"apple", "fig", "mango"}
if set_1 == set_2:
print("The two sets are EQUAL.")
else:
print("The two sets are NOT EQUAL.")
```

Run Code Copy**Output**

`The two sets are NOT EQUAL.`

### 3. Change order of items in Set and check if they are equal

In this example, we take two sets in **set_1** and **set_2** with some initial values, such that the elements in the sets are same, but the order of elements is different, and check if they are equal using Equal-to operator.

Please note that the order of items that we mention in a set, while initializing, does not affect their equality, because the items in a set are not ordered.

**Python Program**

```
set_1 = {"apple", "banana", "cherry"}
set_2 = {"banana", "cherry", "apple"}
if set_1 == set_2:
print("The two sets are EQUAL.")
else:
print("The two sets are NOT EQUAL.")
```

Run Code Copy**Output**

`The two sets are EQUAL.`

## Summary

In this tutorial of Python Sets, we learned how to check if sets are equal in Python, using Equal-to operator, with examples.