# Python. Operations and functions for defining supersets and subsets. Comparison of sets

## Operations and functions for defining supersets and subsets. Comparison of sets

### Contents

Search other websites:

##### 1. Operations >, >=. Superset. Example

The operations >, >= are intended to determine if a given set is a superset of another. The result of the operations is True or False.

The general form of the operation > next

`set1 > set2`

where

• set1 – a set that is considered as a superset. If all elements of set2 are in the set1 and set1!=set2 (the sets are not equal), then the result of the operation is True. Otherwise, the result of the operation is False;
• set2 – a set that is considered as a subset.

The general form of the operation >= next

`set1 >= set2`

where set1, set2 are sets that are considered respectively as a superset and subset.

The operation >= returns True if one of the conditions is true:

• all elements of set2 are included in set1;
• set1==set2 (sets are equal).

In operations >, >= sets cannot be empty.

Example.

```# Sets
# 1. Operation >. Determining if a set is a superset
x = { 1, 2, 3 }
y = { 2 }
f = x > y # f = True

x = { 'а', 'b', 'с' }
y = { 'а', 'b', 'с' }
f = x > y # f = False - sets are equal

x = { 1, 2, 3 }
y = { 1, 4 }
f = x > y # f = False

# 2. Operation >=

x = { 1, 2, 3 }
y = { 1, 2, 3 }
f = x >= y # f = True

x = { 'abc', True, 5.66 }
y = { True, 5.66 }
f = x >= y # f = True```

### ⇑

##### 2. Operations <, <=. Subset. Example

The operations <, <= are intended to determine if a given set is a subset of another. The difference between the operations < and <= occurs when the compared sets are equal to each other. If the sets are equal, then the operation < returns False, and the operation <= returns True.

The general form of the operation < next

`set1 < set2`

where

• set1 – a set that is considered as a subset. If all elements of set1 are placed in set2 and the sets are not equal (set1!=set2), then the result of the operation is True;
• set2 – a set that is considered as a superset.

The general form of the operation <= next

`set1 <= set2`

where set1, set2 are nonempty sets that are considered as a subset and superset.

The result of the operation <= is True if one of the conditions is true:

• all elements of set1 are included in set2;
• sets are equal (set1==set2).

In operations <, <= sets cannot be empty.

Пример.

```# Sets
# 1. Operation <
x = { 1, 2, 3 }
y = { 1, 3, 5, 2, 8, 9 }
f = x < y # f = True

x = { 5 }
y = { 2, 'b', 7.20 }
f = x < y # f = False

# 2. Operation <=

x = { 2.8, 2.9, 3.2 }
y = { 2.9, 3.2, 2.8 }
f = x <= y # f = True - sets are equal

x = { 'a', 'c', 'd' }
y = { 'c', 'a' }
f = x <= y # f = False```

### ⇑

##### 3. Operations of sets comparison ==, !=. Example

The operations ==, != Are used to compare sets for equality/inequality. The result of the operations is True or False.

The general form of the equality checking == is as follows

`set1 == set2`

where set1, set2 – sets that are compared for equality. If the sets are equal, then True is returned, otherwise False is returned.

The general form of the inequality check operation != Is as follows

`set1 != set2`

where set1, set2 – sets that are compared on inequality. If the sets are unequal, then True is returned; otherwise, False is returned.

Example 1. Using the operation == (equality of sets).

```# Sets
# Operation ==. Comparison of sets for equality
x = { 1, 2, 3 }
y = { 1, 3, 2 }
f = x == y # f = True
print('f = ', f)

# Definition of equality of sets in an if statement
x = { 'a', 'b', 'c', 'd' }
y = { 'b', 'a', 'c' }

if x==y:
print("Sets are equals.")
else:
print("Sets are not equals.")```

The result of the program

```f = True
Sets are not equals.```

Example 2. Using the operation != (inequality comparison).

```# Sets
# Operation !=

# The use in if statement
x = set('hello')
y = { 'h', 'e', 'l', 'o' }

print('x = ', x)
print('y = ', y)

if x!=y:
print("Sets are not equals.")
else:
print("Sets are equals.")```

The result of the program

```x = {'h', 'l', 'e', 'o'}
y = {'h', 'l', 'e', 'o'}
Sets are equals.```

### ⇑

##### 4. Function issubset(). Subset determining

The issubset() function is designed to determine if the current set is a subset of another. The general form of function declaration is as follows

`set.issubset(other)`

where

• set – current subset;
• other – another set that is compared to the entry with the set.

The issubset() function returns True if one of the conditions is true:

• set is a subset of other. In other words, all elements of set are in the set other;
• the sets set and other are equal.

In other cases, the issubset() function returns False. The issubset() function can be replaced with the <= operation.

Example.

```# Sets
# Function issubset()
x = { 2, 3, 4 }
y = { 3, 4 }
f1 = x.issubset(y) # f1 = False
f2 = y.issubset(x) # f2 = True```

### ⇑

##### 5. Function issuperset(). Superset definition

The issuperset() function determines if the current set is a superset of another. The general function declaration form is as follows

`set.superset(other)`

where

• set – current set;
• other – another set which is associated with set. If set is a superset of other, then the function returns True. Otherwise, the function returns False.

The issuperset() function can be replaced with the >= operation.

Example.

```# Sets
# Function issuperset()
x = { 2, 4, 6, 8 }
y = { 2, 8 }
f1 = x.issuperset(y) # f1 = True
f2 = y.issuperset(x) # f2 = False```