When I wrote my first Python script, I thought programming was just about typing instructions and getting output. I was wrong. 😅
After dozens of scripts (and countless errors), I realized Python is not just a language – it's a way of thinking. Here are 5 concepts that changed everything for me.
- Variables Are Names, Not Boxes In many languages, variables are like boxes where you store values. In Python, they're more like labels pointing to objects.
python
a = [1, 2, 3]
b = a
b.append(4)
print(a) # [1, 2, 3, 4] ← Surprise! 'a' changed too!
Why? Because a and b point to the same list object. You didn't copy the list – you copied the reference.
Fix:
python
b = a.copy() # Now they're independent
Understanding this early saves hours of debugging "mysterious" bugs.
- The Power of f-Strings I used to concatenate strings like a caveman:
python
name = "Mahdi"
age = 25
print("My name is " + name + " and I am " + str(age) + " years old.")
So much + and str()... ugh.
Then I discovered f-strings:
python
print(f"My name is {name} and I am {age} years old.")
Clean. Readable. Beautiful.
And you can even put expressions inside:
python
print(f"Next year I'll be {age + 1}.")
print(f"My name in uppercase: {name.upper()}")
Once you go f-string, you never go back.
- List Comprehensions Are Not Scary I used to write 4-line loops for simple list operations:
python
squares = []
for i in range(10):
if i % 2 == 0:
squares.append(i ** 2)
Then I learned this one-liner:
python
squares = [i ** 2 for i in range(10) if i % 2 == 0]
Result: [0, 4, 16, 36, 64]
The structure: [expression for item in iterable if condition]
Another real-world example:
python
Get all active usernames in uppercase
usernames = [user.name.upper() for user in users if user.is_active]
Readable. Pythonic. Fast.
- Functions Are First-Class Citizens In Python, you can pass functions as arguments, store them in lists, return them from other functions...
python
def greet(name):
return f"Hello, {name}!"
def shout(func):
return func().upper()
Passing a function
print(shout(lambda: greet("Mahdi"))) # HELLO, MAHDI!
This opens the door to decorators, callbacks, and cleaner code architecture.
Real use case – sorting with a custom key:
python
students = [{"name": "Ali", "grade": 85}, {"name": "Sara", "grade": 92}]
sorted_students = sorted(students, key=lambda s: s["grade"], reverse=True)
- Virtual Environments Will Save You I ignored virtual environments for months. Biggest mistake.
Without them:
Project A needs requests==2.25
Project B needs requests==2.28
Chaos. Dependency hell. Tears.
With virtual environments:
bash
Create one per project
python -m venv venv
Activate it
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
Install whatever you want – it stays isolated
pip install requests pandas numpy
Clean. Isolated. Professional.
Pro tip: Always add venv/ to your .gitignore file.
🎁 Bonus: The if name == "main" Mystery Solved
I saw this everywhere but didn't understand it:
python
def main():
print("Running the script...")
if name == "main":
main()
What it means:
When you run the script directly: python script.py → main() executes
When you import it: import script → main() does NOT execute
It separates code that should only run when the script is executed directly from code that should always be available on import.
📝 Summary Table
Concept Why It Matters
Variables as references Avoids unexpected mutations
f-Strings Cleaner, faster string formatting
List Comprehensions Less code, more readability
First-Class Functions Enables decorators and flexible APIs
Virtual Environments Dependency isolation per project
🚀 What's Next?
These 5 concepts transformed how I write Python. I'm now building automation scripts with n8n and Docker, and these fundamentals make every step smoother.
What Python concept took you way too long to understand? Drop it in the comments – let's help the next beginner skip our mistakes! 🐍
Top comments (0)