Python Tutorial

Learn Python from scratch — syntax, data structures, functions, and a real command-line project you can run today.

2 min read 365 words Updated Mar 4, 2026

Python is a readable, batteries-included language used for scripting, web backends, data science, and automation. This tutorial gets you from zero to a working program.

Why Python

  • Readable by design — indentation is part of the syntax, so code looks consistent.
  • Huge standard library — file I/O, HTTP, JSON, and dates ship in the box.
  • Great ecosystempip install gives you Django, FastAPI, Pandas, and more.

Rule of thumb: if you can describe a task in plain English, you can usually write it in Python in a few lines.

Installing Python

# Check your version
python3 --version

# Create and activate a virtual environment (recommended)
python3 -m venv .venv
source .venv/bin/activate      # Windows: .venv\Scripts\activate

Your first program

def greet(name: str) -> str:
    return f"Hello, {name}!"

if __name__ == "__main__":
    print(greet("world"))

Run it with python3 main.py. The if __name__ == "__main__" guard means the code only runs when the file is executed directly, not when imported.

Variables and types

Python is dynamically typed — you don’t declare types, but type hints (like name: str) make code clearer and let editors catch bugs.

count = 10          # int
price = 9.99        # float
name = "Ada"        # str
items = [1, 2, 3]   # list
config = {"debug": True}  # dict
Type Example Mutable?
int 42 no
str "hi" no
list [1, 2] yes
dict {"a": 1} yes
tuple (1, 2) no

Control flow

for i in range(5):
    if i % 2 == 0:
        print(f"{i} is even")
    else:
        print(f"{i} is odd")

Functions and comprehensions

numbers = [1, 2, 3, 4, 5]
squares = [n * n for n in numbers if n % 2 == 1]
# -> [1, 9, 25]

def total(xs: list[int]) -> int:
    return sum(xs)

Mini project: a word counter

import sys
from collections import Counter

def main(path: str) -> None:
    text = open(path, encoding="utf-8").read().lower().split()
    counts = Counter(text)
    for word, n in counts.most_common(10):
        print(f"{word:>12} {n}")

if __name__ == "__main__":
    main(sys.argv[1])

Save as wordcount.py and run python3 wordcount.py somefile.txt. You now have a reusable CLI tool.

Where to go next

  • Learn the standard library: pathlib, datetime, json, re.
  • Explore packaging with pyproject.toml.
  • Build a web API with FastAPI or a data notebook with Pandas.