Warming up the neural circuits...
By the end of this chapter you will:
Python is the second most popular programming language in the world — and it's not even close. It powers Instagram's backend, Netflix's recommendation engine, Spotify's data pipelines, and most of the AI/ML research happening today. It's also the language people reach for when they need to automate a boring task at 2am.
Why? Because Python reads like English, runs almost anywhere, and has a library for almost everything. You can write a script in 10 lines that would take 50 in Java or C++. That speed — both in writing and running — is why Python has won.
But speed has a cost: Python is slower than compiled languages like C or Rust. For most applications, that doesn't matter. When it does, Python lets you drop into C for just the slow parts. That's the pragmatic deal: write the easy stuff in Python, optimize the hard stuff later.
When people say "Python," they usually mean three different things at once. Let's separate them:
1. The language — Python is a set of rules: syntax (how you write), semantics (what it means), and a standard library (built-in tools). The language itself is just a specification — it doesn't run anything.
2. The interpreter — CPython is the program that reads your Python code and executes it. It's written in C, hence the name. When you install Python from python.org, you're installing CPython. There are other interpreters (PyPy, MicroPython, Jython), but CPython is the default and what we'll use.
3. The ecosystem — PyPI (the Python Package ) is a repository of 500,000+ third-party libraries. When you pip install requests, you're pulling code from PyPI. The ecosystem is what makes Python powerful: someone has probably already solved your problem.
Mental model: Think of Python like a recipe language. The language is the vocabulary and grammar. The interpreter is the chef who reads the recipe and cooks. PyPI is the pantry full of pre-made ingredients.
Python is an interpreted language. That means there's no separate "compile" step like in C or Java. You write code in a .py file, and the interpreter runs it line by line.
Here's what happens when you run python hello.py:
.pyc files (you've probably seen __pycache__ folders).You don't need to understand all three steps yet. The key insight: Python code is portable. A .py file runs the same on Windows, macOS, and Linux because the bytecode is platform-independent.
Real-world example:
# hello.py
print("Hello, world!")Run it:
$ python hello.py
Hello, world!That's it. No compilation, no linking, no build system. Just run the file.
Python dominates five domains. Click each domain to see how Python is used in production:
Python web frameworks handle millions of requests per second. Instagram's backend is one of the largest Django deployments in the world. Spotify uses Python for its layer. FastAPI is the modern choice for high-performance APIs with automatic documentation.
Why Python won web: Rapid development, excellent support, and a massive ecosystem of and libraries.
Most data scientists use Python. pandas handles data manipulation, numpy provides fast numerical operations, and matplotlib/seaborn create visualizations. Netflix uses Python for its recommendation engine and A/B testing analysis.
Why Python won data: The ecosystem is unmatched — pandas, numpy, and scikit-learn a complete data toolkit.
Most AI research papers ship Python code. OpenAI's API clients are in Python. PyTorch and TensorFlow are the dominant frameworks for training neural networks. scikit-learn handles traditional machine learning.
Why Python won ML/AI: First-class support for numerical computing, automatic differentiation, and GPU acceleration.
If you can describe a task in English, you can probably automate it in Python. System administrators use Python for server management. QA teams automate testing. DevOps engineers build deployment pipelines.
Why Python won automation: Simple syntax, excellent standard library, and cross-platform compatibility.
Researchers in biology, physics, and finance use Python for simulations and data analysis. CERN uses Python for analyzing particle physics data. NASA uses Python for scientific computing.
Why Python won science: Readable code for complex algorithms, extensive math libraries, and easy integration with C/Fortran for performance-critical sections.
Why Python won these domains: It's not the fastest language, but it's the fastest to develop in. For most applications, developer time is more expensive than CPU time. Python lets you ship features quickly, then optimize later if needed.
Python excels when:
| Mistake | Why it's wrong | Fix |
|---|---|---|
python command not found | Python isn't installed, or it's not in your PATH | Install from python.org and check "Add Python to PATH" on Windows, or use python3 on macOS/Linux |
SyntaxError: invalid syntax on print "hello" | You're using Python 2 syntax. Python 2 reached end-of-life in 2020. | Use print("hello") with parentheses — Python 3 syntax |
ModuleNotFoundError: No module named 'requests' | The library isn't installed in your current environment | Run pip install requests (or pip3 on macOS/Linux) |
| Code works on my machine but not on the server | Different Python versions or missing dependencies | Use virtual environments (covered in L1) and pin dependencies with requirements.txt |
IndentationError: unexpected indent | Python uses indentation to define code blocks. Mixed tabs and spaces break this. | Configure your editor to use 4 spaces per indent level, never tabs |
Performance: Python is 10–100x slower than C for CPU-bound tasks. For most applications (web APIs, data processing, automation), this doesn't matter — you're waiting on network I/O or disk I/O, not CPU. When CPU is the bottleneck, use numpy (which is written in C) or drop into a C extension.
Security: Python itself is secure, but third-party packages can have vulnerabilities. Use pip-audit to scan your dependencies, and keep packages updated.
Deployment: Python apps are typically deployed as source code (not compiled binaries). Use to package your app with its dependencies, or use a Platform-as-a-Service like Heroku or Railway.
python --version (or python3 --version). What version do you see? If you get an error, what does it say?test.py with print("Hello, world!") and run it with python test.py. What happens?print("Hello, world!")python -c "print(2 + 2)". What does the -c flag do?python -c "print(2 + 2)"Beginner:
"What's the difference between an interpreted language and a compiled language?"
Interpreted languages (like Python) are executed line by line by an interpreter at runtime. Compiled languages (like C) are translated into machine code before running. Interpreted languages are more portable but slower; compiled languages are faster but platform-specific.
"What is PyPI?"
PyPI (the Python Package Index) is a repository of third-party Python libraries. You install packages from PyPI using
pip install <package>. It's like an app store for Python code.
Senior:
"Explain the three steps CPython takes when running a .py file."
(1) Parse the source code into an abstract syntax tree (AST). (2) Compile the AST into bytecode (stored in
.pycfiles). (3) Execute the bytecode on the Python Virtual Machine (PVM). The bytecode is platform-independent, which is why Python code is portable.
"When would you choose Python over a compiled language like Rust? When would you choose Rust over Python?"
Choose Python when development speed matters more than execution speed (web APIs, data analysis, automation, prototyping). Choose Rust when execution speed is critical (game engines, operating systems, high-frequency trading) or when you need fine-grained memory control. Python is also better when you need a large ecosystem of libraries; Rust's ecosystem is smaller but growing.
I want to…
├── Run Python code → python file.py (or python3 on macOS/Linux)
├── Install a library → pip install <package>
├── Check Python version → python --version
├── Run a one-liner → python -c "code here"
└── Find a library → search pypi.orgWhat are the three things people mean when they say 'Python'?
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello, FastAPI!"}
@app.get("/users/{user_id}")
async def get_user(user_id: int):
return {"user_id": user_id, "name": f"User {user_id}"}Use when: Building high-performance APIs with automatic documentation.