Get The Most Affordable Hosting in the World!

Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.

Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!

Get Started Now!

A set is one of the built-in data types in Python. In mathematics, set is a collection of distinct objects. Set data type is Python's implementation of a set. Objects in a set can be of any data type.

Set in Python also a collection data type such as list or tuple. However, it is not an ordered collection, i.e., items in a set or not accessible by its positional index. A set object is a collection of one or more immutable objects enclosed within curly brackets {}.

Example 1

Some examples of set objects are given below −

 
s1 = {"Rohan", "Physics", 21, 69.75} s2 = {1, 2, 3, 4, 5} s3 = {"a", "b", "c", "d"} s4 = {25.50, True, -55, 1+2j} print (s1) print (s2) print (s3) print (s4)

It will produce the following output −

{'Physics', 21, 'Rohan', 69.75}
{1, 2, 3, 4, 5}
{'a', 'd', 'c', 'b'}
{25.5, -55, True, (1+2j)}

The above result shows that the order of objects in the assignment is not necessarily retained in the set object. This is because Python optimizes the structure of set for set operations.

In addition to the literal representation of set (keeping the items inside curly brackets), Python's built-in set() function also constructs set object.

set() Function

set() is one of the built-in functions. It takes any sequence object (list, tuple or string) as argument and returns a set object

Syntax

Obj = set(sequence)

Parameters

  • sequence − An object of list, tuple or str type

Return value

The set() function returns a set object from the sequence, discarding the repeated elements in it.

Example 2

 
L1 = ["Rohan", "Physics", 21, 69.75] s1 = set(L1) T1 = (1, 2, 3, 4, 5) s2 = set(T1) string = "TutorialsPoint" s3 = set(string) print (s1) print (s2) print (s3)

It will produce the following output −

{'Rohan', 69.75, 21, 'Physics'}
{1, 2, 3, 4, 5}
{'u', 'a', 'o', 'n', 'r', 's', 'T', 'P', 'i', 't', 'l'}

Example 3

Set is a collection of distinct objects. Even if you repeat an object in the collection, only one copy is retained in it.

 
s2 = {1, 2, 3, 4, 5, 3,0, 1, 9} s3 = {"a", "b", "c", "d", "b", "e", "a"} print (s2) print (s3)

It will produce the following output −

{0, 1, 2, 3, 4, 5, 9}
{'a', 'b', 'd', 'c', 'e'}

Example 4

Only immutable objects can be used to form a set object. Any number type, string and tuple is allowed, but you cannot put a list or a dictionary in a set.

 
s1 = {1, 2, [3, 4, 5], 3,0, 1, 9} print (s1) s2 = {"Rohan", {"phy":50}} print (s2)

It will produce the following output −

   s1 = {1, 2, [3, 4, 5], 3,0, 1, 9}
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'list'
   s2 = {"Rohan", {"phy":50}}
        ^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'dict'

Python raises TypeError with a message unhashable types 'list' or 'dict'. Hashing generates a unique number for an immutable item that enables quick search inside computer's memory. Python has built-in hash() function. This function is not supported by list or dictionary.

Even though mutable objects are not stored in a set, set itself is a mutable object. Python has a special operators to work with sets, and there are different methods in set class to perform add, remove, update operations on elements of a set object.

 

 

 

The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

這篇文章有幫助嗎? 1 用戶發現這個有用 (1 投票)