Introduction
Sets help you store unique items without any order. They are useful when you want to avoid duplicates.
Jump into concepts and practice - no test required
my_set = {item1, item2, item3}
# or
my_set = set([item1, item2, item3])fruits = {'apple', 'banana', 'cherry'}numbers = set([1, 2, 3, 4])
empty_set = set()fruits = {'apple', 'banana', 'apple', 'cherry'}
print(fruits)
empty_set = set()
print(empty_set){1, 2, 3} correctly creates a set with elements 1, 2, and 3.{} creates an empty dictionary, not a set.set() correctly creates an empty set.my_set = {1, 2, 2, 3, 4, 4}
print(my_set)my_set = set{1, 2, 3}
print(my_set)set() with parentheses, not curly braces.set{1, 2, 3} is invalid syntax and causes a SyntaxError.nums = [1, 2, 2, 3, 4, 4, 5], which code snippet correctly creates a set of unique elements from this list?unique_nums = {nums} creates a set with the entire list as one element (invalid). unique_nums = set(nums) correctly converts the list to a set. unique_nums = list(set(nums)) converts to set then back to list, which is not a set. unique_nums = {1, 2, 3, 4, 5} manually writes the set but is not dynamic.