I am learning Python as an experienced programmer. Here are my thoughts and experiences.
Mental model
- Everything is an Object.
- Mutable vs Immutable.
- Immutable: int, float,pars str, tuple, frozenset
- Mutable: list, dict, set, bytearray
- Python relies heavily on duck typing. This implies that Python depends on expected method at runtime.
Important Python data structures
list
A list is an ordered, mutable sequence of values. It is the most common general-purpose container in Python.
- Good for collections of related values
- Supports indexing, slicing, appending, and removal
- Example:
items = [1, 2, 3]
tuple
A tuple is an ordered, immutable sequence. Once created, it cannot be changed.
- Good for fixed collections and structured data
- Often used for coordinates, records, and return values
- Example:
point = (10, 20)
dict
A dict stores data as key-value pairs. Keys are unique and lookups are efficient.
- Good for mappings, configuration, and caches
- Keys must be hashable
- Example:
user = {"name": "Ada", "role": "engineer"}
set
A set is an unordered collection of unique values.
- Good for deduplication and membership testing
- Supports set operations like union and intersection
- Example:
seen = {"a", "b", "c"}
str
A string is an immutable sequence of characters.
- Good for text, labels, file names, and parsing
- Example:
message = "hello world"
None
None represents the absence of a value in Python.
- It is not the same as
0,False, or an empty string - It is commonly used as a default or sentinel value
Slicing & Indexing
a = [0, 1,2,3,4,5]
a[1:4] # returns [1, 2, 3] a[-1] # returns 5 a[:3] # returns [0, 1, 2] a[::2] # returns [0, 2, 4] (every second element) a[::-1] # returns [5, 4, 3, 2, 1, 0] (reversed list)
Pattern Matching
Extended unpacking
first, *middle, last = [1,2,3,4,5] # middle will be [2, 3, 4]
Structural pattern matching
match point: case (0, 0): print(“Origin”) case (x, 0): print(f”X-axis at {x}“) case (x, y): print(f”Point at ({x}, {y})“)
Dunder Methods
Python’s operator overloading and object interface design use double underscore (dunder) methods.
class Vector:
def __init__(self, x, y):
self.x, self.y = x, y
def __repr__(self): # String representation (like toString / disp)
return f"Vector({self.x}, {self.y})"
def __add__(self, other): # Overloads + operator
return Vector(self.x + other.x, self.y + other.y)
def __len__(self): # Overloads len(obj)
return 2Recommended Reading
- Learn X in Y minutes A single runnable page containing entire language syntax with rapid commentary for experienced programmers.