Python Cheat Sheet

Core Python syntax, control flow, operators, collections, and everyday language patterns.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Basics and running code

Core syntax and invocation patterns.

Multiple assignment

Multiple assignment

pythonANYassignmentbasics
python
x, y = 10, 20

Swap variables

Swap variables

pythonANYassignmenttuple
python
a, b = b, a

Read input

Read input

pythonANYinputbasics
python
name = input("Name: ")

Command-line one-liner

Command-line one-liner

bashANYclipython
bash
python -c "print(sum(range(10)))"

Run a module

Run a module

bashANYclimodules
bash
python -m http.server 8000

Numbers, booleans, and comparisons

Arithmetic and boolean expression patterns.

Integer division

Integer division

pythonANYnumbersoperators
python
quotient = 7 // 3

Modulo

Modulo

pythonANYnumbersoperators
python
remainder = 7 % 3

Exponentiation

Exponentiation

pythonANYnumbersoperators
python
power = 2 ** 8

Chained comparison

Chained comparison

pythonANYcomparisonsoperators
python
is_between = 1 < x < 10

Truthy fallback

Truthy fallback

pythonANYbooleansoperators
python
username = supplied_name or "anonymous"

Ternary expression

Ternary expression

pythonANYconditionalsoperators
python
label = "adult" if age >= 18 else "minor"

Conditionals and loops

Control flow tools from the Python tutorial and language reference.

If / elif / else

If / elif / else

pythonANYifcontrol-flow
python
if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
else:
    grade = "C"

For over a list

For over a list

pythonANYforiteration
python
for name in names:
    print(name)

For with enumerate

For with enumerate

pythonANYenumerateiteration
python
for idx, value in enumerate(items, start=1):
    print(idx, value)

For with zip

For with zip

pythonANYzipiteration
python
for name, score in zip(names, scores):
    print(name, score)

While loop

While loop

pythonANYwhileiteration
python
while queue:
    item = queue.pop(0)
    print(item)

Loop with else

Loop with else

pythonANYfor-elsecontrol-flow
python
for item in items:
    if item == target:
        print("found")
        break
else:
    print("not found")

Match statement

Match statement

pythonANYmatchpattern-matching
python
match status:
    case 200:
        message = "ok"
    case 404:
        message = "not found"
    case _:
        message = "other"

Collections overview

Common core data structures.

List literal

List literal

pythonANYlistcollections
python
items = [1, 2, 3]

Tuple literal

Tuple literal

pythonANYtuplecollections
python
point = (10, 20)

Set literal

Set literal

pythonANYsetcollections
python
tags = {"python", "api", "cli"}

Dictionary literal

Dictionary literal

pythonANYdictcollections
python
user = {"id": 1, "name": "Ada"}

Membership test

Membership test

pythonANYmembershipcollections
python
if "python" in tags:
    print("present")

Destructuring assignment

Destructuring assignment

pythonANYunpackingcollections
python
first, *middle, last = values