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.
Similar Programs Chapter Last Updated
Python Date Difference In Days Python 01-04-2023
Bellman Ford Algorithm In Python Python 25-03-2023
Python Program To Display Current Date And Time Python 24-03-2023
Binary Search Tree Implementation Python Python 21-03-2023
Python Program To Check Palindrome Number Python 12-03-2023
How to Merge Two PDF Files Using Python Python 12-03-2023
Python Tuples Example Python 19-11-2021
Python Iterator Example Python 12-11-2021
Python Lambda Functions Python 11-11-2021
Integer To String In Python Python 22-10-2021
Python Datetime Format Python 21-10-2021
Range Function In Python | Python range () Python 11-10-2021
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

1 2