Set In Python

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

Program:

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

# 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.
set_example.add("Swift")
print(set_example)

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

# 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
set_example.remove("Javascript")
print(set_example)

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

Output

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

Notes:

  • 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.

Tags

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
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

1