Set In Python

Chapter: Python Last Updated: 05-10-2018 12:43:06 UTC


            /* ............... START ............... */
# Python set example
set_example = {"java", "Python", "c#"}

# By using for loop, printing the content in the set
for x in set_example: print(x)

# Check if the entry is present in the set
set_example = {"java", "Python", "c#", "c++"}
print("c++" in set_example)
print("c" in set_example)

# Add method is used to add one entry in set.

#Update method is used to add more than one entry
set_example.update(["Javascript", "TypeScript", "Ruby"])

# Len function is used to print the length of the set in Python
print(len(set_example)) # Length : 8

# remove function will remove the entry from set

# The pop() randomly remove element from the set and returns the element removed.
x = set_example.pop()
                /* ............... END ............... */


{'java', 'Python', 'c#'}
{'java', 'Python', 'c#', 'c++', 'Swift'}
{'Javascript', 'java', 'Python', 'c#', 'Ruby', 'c++', 'Swift', 'TypeScript'}
{'java', 'Python', 'c#', 'Ruby', 'c++', 'Swift', 'TypeScript'}
{'Python', 'c#', 'Ruby', 'c++', 'Swift', 'TypeScript'}


  • As like list set is collection in python which is unordered and unindex set , which is enclosed in two curly brackets.
  • By using print method we can print the sets eg : '{'Python', 'java', 'c#'}.
  • We can print the contents of the set using for loop in Python, please refer program for clarification.
  • By using the in operation, we can check the whether the entry is present in the set. In the above program first we are checking whether c++ is present in the set and got true value, but when checking c it will print false mentioning that it is not in the set.
  • In python add method is used to add one entry in the set. But by using update method we can add more than one entry at at time. Kindly refer the program for more clarification.
  • len function in python is used to find the length of the set. Syntax : len(set).
  • remove method is used to remove the entry in set. In the example you can see that "JavaScript" has been removed from the set.
  • The pop() in python randomly remove element from the set and returns the element removed.


set in python example, Python set Example, python set update, set in python 3, python set add, python set operators

Similar Programs Chapter Last Updated
Python Desktop Notification Popup In Linux Python 10-07-2021
Python JSON Parser Example | How To Parse JSON In Python Python 25-06-2021
Python PIP | How To Install Packages In Python Python 16-06-2021
How To Access Python Dictionary Python 14-06-2021
Dictionary In Python Examples Python 10-06-2021
Shortest Path Algorithm In Python Python 23-02-2021
Merge Sort In Python Python 01-12-2020
Kruskal Algorithm In Python Python 28-11-2020
Python Mysql Connector Example Python 21-11-2020
Add To Set Python Python 05-10-2018
List In Python Python 29-09-2018
Integer To String Python Python 29-09-2018
Python Variables Python 21-09-2018
Python String Contains Python 15-09-2018
String In Python Python 22-09-2018
Python Switch Statement Python 20-09-2018
Python Function Example Python 14-09-2018
Python If Statement Python 14-09-2018
Area Of Triangle In Python Python 14-09-2018
Python Square Root Python 22-09-2018
Add Two Numbers In Python Python 13-09-2018
Python Comments Python 22-09-2018
Python Hello World Python 14-09-2018