python5 min read

Python Tutorial: Learn Python Programming from Scratch (2026)

Python Tutorial: Learn Python Programming from Scratch (2026)

Published:  |  Category: Python  |  Reading time: ~15 min
Python Tutorial: Learn Python Programming from Scratch (2026)

I picked up Python back when 2.7 was still the standard, and what hooked me wasn't the hype but how fast I could go from idea to working code. No compile step, no verbose ceremony — just write and run. Over the years I've used it to scrape websites, automate server fleets, build recommendation engines, and ship production APIs. The same language that powers a five-line script can power a distributed ML pipeline, and that versatility is rare.

This tutorial walks through Python the way I wish someone had shown me: start with the syntax quirks that trip up beginners, then immediately put them to work with functions, classes, and the standard library. We'll build a small CLI tool that reads CSV data, processes it, and generates a report — so every concept has a tangible outcome. By the end you'll have a mental model of Python that makes reading any framework or library feel familiar.

Setting Up Your Environment and First Script

I recommend starting with pyenv on macOS or Linux, and the official installer on Windows — just check 'Add Python to PATH' during install. Once you've got Python 3.12+ running, open a terminal and type python. You'll see the REPL prompt. Pressing Ctrl+D exits it. Your first script can be a single print call saved to hello.py, but let's make it do something more interesting: accept a command-line argument.

import sys

name = sys.argv[1] if len(sys.argv) > 1 else "World"
print(f"Hello, {name}!")

# Run: python hello.py Alice

Variables, Types, and the Dynamic Nature of Python

Python is dynamically typed, which means you don't declare types — the interpreter infers them at runtime. This speeds up prototyping but demands discipline on larger projects. Mutable types like lists and dicts get modified in-place, while immutable types like strings and tuples always produce new objects. Understanding this distinction early saves you from the classic 'list-of-lists' bug where every sublist points to the same reference.

a = [0] * 3          # [0, 0, 0]
b = [[0] * 3] * 3     # [[0,0,0], [0,0,0], [0,0,0]]
b[0][0] = 42          # Changes all three rows!

# Correct way:
c = [[0] * 3 for _ in range(3)]
c[0][0] = 42          # Only first row changes

Control Flow and Comprehensions

Conditionals and loops work as you'd expect, but Python's for-loop is a for-each — it iterates over items directly. The range() function generates numeric sequences when you need indices. List comprehensions are the idiomatic Python way to transform sequences, and they're generally faster than manual loops because the iteration happens at C speed inside the interpreter.

numbers = range(1, 11)
evens = [n for n in numbers if n % 2 == 0]
squares = {n: n**2 for n in evens}

print(evens)   # [2, 4, 6, 8, 10]
print(squares) # {2: 4, 4: 16, 6: 36, 8: 64, 10: 100}

Functions, Scope, and First-Class Status

Functions in Python are first-class objects — you can pass them around, assign them to variables, and nest them. Lambda expressions give you anonymous one-liners for simple operations, though anything complex should be a def block for readability. The key scope rule is LEGB: Local, Enclosing, Global, Built-in. Knowing this resolves most surprises around variable shadowing.

def multiplier(factor):
    def multiply(x):
        return x * factor
    return multiply

double = multiplier(2)
triple = multiplier(3)

print(double(5))  # 10
print(triple(5))  # 15

Working with the Standard Library

Python ships with a 'batteries included' philosophy. The standard library covers file I/O, JSON parsing, CSV handling, regex, datetime, math, and HTTP servers. I rarely install third-party packages for basic tasks because stdlib handles most of it. The pathlib module is my go-to for filesystem operations — it's more intuitive than os.path and works cross-platform.

from pathlib import Path
import json

data_path = Path("data")
data_path.mkdir(exist_ok=True)

records = [{"id": 1, "value": "foo"}, {"id": 2, "value": "bar"}]
data_path.joinpath("records.json").write_text(json.dumps(records, indent=2))

Building a Practical CLI Report Tool

Let's tie everything together. We'll read a CSV of sales data, compute per-category totals, and write a summary. This combines file parsing, data structures, and formatted output in roughly 30 lines. It's the kind of script I've written dozens of times in real jobs, and it demonstrates why Python dominates data-processing glue code.

import csv
from collections import defaultdict
from pathlib import Path

def generate_report(csv_path):
    totals = defaultdict(float)
    with open(csv_path, newline='') as f:
        reader = csv.DictReader(f)
        for row in reader:
            totals[row['category']] += float(row['amount'])

    report = Path("report.txt")
    lines = [f"{cat}: ${amt:.2f}" for cat, amt in sorted(totals.items())]
    report.write_text('\n'.join(lines))
    print(f"Report written to {report}")

if __name__ == "__main__":
    generate_report("sales.csv")

Frequently Asked Questions

Do I need to install Python differently on Windows versus Mac?

On Windows, use the official installer and check 'Add Python to PATH'. On Mac, I recommend Homebrew: brew install python. Linux users should use their package manager or pyenv for version control.

What's the fastest way to get comfortable with Python syntax?

Open the REPL (type python) and experiment. Try list comprehensions, dict merges with |, and f-strings. The instant feedback loop is the best teacher.

Should I learn Python 2 or Python 3 in 2026?

Python 2 has been dead since 2020. Every major library, framework, and tool now requires Python 3.8+. Use 3.12 or 3.13.

Is Python slow for production applications?

Python is slower than C++ or Rust for raw computation, but for I/O-bound and glue-code workloads it's fast enough. Critical paths can be offloaded to C extensions (NumPy), Cython, or rewritten in a faster language. Most teams optimize for developer time, not CPU time.

Originally published on Ayodhyyya. Last updated June 1, 2026.