Python Tricks
1. Multiple Assignment (Swapping)
In most languages, swapping two variables requires a temporary variable. In Python, you can do it in one line using tuple packing and unpacking.
Application: In-place array reversals, QuickSort partition functions, or swapping pointers in a Linked List.2. Unpacking with *
You can use the * operator to unpack the "rest" of an iterable into a list. This is amazing for dealing with variable-length data.
record = ("Alice", "alice@test.com", "555-1234", "555-9876")
name, email, *phone_numbers = record
print(name) # Alice
print(phone_numbers) # ['555-1234', '555-9876']
3. Simultaneous Loop Iteration
If you need to iterate through two arrays at the same time, never use an index loop. Use zip.
names = ["Alice", "Bob"]
scores = [90, 85]
for name, score in zip(names, scores):
print(f"{name}: {score}")
4. Chained Comparisons
Python allows you to chain comparison operators, exactly how you would write them in mathematics.
Application: Boundary checking in matrices or ensuring values fall within a specific range.5. Default Dictionary Values (.get())
When counting frequencies, trying to increment a key that doesn't exist throws a KeyError. Instead of writing an if/else block, use .get().
counts = {}
for char in "hello":
# If char isn't in dict, return 0, then add 1
counts[char] = counts.get(char, 0) + 1
collections.Counter or collections.defaultdict(int) is usually better, but if the interviewer restricts you to standard dictionaries, use .get()).
6. Slicing Magic
Python's slicing syntax [start:stop:step] is incredibly powerful.