Posts

Showing posts with the label sets

Sets in Python

Image
 Sets in Python Sets in Python are unordered collections of unique elements in Python. They are similar to lists but do not allow duplicate elements. Sets are defined using curly braces {} or the set() constructor. Key Characteristics: Unordered: Elements in a set do not have a specific order. Unique Elements: Sets cannot contain duplicate elements. Mutable: Sets can be modified by adding or removing elements. Iterable: You can iterate over the elements of a set using a loop. Creating Sets: # Creating a set using curly braces my_set = {1, 2, 3, 4, 5} # Creating a set using the set() constructor my_set = set([1, 2, 3, 4, 5]) Creating a set The set can be created by enclosing the comma-separated immutable items with the curly braces {}. Python also provides the set() method, which can be used to create the set by the passed sequence. Example 1: Using curly braces Days = { "Monday" ,  "Tuesday" ,  "Wednesday" ,  "Thursday" ,  "Friday&