DelphiLearn PythonPythonPython GUIRAD Studio

Quickly Learn How To Use Python Sets In A Windows GUI App


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.

Open Demo01.dproj

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.


Output in Python4Delphi:

Creating Python Sets 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:

Output in Python4Delphi:

Duplicates not Allowed in sets 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:

Output in Python GUI:

Python Set Items with Various Data Types Shown in Python4Delphi.

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


A Set can Contain Different Data Types in Python4Delphi.


3. The set() Constructor in Python

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


The set() Constructor in Python as Shown in Python4Delphi.


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:

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.

Related posts
Learn PythonPythonVideos

Learn Python With The Hidden Power of the Python Runtime by Elizaveta Shashkova (PyCon 2020) Videos

Learn PythonPythonVideos

Learn Python With Organizing to Improve Your Workplace: Know Your Rights by Shauna Gordon-McKeon (PyCon 2020) Video

DelphiPythonPython GUIWindows

Learn To Build A Python GUI For Manipulating Datetime Using Delorean Library In A Delphi Windows App

DelphiDelphiVCLPythonPython GUIWindows

How to use the DelphiVCL.DrawGrid.Row

Leave a Reply

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