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 Program To Check Whether Element Present In Set Or Not Example Python 04-10-2023
Python Program To Find Maximum And Minimum Number In A Set Python 04-10-2023
Python Program To Check Symmetric Matrix Python 04-10-2023
Python Program To Find Subsets Of A Set Python 04-10-2023
Python Program To Find Power Set Of A Set Python 04-10-2023
Remove All Duplicates From List Python Python 04-10-2023
Python Program To Find Symmetric Difference Of Two Sets Python 27-09-2023
Python Program To Find Common Item From Two Set Python 27-09-2023
Python Program To Get Unique Values From A List Python 27-09-2023
Python Encode And Decode String With Key Python 24-09-2023
Python Simple Encrypt Decrypt String Python 24-09-2023
Python Format String To Specific Length Python 24-09-2023
Python Code To Check If String Contains Substring Python 24-09-2023
Python Program To Find Most Repeated Word In A String Python 23-09-2023
Split String Into Words Python Python 23-09-2023
Remove All Punctuation Python Python 23-09-2023
Python Program To Reverse An Array Python 23-09-2023
Python Program To Find Number Of Palindrome In A String Python 23-09-2023
Python Program To Find Longest Common Substring Python 23-09-2023
Python Program To Find Number Of Days In A Given Month And Year Python 22-09-2023
Python Program To Calculate Age Of A Person Python 22-09-2023
Python Code To Get Day Of Week Python 22-09-2023
Python Convert String To Date Without Time Python 22-09-2023
Python Program To Print Current Date And Time In Format dd/mm/yyyy Python 22-09-2023
Python Program To Find Working Days In A Month Python 19-09-2023
Python Code To Change Date Format Python 16-09-2023
Python Program To Calculate Number Of Days Between Two Dates Python 16-09-2023
Python Program To Calculate Age In Years Months And Days Python 16-09-2023
Python Program To Schedule A Job To Run After A Certain Amount Of Time Python 10-08-2023
Python Program To Schedule A Job To Run Randomly Once A Day Python 10-08-2023

1 2 3 4