Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
259 views

Python Sets

Sets in Python are unordered collections of unique elements that can be created using the built-in set() function. Sets can contain different data types and have optimized methods for checking membership. Sets cannot store mutable elements like lists or dictionaries. The document demonstrates how to create empty and populated sets from strings, objects, and lists.

Uploaded by

Gourav Kataruka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
259 views

Python Sets

Sets in Python are unordered collections of unique elements that can be created using the built-in set() function. Sets can contain different data types and have optimized methods for checking membership. Sets cannot store mutable elements like lists or dictionaries. The document demonstrates how to create empty and populated sets from strings, objects, and lists.

Uploaded by

Gourav Kataruka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Sets

In Python, Set is an unordered collection of data type that is iterable, mutable


and has no duplicate elements. The order of elements in a set is undefined
though it may consist of various elements.

The major advantage of using a set, as opposed to a list, is that it has a highly
optimized method for checking whether a specific element is contained in the
set.

Creating a Set

Sets can be created by using the built-in set() function with an iterable object or


a sequence by placing the sequence inside curly braces, separated by ‘comma’.

Note – A set cannot have mutable elements like a list, set or dictionary, as its
elements.

# Python program to demonstrate 


# Creation of Set in Python
  
# Creating a Set
set1 = set()
print("Intial blank Set: ")
print(set1)
  
# Creating a Set with 
# the use of a String
set1 = set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)
  
# Creating a Set with
# the use of Constructor
# (Using object to Store String)
String = 'GeeksForGeeks'
set1 = set(String)
print("\nSet with the use of an Object: " )
print(set1)
  
# Creating a Set with
# the use of a List
set1 = set(["Geeks", "For", "Geeks"])
print("\nSet with the use of List: ")
print(set1)
Output:

Intial blank Set:


set()

Set with the use of String:


{'e', 'r', 'k', 'o', 'G', 's', 'F'}

Set with the use of an Object:


{'r', 'o', 'e', 'F', 's', 'k', 'G'}

Set with the use of List:


{'Geeks', 'For'}

You might also like