IKH

Introduction

  • If we want to represent a group of unique values as a single entity then we should go for set.

Properties

  • Duplicates are not allowed.
  • Insertion order is not preserved. But we can sort the elements.
  • Indexing and slicing not allowed for the set.
  • Heterogeneous elements are allowed.

Remark

  • Set objects are mutable i.e. once we creates set object we can perform any changes in that object based on our requirement.
  • We can represent set elements within curly braces and with comma separation.
  • We can apply mathematical operations like union, intersection, difference … etc. on set objects.

Creation of set object

There are two methods to create set.

s={element, …}

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

Output

PowerShell
{40, 10, 20, 30}
<class 'set'>

s=set(any sequence)

Python
l = [10,20,30,40,10,20,10]
s=set(l)
print(s)

Output

PowerShell
{40, 10, 20, 30}

Example

Python
s=set(range(5))
print(s)

Output

PowerShell
{0, 1, 2, 3, 4}

Note

  • For creating empty set compulsory we should use set() function.

Example

Python
s={} 
print(s) 
print(type(s))

Output

PowerShell
{}
<class 'dict'>

Example

Python
s=set() 
print(s) 
print(type(s))

Output

PowerShell
set()
<class 'set'>

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error