How to Code in Python: A Step-by-Step Guide for Absolute Beginners
The Day I Realized I Needed Python
I was sitting at my desk at 11pm, manually renaming 200 product photos one by one. Right click, rename, type, enter. Repeat. My wrist hurt and I still had 150 to go.
A coworker walked by, looked at my screen, and said "you know Python could do that in about 3 seconds, right?"
I didn't know. But I downloaded it that night.
That was 2019. I'd tried learning to code twice before and quit both times. Once with Java. Too many semicolons and `public static void main` everywhere and I had no idea what any of it meant. Once with C++ in college. Barely passed. Python was different. Not because it's easier exactly, but because I could actually read what I wrote.
```python
print("hello")
```
That looks like English. It does what it says. No ceremony, no boilerplate, no `System.out.println` nonsense. I typed it, hit enter, and it worked. I'm not gonna lie, that moment was kinda electric.
What Actually Stuck (And What Didn't)
Here's the thing nobody tells you about learning Python. Most of the stuff people say you need to know in week one, you'll never use. I spent 3 hours on bitwise operators my first weekend. Three hours. I have used `&` exactly zero times in 5 years of writing Python for money.
The stuff that actually mattered, I learned by accident.
Variables came first. The mental model everyone explains wrong: they'll tell you a variable is "a box that holds a value." That's Java thinking. In Python, variables are labels you stick onto objects. The object has a type, not the variable. This sounds pedantic but it'll save you from some genuinely confusing bugs later.
```python
x = [1, 2, 3]
y = x
y.append(4)
print(x) # [1, 2, 3, 4] -- wait what?
```
Both x and y point to the same list. Understanding that early would've saved me about 4 hours of debugging my first week.
Indentation. Python uses whitespace to define blocks instead of curly braces. Honestly I thought this was stupid at first. But after a week of reading other people's code, I realized something: Python code looks the same to everyone. There's exactly one way to indent a given block and if you don't do it, the program crashes with a clear error. Try reading a junior developer's JavaScript sometime and you'll understand why this matters.
```python
if age >= 18:
print("you can vote")
print("go register")
else:
print("too young")
```
Notice the colon after `if` and `else`. Forgetting that colon is probably the single most common error in human history. I still forget it sometimes. The error message says `SyntaxError` and points to the next line, which is confusing because the mistake is actually on the line above. After the third time you'll internalize it.
Then there's functions and the variable scope mess. I wrote a function once that modified a list I passed in, and spent an hour wondering why the original list changed too. Turns out lists are passed by reference in Python. Integers and strings get copied. Lists and dicts don't. This isn't a bug, it's by design, but nobody explained it to me until I stumbled into it the hard way.
```python
def add_item(my_list):
my_list.append("new item")
return my_list
original = ["a", "b"]
result = add_item(original)
print(original) # ['a', 'b', 'new item'] -- surprised?
```
And mutable default arguments. Oh man. If you define `def func(items=[])`, that empty list gets created once when the function is defined, not every time it's called. Multiple calls share the same list. This is the kind of thing that creates bugs you chase for hours. The fix is `def func(items=None)` and then `if items is None: items = []` inside the function. Weird, I know.
The Project That Actually Taught Me Something
Two weeks in, I stopped watching tutorials and built something stupid. Best decision.
I scraped my own Twitter archive. Twitter lets you download all your data as a giant JSON file, and I wanted to know: what time of day do I tweet most? What words do I overuse? Who do I reply to most?
I had no idea what I was doing. Googled "python read json file" and found `json.load()`. Then "python count occurrences in list" and found `collections.Counter`. Then "python plot bar chart" and found `matplotlib`. Each search led to a Stack Overflow answer, each answer led to a copy-paste, each copy-paste broke in some interesting way, and each break taught me something real.
The script was 40 lines. Ugly. No error handling. It crashed if the JSON file was missing. But it worked and I learned more in that afternoon than in the previous two weeks of video courses.
```python
import json
from collections import Counter
with open('tweets.json', 'r') as f:
data = json.load(f)
words = []
for tweet in data:
words.extend(tweet['text'].lower().split())
common = Counter(words).most_common(20)
for word, count in common:
print(f"{word}: {count}")
```
Twenty lines of actual code gave me a word frequency report for my entire Twitter history. I found out I say "honestly" way too much on Twitter too. Ironic, I know.
What I'd Do If I Was Starting Over Tomorrow
Skip the courses. For the first two weeks, at least.
Pick three tiny projects, each building on the last. A script that renames files in a folder. Then one that reads a CSV and does basic math on it. Then something that hits an API and prints the result. I could list a dozen more but you get the idea. That covers file I/O, data structures, loops, conditionals, basic error handling, and external libraries. All in maybe 100 total lines of code.
The official Python tutorial at docs.python.org is genuinely good. Better than most YouTube courses honestly. And learn about `pdb` early. Just put `import pdb` then `pdb.set_trace()` wherever you want to pause. Way better than `print()` debugging once your scripts get past 30 lines.
I guess what I'm saying is you don't learn Python by studying Python. You learn it by having a problem that Python can solve, and then being just stubborn enough to figure out how. The syntax comes along for the ride.
One more thing. Virtual environments. I ignored them for 6 months and my system Python became a dumpster fire of conflicting package versions. `python -m venv myproject` takes 2 seconds. Just do it from day one, trust me.
Also, get comfortable with the terminal. I know GUIs feel safer but the command line is where Python actually lives. You'll need it for installing packages, running scripts, managing environments, and reading those error messages I mentioned. It's not as scary as it looks and after a week you'll wonder why you ever avoided it.