Notifications

No notifications

/Phase 1

Introduction to Python

Welcome to Python 🐍

Python is a high-level, interpreted programming language known for its clean syntax and readability. Created by Guido van Rossum in 1991, it's one of the most popular languages in the world.

Why Python?

FeatureDescription
Easy to LearnClean, English-like syntax with enforced indentation
InterpretedNo compilation step — run code instantly
Dynamically TypedNo need to declare variable types
Huge Ecosystem400,000+ packages on PyPI
VersatileWeb, AI/ML, data science, automation, scripting

Installing Python

1. Download from [python.org](https://python.org) 2. Check the "Add Python to PATH" checkbox during installation 3. Verify: python --version

Your First Program

print("Hello, World!")

Key Concepts

ConceptExample
print()print("Hello") — output to console
input()name = input("Enter name: ") — read user input
Comments# This is a comment
IndentationPython uses 4 spaces (not braces) to define blocks

Comments in Python

# Single-line comment
"""
Multi-line comment
(technically a docstring)
"""

> Important: Indentation is not optional in Python. It defines the structure of your code. Mixing tabs and spaces causes errors.

On this page

Detailed Theory

Python is the friendliest programming language to learn first — readable enough to mistake for English, powerful enough to run NASA missions, Instagram, and most AI on the planet. If you can write a shopping list, you can write Python.

What Python Actually Is

Python is an interpreted, dynamically-typed, multi-paradigm language. In plain English:

  • Interpreted — you don't compile to a binary; the python interpreter reads your code and runs it.
  • Dynamically typed — you don't declare types; Python figures them out at runtime.
  • Multi-paradigm — procedural, object-oriented, or functional, your call.

Your First Program

print("Hello, world!")

One line. No main, no semicolons, no curly braces. The interpreter reads top-to-bottom and runs each statement.

How Python Actually Runs Your Code

.py source  →  bytecode (.pyc)  →  Python Virtual Machine  →  output

First run, Python compiles your file to bytecode (cached in __pycache__). The PVM executes that bytecode. You don't think about this day-to-day, but it's why Python is portable: the same .py file runs anywhere with a Python interpreter.

Setting Up Without Pain

1. Install Python 3.12+ from python.org or via the Microsoft Store / Homebrew. 2. Verify: python --version (or python3). 3. Use VS Code with the Python extension. It picks up your interpreter automatically. 4. Always work inside a virtual environment: python -m venv .venv then .venv\Scripts\activate (Windows) or source .venv/bin/activate (mac/linux). Keeps project deps isolated.

The REPL — Your Sandbox

Type python in a terminal and you get an interactive prompt. Type 2 + 2, hit Enter, see 4. Perfect for tiny experiments. Press Ctrl+D / Ctrl+Z to exit.

For anything bigger, use Jupyter (pip install notebook) — same idea, but cells you can re-run.

Indentation Is Syntax

Python uses whitespace to mark blocks. There are no { }. Four spaces, every time:

if age >= 18:
    print("adult")    # 4 spaces in
else:
    print("minor")

Mix tabs and spaces and you'll get IndentationError. Configure your editor to insert 4 spaces on Tab.

Beginner Mistakes to Skip

1. print x instead of print(x). Python 3 made print a function. Always use parentheses. 2. Mixing tabs and spaces. Pick spaces, configure once, forget about it. 3. Forgetting the colon at the end of if / for / def / class lines. 4. Running with the system Python. Always use a virtualenv — prevents "it works on my laptop" bugs. 5. Naming files random.py or json.py. Shadows the standard library, breaks imports. 6. Ignoring the error message. Python tracebacks are descriptive — read the *last* line first; that's the actual error.

Intermediate: Comments, Docstrings & PEP 8

# single-line comment
"""
Multi-line string. When placed as the first statement of a module/function/class
it becomes a docstring — readable via help() and tooling.
"""

PEP 8 is Python's style guide:

  • 4-space indentation, no tabs.
  • snake_case for variables and functions; PascalCase for classes; UPPER_SNAKE for constants.
  • Lines ≤ 88 chars (modern teams use [Black](https://black.readthedocs.io/) which auto-formats).
  • Blank line between functions; two between top-level definitions.
Learn it once; tools like Black + Ruff enforce it for you.

Intermediate: Reading Errors

A traceback ends with the actual exception. Train yourself to read bottom-up:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(items[5])
IndexError: list index out of range

Last line = the error class + message. Line above = where it happened. 90% of debugging is reading this carefully.

Intermediate: if __name__ == '__main__'

def main():
    print("running directly")

if __name__ == '__main__': main()

This lets a file work both as a script (python file.py) and as an importable module (from file import main). Standard pattern in every Python project.

Advanced: Python 2 vs 3 (and Why We Ignore 2)

Python 2 reached end-of-life in January 2020. Don't write new Python 2 code. Differences you'll occasionally see in old Stack Overflow answers:

  • print x (statement, Py2) vs print(x) (function, Py3).
  • 5 / 2 returns 2 in Py2, 2.5 in Py3.
  • Strings are bytes in Py2, Unicode in Py3.

Advanced: CPython, PyPy & Other Implementations

  • CPython — the reference implementation in C. What python usually means.
  • PyPy — a JIT-compiled Python; sometimes 5–10× faster on pure-Python loops.
  • MicroPython — stripped-down Python for microcontrollers.
  • Mojo / Cython / Numba — paths to native-speed Python.
For learning and 99% of work, CPython is what you want. Performance issues? Profile first; reach for NumPy / Cython / PyPy only when justified.

Advanced: The Python Ecosystem in One Map

  • Web — Django, Flask, FastAPI.
  • Data — NumPy, Pandas, Polars, DuckDB.
  • ML / AI — scikit-learn, PyTorch, TensorFlow, transformers.
  • Automation / scripting — requests, BeautifulSoup, playwright, click/typer.
  • Toolingpip, venv, uv, poetry, pytest, black, ruff, mypy.
Every library has a one-line install: pip install . Once you've used it, you're plugged into the largest open-source ecosystem alive.

Practice Path

1. Install Python 3.12, verify with python --version, create a virtualenv, activate it. 2. Write a one-file program that prints Hello, , takes input with input(), and runs from the command line. 3. Force an error on purpose (int("abc")); read the traceback bottom-up and explain it. 4. Add an if __name__ == '__main__': block and import the file from a second script to confirm it doesn't auto-run.