DEV Community

Cover image for 10 Python Concepts That Finally “Clicked”
Naved Shaikh
Naved Shaikh

Posted on

10 Python Concepts That Finally “Clicked”

We’ve all been there: you think you know Python, and then a weird bug keeps you up until 2 AM. For the longest time, I thought I was an “expert” because I knew the syntax. But the real breakthroughs happened when I stopped memorising and started understanding the why.

Here are the 10 “light-bulb moments” that changed how I write code.

1. Mutable vs. Immutable Objects
The classic “Why is my list changing?!” moment.

  • Immutable (int, str, tuple): When you change it, Python creates a new object.
  • Mutable (list, dict, set): You’re modifying the original object in place.
def add_item(items, value):
    items.append(value)
    return items

my_list = [1, 2, 3]
add_item(my_list, 4)
print(my_list)  # [1, 2, 3, 4] - The original list was modified!
Enter fullscreen mode Exit fullscreen mode

2. The Danger of Default Mutable Arguments
This one is a rite of passage. If you use a list as a default argument, Python evaluates it once when the function is defined, not every time it’s called.

The Bug:

def add_to_list(val, items=[]):
    items.append(val)
    return items

print(add_to_list(1)) # [1]
print(add_to_list(2)) # [1, 2] - Wait, what?
Enter fullscreen mode Exit fullscreen mode

The Fix: Always use None as the default.

3. Pass-by-Object-Reference
Is Python “pass-by-value” or “pass-by-reference”? Actually, it’s pass-by-object-reference. Think of variables as tags stuck onto objects. If you pass a mutable object (like a list) to a function, the function gets a tag for that same object. If it’s immutable (like an integer), the function can’t change the original.

4. is vs ==
I used to use these interchangeably. Huge mistake.

  • == checks if the values are the same.

  • is checks if they are the exact same object in memory.

a = [1, 2]
b = [1, 2]
print(a == b) # True
print(a is b) # False (Two different lists that happen to look the same)
Enter fullscreen mode Exit fullscreen mode

5. Iterators and Generators
I used to build massive lists just to loop through them. Then I discovered Generators. Instead of storing 1 million items in RAM, a generator calculates them on the fly using yield.

def countdown(n):
    while n > 0:
        yield n
        n -= 1

for i in countdown(3):
    print(i) # Memory efficient and fast.
Enter fullscreen mode Exit fullscreen mode

6. List Comprehensions vs. Generator Expressions
They look almost identical, but the brackets matter:

  • [x for x in range(100)] → List: Memory hog, builds the whole
    thing now.

  • (x for x in range(100)) → Generator: Lazy, builds items only
    when you ask for them.

7. Context Managers (with)
We’ve all forgotten to close a file or a database connection. Using with ensures that even if your code crashes, Python cleans up after itself. It’s not just for files; you can use it for locks, sockets, and custom cleanup using __enter__ and __exit__.

8. The “Magic” of *args and **kwargs
These used to look like hieroglyphics to me. Now, I see them as the key to flexible code.

  • *args = list of positional arguments.

  • **kwargs = dictionary of named arguments. They allow you to write
    functions that can handle any number of inputs.

9. Decorators
Decorators are just functions that wrap other functions to add extra behaviour (like logging or timing). Once you realise a decorator is just a function returning another function, the “magic” disappears and the power remains.

def log_call(func):
    def wrapper(*args, **kwargs):
        print(f"Executing {func.__name__}...")
        return func(*args, **kwargs)
    return wrapper

@log_call
def greet(name):
    print(f"Hi {name}")
Enter fullscreen mode Exit fullscreen mode

10. if __name__ == "__main__":
This isn’t just boilerplate. It tells Python: “Only run this code if I executed this specific file.” If I import this file elsewhere, the code under this block won't run. It’s the difference between a library and a script.

Top comments (2)

Collapse
 
naved_shaikh profile image
Naved Shaikh

Which of these was your biggest “aha!” moment?

Collapse
 
naved_shaikh profile image
Naved Shaikh

Python is easy to pick up, but its “quirks” are what make it powerful. If these didn’t click for you on day one, don’t worry it took me years of trial and error to truly get them.