This is Python Programming | No Questions Asked

--

I know what you’re thinking — what the h- is a set?

Photo by Shahadat Rahman on Unsplash

Well in Python programming, a set is:

“An abstract data type that stores unique elements and without any order.”

A set is unlike a list in Python or an array in general programming because these data types are ordered and can have non-unique elements in storage.

If you try to add an element to a set that already exists within storage, nothing will happen. Don’t believe me? Well let’s have a look under the hood.

example_string = "Hello"
example_set = set()
for char in string:
example_set.add(char)
print(example_set)
Result: set(['h', 'e', 'l', 'o'])

Notice that only a single ‘l’ character survives. Once a unique character is added to a set, the non-unique copies are ignored. This is why there is only one ‘l’.

Adding an element to a set

Adding an element to a set is easy, and we can use the add() function like above.

This function takes a singular input: add(element). The add() function also does not return a value.

Removing an element to a set

--

--

Justin Alvey
Rocketfy: A think space for Software Engineers

I’m passionate about #entrepreneurship, #programming, #space, #technology, and #writing.