Skip to Content
🎉 Welcome to my notes 🎉
Python8. Tuples & Sets

📐 Tuples & Sets in Python

🔗 Tuples in Python

Tuples are immutable, ordered collections. Once created, their contents cannot be changed. Tuples are very much like lists, but they are used when you want to ensure that the data cannot be modified. Tuples are faster than lists for certain operations due to their immutability. Tuples are defined using parentheses ().

tuples.py
# Creating a tuple my_tuple = (1, 2, 3) print(my_tuple) # Output: (1, 2, 3) # Accessing elements print(my_tuple[0]) # Output: 1 print(my_tuple[1]) # Output: 2 print(my_tuple[2]) # Output: 3 # Nested tuples nested_tuple = (1, 2, (3, 4)) print(nested_tuple[2]) # Output: (3, 4) print(nested_tuple[2][0]) # Output: 3

🧩 Tuple Operations & Methods

Tuples support various operations, but they are limited compared to lists due to their immutability:

tuple_operations.py
my_tuple = (1, 2, 3) # Tuple operations print(len(my_tuple)) # Output: 3 print(2 in my_tuple) # Output: True print(my_tuple + (4, 5)) # Output: (1, 2, 3, 4, 5) print(my_tuple * 2) # Output: (1, 2, 3, 1, 2, 3) print(my_tuple[1:3]) # Output: (2, 3) # Tuple methods print(my_tuple.index(2)) # Output: 1 print(my_tuple.count(1)) # Output: 1

🎁 Tuple Unpacking

Tuple unpacking allows you to assign the elements of a tuple to multiple variables in a single statement:

tuple_unpacking.py
my_tuple = (1, 2, 3) a, b, c = my_tuple print(a) # Output: 1 print(b) # Output: 2 print(c) # Output: 3

🧮 Sets in Python

Sets are a built-in data type in Python that represent an unordered collection of unique elements. They are similar to mathematical sets and can be used to perform various operations like union, intersection, and difference.

set.py
# Creating a set my_set = {1, 2, 3, 4, 5} print(my_set) # Output: {1, 2, 3, 4, 5} my_set = set([1, 2, 3, 4, 5]) # Using the set constructor # Adding Elements my_set.add(6) print(my_set) # Output: {1, 2, 3, 4, 5, 6} my_set.update([7, 8]) print(my_set) # Output: {1, 2, 3, 4, 5, 6, 7, 8} # Removing Elements my_set.remove(8) # Raises KeyError if 8 is not present print(my_set) # Output: {1, 2, 3, 4, 5, 6, 7} my_set.discard(7) # Does not raise an error if 7 is not present print(my_set) # Output: {1, 2, 3, 4, 5, 6} my_set.pop() # Removes and returns an arbitrary element print(my_set) # Output: {2, 3, 4, 5, 6} (example output)

🧩 Set Operations

Sets support various mathematical operations, such as union, intersection, and difference.

set_operations.py
set_a = {1, 2, 3} set_b = {3, 4, 5} # Union set_union = set_a | set_b # or set_a.union(set_b) print(set_union) # Output: {1, 2, 3, 4, 5} # Intersection set_intersection = set_a & set_b # or set_a.intersection(set_b) print(set_intersection) # Output: {3} # Difference set_difference = set_a - set_b # or set_a.difference(set_b) print(set_difference) # Output: {1, 2}

🛠️ Set Comprehensions

Set comprehensions allow you to create sets in a concise way, similar to list comprehensions.

set_comprehension.py
# Set comprehension squares = {x**2 for x in range(10)} print(squares) # Output: {0, 1, 4, 9, 16, 25, 36, 49, 64, 81} # Filtering with set comprehension evens = {x for x in range(10) if x % 2 == 0} print(evens) # Output: {0, 2, 4, 6, 8}

📚 Set Methods

Sets come with several built-in methods that allow you to manipulate them easily.

set_methods.py
# Set methods my_set = {1, 2, 3, 4, 5} print(len(my_set)) # Output: 5 (number of elements) print(3 in my_set) # Output: True (check membership) print(my_set.isdisjoint({6, 7})) # Output: True (no common elements) print(my_set.issubset({1, 2, 3, 4, 5, 6})) # Output: True (my_set is a subset) print(my_set.issuperset({2, 3})) # Output: True (my_set is a superset)
Last updated on