Menu

Python Day 27 – Sets in Python: Properties, Methods and Mathematical Operations
📰

Python Day 27 – Sets in Python: Properties, Methods and Mathematical Operations

DEV Community: python·Vasanthakumari N·about 7 hours ago
#vNWGX8f4
#devtopython#devto#dev#enter#exit
Long article (1260 words) — no AI summary yet.
Reading 0:00
15s threshold

In Python, a Set is a collection of unique elements. It is mainly used when we want to store multiple values without duplicates.

A set is based on the mathematical concept of sets.

1. What is a Set?

A set is an unordered collection of distinct, hashable objects.

Property Description

Unordered Does not guarantee insertion order

Unique Elements Duplicate elements are not allowed

No Indexing Cannot access elements using index positions

No Slicing Slicing operations are not supported

No Step Operations Cannot access elements using step-based indexing

Hashable Elements Elements must be hashable

Mutable Elements can be added or removed after creation

Example

s = { 10 , 20 , 30 , 40 } print ( s )

s = { 10 , 10 , 10 , 20 , 30 , 40 } print ( s )

Enter fullscreen mode

Exit fullscreen mode

Output:

{10, 20, 30, 40} {10, 20, 30, 40}

Enter fullscreen mode

Exit fullscreen mode

Even though we provided duplicate values, Python stores only unique elements.

2.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.