Python – Sets

After reading this Python Sets topic, you will understand how to create the Set, and you will know how to accessing Items, changing Items and use Sets built-in methods in Python.

A set is a variable that collects same or different type of elements(items) does not exist in a sequence. The elements of the sets cannot repeat and set elements does not support index.

How to create Sets in python?

The set items should be written within the curly brackets (i.e. {}) and each item separated by a comma (,).

Example:

Program (1): To demonstrate how to create and display set in Python.

# storing items 2,3 and 4 in the set
x = {2,3,4}
# print a set
print(x)

output

(2, 3, 4)

Accessing Items from Sets using index not possible

Example:

Program (1): To demonstrate how to create set and access set items using index not possible in Python.

# storing items 2,3 and 4 in the set
x = {2,3,4}
print(x[0])
print(x[1])

Output(1)

print(x[0])
TypeError: 'set' object does not support indexing

Changing Items are not applicable using index in Set

Example:

Program (1): To show how changing set items not possible in Python.

# storing items 2,3 and 4 in the set
x = {2,3,4}
#print original set
print(x)
#change second item of set
x[1]=5
# print set with changed items
print(x)

Output(1)

{2, 3, 4}
TypeError: 'set' object does not support item assignment

Set handling build-in methods

Some of commonly used set handling methods like len(), sum(), max(), min() and remove().

len() : This method gives the total number of items in the set.

Example:

Program (1): To demonstrate how to use len() function on the set in Python.

# storing items 2,3 and 4 in the set
x = {2,3,4}
# using len() method to calculate to the total number of items in the set.
print(len(x))

Output(1)

3

sum(): This method gives the sum of all the items in a set.

Example:

Program (1): To demonstrate how to use sum() function on the set in Python.

# storing items 2,3 and 4 in the set
x = {2,3,4}
# using sum() method gives addition of all the items in set
print(sum(x))

Output(1)

9

max() : This method gives largest item in the set.

Example:

Program (1): To demonstrate how to use max() function on the set in Python.

# storing items 2,3 and 4 in the set
x = {2,3,4}
# using max() method to get minimum item
print(max(x))

Output(1)

4

min(): This method gives the smallest item in the set.

Example:

Program (1): To demonstrate how to use min() function on the set in Python.

# storing items 2,3 and 4 in the set
x = {2,3,4}
# using min() method to get minimum item
print(min(x))

Output(1)

2

remove() : This method remove an item from the set.

Example:

Program (1): To demonstrate how to use remove() function on the set in Python.

# storing items 2,3 and 4 in the set
x = {2,3,4}
# using remove() method to remove item 3 from the set
x.remove(3)
# print set
print(x)

Output(1)

{2, 4}

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

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