πŸ“‹ Cheatsheet in Cheatsheets

Python Cheatsheet

The Python 3 syntax, built-ins and commands you reach for most β€” f-strings, comprehensions, slicing, venv and pip β€” on one scannable page.

Updated June 12, 2026

πŸ“‹ On this page

Overview

This Python cheatsheet collects the syntax, built-ins and commands you reach for most often, in one scannable page. It targets Python 3 and assumes you already write a little code β€” the goal is fast recall, not a tutorial.

Bookmark it, or grab the PDF, and keep it next to your editor.

Quick Reference

TaskSnippet
F-stringf"{name} is {age}"
List comprehension[x*x for x in nums if x > 0]
Dict comprehension{k: v for k, v in pairs}
Ternarya if cond else b
Enumeratefor i, v in enumerate(seq):
Zipfor a, b in zip(xs, ys):
Open filewith open(path) as f:
Default dict getd.get(key, fallback)

Syntax Examples

Functions, including default and keyword-only arguments:

def greet(name, *, greeting="Hello"):
    return f"{greeting}, {name}!"

greet("Ada")                 # 'Hello, Ada!'
greet("Ada", greeting="Hi")  # 'Hi, Ada!'

Slicing works on any sequence β€” seq[start:stop:step]:

nums = [0, 1, 2, 3, 4, 5]
nums[1:4]    # [1, 2, 3]
nums[::2]    # [0, 2, 4]
nums[::-1]   # [5, 4, 3, 2, 1, 0]  (reverse)

Unpacking and the walrus operator:

first, *rest = [1, 2, 3, 4]   # first=1, rest=[2, 3, 4]
if (n := len(rest)) > 2:
    print(f"{n} items left")

Common Commands

Day-to-day CLI for environments and packages:

python -m venv .venv            # create a virtual environment
source .venv/bin/activate       # activate (macOS/Linux)
.venv\Scripts\activate          # activate (Windows)

pip install requests            # install a package
pip freeze > requirements.txt   # snapshot deps
pip install -r requirements.txt # restore deps

python -m pytest                # run tests
python -m http.server 8000      # quick static server

Best Practices

  • Prefer f-strings over % and .format() for readability.
  • Use with for files, locks and connections so they always close.
  • Type-hint public functions; run mypy or pyright in CI.
  • Keep one virtual environment per project; never pip install globally.
  • Reach for the standard library first β€” collections, itertools, pathlib, dataclasses.
  • Format with black and lint with ruff so style is never a debate.

Frequently asked questions

Is this Python cheatsheet for Python 2 or Python 3?

Python 3. Python 2 reached end of life in 2020, so every snippet here targets modern Python 3 syntax.

What is the difference between a list and a tuple in Python?

A list is mutable (you can change it after creation) and uses square brackets; a tuple is immutable and uses parentheses. Use a tuple for fixed collections and as dictionary keys.

How do I create a virtual environment in Python?

Run 'python -m venv .venv', then activate it with 'source .venv/bin/activate' on macOS/Linux or '.venv\\Scripts\\activate' on Windows.

What is an f-string in Python?

An f-string is a string literal prefixed with 'f' that lets you embed expressions inside braces, like f"{name}". It is the fastest and most readable way to format strings in Python 3.6+.

πŸ“¬ Weekly Newsletter

Stay ahead of the curve

Get the best programming tutorials, data analytics tips, and tool reviews delivered to your inbox every week.

No spam. Unsubscribe anytime.