Python Sets

Python Sets are the collection of multiple data like the list and tuple. The main difference between the list and the Python Sets is that the set elements are unordered and unindexed.

Declaration of Python sets

set_name={'a','d','e'}

We can also create the set by using a set constructor such as set_name=set(('a','d','e'))

In python programming, sets are written with the help of curly braces {}


Printing a set

Printing a set is simple like printing a variable.

Example of printing a set

my_set = {"PHP", "Python", "Java"}
print(my_set)
Tutorials Class - Output Window

{‘PHP’, ‘Java’, ‘Python’}


Topics to remember about Python Sets

Sets in Python are unordered and have a definite count. We can concatenate the sets, so many things.

Printing a specific element from the sets

We can’t print the specific value from the set by specifying an index. Because set elements don’t have a specific order and index.
But we can do the so by using condition statement.

Example to print a specific element from the set using conditional statement

letter_set = {'PHP','Python','Java'}
check_element = 'PHP'
if check_element in letter_set:
    output = check_element
    print(output)
Tutorials Class - Output Window

PHP


Modifying or updating elements of a set

We can’t modify element from the set directly. If we try to do the so, then python will show the error.

my_set = ("PHP", "Python", "java")
my_set[0] = "JavaScript"
print(my_set)
Tutorials Class - Output Window

Traceback (most recent call last):
File “even.py”, line 2, in
my_set[0] = “JavaScript”
TypeError: ‘set’ object does not support item assignment


Python Sets – Adding elements to a set

We can add any number of elements in set directly by using some inbuilt functions.

Example to add a single element to the set using add() function

my_Set = {"PHP", "Java", "Python"}
# by using add() function, we can add the single element to the set in Python
my_Set.add("JQuery")
print(my_Set)
Tutorials Class - Output Window

{‘PHP’, ‘Python’, ‘JQuery’, ‘Java’}

Example to add multiple elements to the set using update() function

# by the help of the update() function , we can add multiple elements to the set.
my_Set.update(["JavaScript", "CSS", "HTML"])
print(my_Set)
Tutorials Class - Output Window

{‘PHP’, ‘HTML’, ‘Python’, ‘Java’, ‘CSS’, ‘JavaScript’, ‘JQuery’}


Copying one set to the other Set

We can easily copy one set to the other by using the copy() method.

Example to copy one set to other

set_1 = {"PHP", "Java", "Python"}
set_2 = set_1.copy()
print(set_2)
Tutorials Class - Output Window

{‘Java’, ‘Python’, ‘PHP’}


Checking length of a Python set:

With the help of len() function we can check the length of the set.

Example to calculate the length of a set

my_set = {'T','u','t','o','r','i','a','l','s','C','l','a','s','s'}
print(len(my_set))
Tutorials Class - Output Window

10

Description of output

Here you can see the output is10, but the number of elements are 14. This is because set doesn’t consider duplicate values. If a value is present multiple times, then it will be considered only one time in Python Set.


Removing the specific element from the set

With the help of discard() function, we can remove the specific element from the set. This function will delete both the uppercase and lowercase values(similar to the argument) from the set.

Example to remove specific element from the set

my_set = {'T','u','t','o','r','i','a','l','s','C','l','a','s','s'}
my_set.discard('t')
print(my_set)
Tutorials Class - Output Window

{‘u’, ‘C’, ‘T’, ‘s’, ‘i’, ‘l’, ‘r’, ‘a’, ‘o’}

Description of output

This function will delete both the uppercase and lowercase values from the set.


Python Sets – Deleting a set

In Python programming, we can delete the whole set as well.

Example to delete the whole set

my_set = {"apple", "banana", "cherry"}
del my_set
Description of output

Here you can see that the output is blank because we have deleted the set. So, it will not return anything.

Removing elements from the set using clear() function

We can also delete the elements of the set by using a clear() function. The main difference between the clear and delete() is that clear delete all elements while delete() deletes the set completely.

my_set = {'T','u','t','o','r','i','a','l','s','C','l','a','s','s'}
print(my_set.clear())
Tutorials Class - Output Window

my_set={}

Description of output

This is will make the set none.


Checking if any item exists in Python set

Like list, we can easily check if a element is present in the set or not.

Example to check a element is existing in the set or not

my_set = {"Ramesh", "Suresh", "Rakesh"}
if "Ramesh" in my_set:
  print("Yes, 'Ramesh' is in this set")
else:
   print("No, 'Ramesh' is not in this set")
Tutorials Class - Output Window

Yes, ‘Ramesh’ is in this set


Finding the maximum value in the set

Like the list, we can also find the maximum value in a set by using max() function.

Example to find out the maximum value from the set

my_set = {1,2,2,1,2,3}
print(max(my_set))
Tutorials Class - Output Window

3


Finding the minimum value in the set

With the help of the min() we can also find the minimum value in a set.

Example to find out the minimum value from the set

my_set = {1,2,2,1,2,3}
print(min(my_set))
Tutorials Class - Output Window

1


Finding the sum of values in the set

We can easily the sum of all the values of a set by using sum() method.

Example to calculate the sum of set elements

my_set = {1,2,2,1,2,3}
print(sum(my_set))
Tutorials Class - Output Window

6

Description of output

Here you can see that the sum is 6, because set doesn’t consider duplicate values. If a value is present multiple times, then it will be considered only one time in Python Set.


Python Sets – Finding the average of elements

Like the list, we can calculate the average easily.

Example to calculate the average of the set elements

my_set = {1,2,2,1,2,3}
print(sum(my_set)/len(my_set))
Tutorials Class - Output Window

2.0

Description of output

Here you can see that the average is 2, because set doesn’t consider duplicate values. If a value is present multiple times, then it will be considered only one time in Python Set. (1+2+3)/3.



Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.