A **set **in Python is an unordered collection of items. Every **set **element is unique (no duplicates) and must be immutable (cannot be changed). However, a **set **itself is mutable. We can add or remove items from it.

**Set** is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. **Set **is a collection which is both *unordered* and *unindexed*.

**Sets** are written with curly brackets. **Sets **can also be used to perform mathematical set operations like union, intersection, symmetric difference, etc.

This post will demonstrate how to run another Python feature in Python4Delphi with RAD Studio: **Sets** in Python GUI apps and gets the output.

**Prerequisites:** Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in this video Getting started with Python4Delphi.

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub.

Examples of sets with Python GUI by Python4Delphi:

## 1. Creating Python Sets

A set is created by placing all the items (elements) inside curly braces `{}`

, separated by comma, or by using the built-in `set()`

function.

It can have any number of items and they may be of different types (integer, float, tuple, string etc.). But a set cannot have mutable elements like lists, sets or dictionaries as its elements.

Examples:

1 2 3 |
fruits = {"apple", "banana", "cherry", "dragon fruit"} print(fruits) |

Output in Python4Delphi:

The above example shows us the proof of the nature of Python sets: Unordered. It means that the items in a set do not have a defined order. Set items can appear in a different order every time you use them, and cannot be referred to by index or key.

Another example will show us that duplicates are not allowed in sets. Sets cannot have two items with the same value:

1 2 3 |
students = {"Alice", "Bob", "Charlie", "Dirac", "Alice"} print(students) |

Output in Python4Delphi:

Duplicate values will be ignored.

## 2. Python Set Items – Data Types

Set items can be of any data type. For example: String, int and boolean data types:

1 2 3 4 5 6 7 |
set1 = {"apple", "banana", "cherry", "date"} set2 = {1, 19, 199, 1999} set3 = {True, False, False, True} print(set1) print(set2) print(set3) |

Output in Python GUI:

Another example, a set can contain different data types (string, int, boolean, float, and complex):

1 2 3 |
set1 = {"abc", 34, True, 40, 4.2, 2+3j} print(set1) |

Output:

## 3. The set() Constructor in Python

It is also possible for us to use the set() constructor to make a set:

1 2 3 4 |
setConstructor = set(("Alice", "Bob", "Charlie", "Dirac")) # Note the double round-brackets print(setConstructor) |

Output:

## 4. Other Python Set Methods

There are many set methods, some of which we have already used above. Here is a list of all the methods that are available with the set objects:

Method | Description |
---|---|

add() | Adds an element to the set |

clear() | Removes all elements from the set |

copy() | Returns a copy of the set |

difference() | Returns the difference of two or more sets as a new set |

difference_update() | Removes all elements of another set from this set |

discard() | Removes an element from the set if it is a member. (Do nothing if the element is not in set) |

intersection() | Returns the intersection of two sets as a new set |

intersection_update() | Updates the set with the intersection of itself and another |

isdisjoint() | Returns `True` if two sets have a null intersection |

issubset() | Returns `True` if another set contains this set |

issuperset() | Returns `True` if this set contains another set |

pop() | Removes and returns an arbitrary set element. Raises `KeyError` if the set is empty |

remove() | Removes an element from the set. If the element is not a member, raises a `KeyError` |

symmetric_difference() | Returns the symmetric difference of two sets as a new set |

symmetric_difference_update() | Updates a set with the symmetric difference of itself and another |

union() | Returns the union of sets in a new set |

update() | Updates the set with the union of itself and others |

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.