While preparing for DSA and technical interviews, I realized something pretty quickly:
Knowing Python syntax is not the same as knowing when to use it.
So instead of trying to finish Python as quickly as possible, I've been following a different approach:
Learn a concept → understand the practical use → create a cheat sheet → solve problems → identify patterns.
Recently, I completed two Python topics that look simple on the surface but become surprisingly useful when solving problems:
- Sets
- Tuples
This is what I learned.
1. Sets
A set is an unordered collection of unique elements.
numbers = {1, 2, 3, 4}
Unlike lists, sets don't keep duplicate values.
numbers = [1, 2, 2, 3, 4, 4]
unique_numbers = set(numbers)
print(unique_numbers)
The duplicates are automatically removed.
The most important pattern: seen
One of the most useful things I learned from sets was the seen pattern.
Suppose I want to check whether a list contains a duplicate.
Instead of maintaining a separate list of duplicates, I can ask:
"Have I seen this value before?"
numbers = [1, 2, 3, 4, 5, 3, 2]
seen = set()
for num in numbers:
if num in seen:
print(True)
break
seen.add(num)
The moment a value appears again, we know a duplicate exists.
This pattern is much more important than simply knowing how to create a set.
2. The Four Set Operations I Need to Remember
Suppose:
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
Union — |
Everything from both sets:
A | B
Result:
{1, 2, 3, 4, 5, 6}
Intersection — &
Elements common to both:
A & B
Result:
{3, 4}
Difference — -
Elements in A but not B:
A - B
Result:
{1, 2}
Symmetric Difference — ^
Elements present in exactly one of the sets:
A ^ B
Result:
{1, 2, 5, 6}
My mental shortcut is:
| → EVERYTHING
& → COMMON
- → ONLY LEFT
^ → EXACTLY ONE
This became especially useful when solving problems written in natural language.
For example:
"Students registered but not logged in"
Immediately becomes:
registered - logged_in
And:
"Students attending both sessions"
becomes:
morning & evening
The goal is to recognize the operation from the wording.
3. Sets and the Two Sum Pattern
Another important pattern was using a set to remember values we've already encountered.
For:
numbers = [2, 7, 3, 9]
target = 10
Instead of checking every possible pair, we can calculate what value we need:
needed = target - num
Then check whether that value has already been seen.
seen = set()
for num in numbers:
needed = target - num
if needed in seen:
print(True)
break
seen.add(num)
The important idea isn't just the code.
It's:
current value → calculate required complement → check whether complement was seen
This is a pattern I expect to see repeatedly in DSA.
4. Tuples
A tuple is an:
ordered, immutable collection of elements that can contain duplicates.
data = (10, 20, 30)
The key difference from a list is mutability.
List → ordered + mutable + duplicates
Tuple → ordered + immutable + duplicates
Set → unique + unordered + mutable
5. Tuple Unpacking
One of the most useful tuple concepts I learned was unpacking.
Instead of:
name = student[0]
age = student[1]
course = student[2]
we can write:
name, age, course = student
Python assigns the values position by position.
This also appears naturally when working with dictionaries:
for key, value in data.items():
print(key, value)
6. Swapping Values
Python also makes swapping very clean:
a, b = b, a
No manually created temporary variable is required.
The right side is evaluated first and then unpacked into the variables.
7. Returning Multiple Values
A Python function can return multiple values:
def calculate(a, b):
return a + b, a * b
We can then unpack them:
total, product = calculate(3, 4)
The returned values behave like a tuple:
(7, 12)
and are unpacked into:
total → 7
product → 12
This is one reason understanding tuples is useful even when you're not explicitly writing tuple-heavy code.
8. Tuple Immutability
This doesn't work:
data = (10, 20, 30)
data[1] = 25
because tuples cannot be modified.
If I need a changed version, I have to create a new tuple.
For example, if every 20 needs to become 25:
data = (10, 20, 30, 20)
new_data = []
for num in data:
if num == 20:
new_data.append(25)
else:
new_data.append(num)
new_tuple = tuple(new_data)
Result:
(10, 25, 30, 25)
The original tuple remains unchanged.
9. Tuple Slicing
Tuples support slicing just like lists.
Reverse:
data[::-1]
First three elements:
data[:3]
So immutability does not mean we cannot create new data from a tuple.
It means we cannot modify the existing tuple.
10. Extended Unpacking
This was one of my favorite small Python features:
data = (10, 20, 30, 40, 50, 60)
first, *middle, last = data
Now:
first → 10
middle → [20, 30, 40, 50]
last → 60
An important detail:
The values captured by *middle become a list.
So even though the original object is a tuple:
type(data)
is:
tuple
while:
type(middle)
is:
list
The Bigger Lesson
The most useful part of learning Sets and Tuples wasn't memorizing methods.
It was learning to translate a problem statement into a pattern.
For example:
"Have I seen this before?"
↓
seen set
"Find common elements"
↓
A & B
"Present in A but not B"
↓
A - B
"Exactly one"
↓
A ^ B
"Take first, middle and last"
↓
*middle unpacking
That's the kind of thinking I want to build before moving deeper into DSA.
I'm currently following a structured Python → Problem Solving → DSA roadmap, and I'm deliberately solving problems topic-by-topic rather than jumping randomly between questions.
The next step is List Comprehensions, followed by the remaining Python fundamentals before moving deeper into problem-solving patterns and DSA.
The goal isn't simply to "finish Python."
The goal is to reach a point where I can look at a problem and start recognizing the underlying pattern.
Top comments (0)