Page cover
Readability counts -
simple is better than complex,
explicit is better than implicit,
beautiful is better than ugly.
📋

Python Cheatsheet

Python

Essential Python syntax, data structures, comprehensions, and common patterns for quick reference

View:

Basic Syntax

Variables

Python uses dynamic typing - no type declaration needed

# Variables (dynamically typed)
x = 5
name = "John"
is_active = True

Variables

Why dynamic typing?

Python prioritizes developer productivity and readability. Dynamic typing means less boilerplate code and faster prototyping, though you can add type hints for larger projects.

Best practice?

Use descriptive variable names (snake_case), and consider type hints for function signatures in production code.

Common pitfall?

Variables can change type unexpectedly. Use isinstance() to check types when needed.

String Formatting

f-strings (Python 3.6+) or str.format() method

name = "Alice"
age = 25
print(f"Hello, {name}. You are {age} years old")
print("Hello, {}. You are {} years old".format(name, age))

String Formatting

Which to use?

f-strings are preferred for Python 3.6+. They're more readable, faster, and allow expressions inside braces like {age + 1}.

When .format()?

Use .format() when you need to reuse a template string or when supporting Python 3.5 or earlier.

Performance?

f-strings are compiled at parse time, making them faster than .format() or % formatting.

Conditional Statements

Use indentation to define code blocks

if age >= 18:
    print("Adult")
elif age >= 13:
    print("Teenager")
else:
    print("Child")

Conditional Statements

Why indentation?

Python uses indentation (4 spaces recommended) instead of braces to define code blocks. This enforces readable code structure.

Ternary option?

For simple conditions: result = 'Adult' if age >= 18 else 'Minor'

Match statement?

Python 3.10+ introduced match-case for pattern matching, similar to switch statements in other languages.

Data Structures

Lists

Ordered, mutable collections

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
fruits[0]  # "apple"
fruits[-1]  # "orange" (last item)
fruits[1:3]  # ["banana", "cherry"] (slice)

Lists

When to use?

Use lists when you need an ordered collection that may change size, like a shopping cart or task queue.

Performance?

O(1) for append/pop at end, O(n) for insert/remove at beginning. Use collections.deque for frequent operations at both ends.

vs Array?

Python lists are dynamic arrays. For numeric operations, use numpy arrays for better performance.

Dictionaries

Key-value pairs, like objects in JavaScript

person = {"name": "John", "age": 30}
person["city"] = "New York"
person.get("name")  # "John"
person.keys()  # dict_keys(['name', 'age', 'city'])

Dictionaries

When to use?

Perfect for lookups, caching, and representing structured data like JSON. O(1) average lookup time.

get() vs []?

Use .get('key', default) to avoid KeyError exceptions. Returns None (or default) if key doesn't exist.

Ordering?

Python 3.7+ guarantees insertion order. Use OrderedDict only if you need explicit ordering behavior.

Sets

Unordered collection of unique elements

unique_nums = {1, 2, 3, 3, 4}  # {1, 2, 3, 4}
unique_nums.add(5)
unique_nums.remove(1)

Sets

When to use?

Ideal for removing duplicates, membership testing, and set operations (union, intersection, difference).

Performance?

O(1) for add, remove, and 'in' checks. Much faster than lists for membership testing.

Limitation?

Elements must be hashable (immutable). Can't store lists or dicts, but can store tuples.

Tuples

Immutable ordered collections

coordinates = (10, 20)
x, y = coordinates  # Unpacking
immutable = (1, 2, 3)  # Cannot be modified

Tuples

Why immutable?

Tuples are hashable (can be dict keys), slightly faster than lists, and signal intent that data shouldn't change.

When to use?

For fixed collections like coordinates, RGB colors, or returning multiple values from functions.

Named alternative?

Use namedtuple or dataclasses for more readable code: Point = namedtuple('Point', ['x', 'y'])

List Comprehensions

Basic Comprehension

Create lists in a single line

squares = [x**2 for x in range(10)]
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Basic Comprehension

Why use it?

More Pythonic, often faster than equivalent for loops, and produces more readable one-liners for simple transformations.

When to avoid?

If logic is complex or spans multiple lines, use a regular for loop for clarity. Readability counts!

Generator alternative?

Use (x**2 for x in range(10)) for memory efficiency with large datasets - evaluates lazily.

With Condition

Filter elements with if condition

even_squares = [x**2 for x in range(10) if x % 2 == 0]
# [0, 4, 16, 36, 64]

With Condition

Multiple conditions?

Chain with 'and/or': [x for x in range(20) if x % 2 == 0 and x > 5]

If-else?

For transformation: [x**2 if x % 2 == 0 else x for x in range(10)] - note the different position!

Equivalent?

Same as filter(lambda x: x % 2 == 0, range(10)) but comprehensions are more Pythonic.

Dict Comprehension

Create dictionaries efficiently

square_dict = {x: x**2 for x in range(5)}
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Dict Comprehension

Common use?

Transform or filter existing dicts: {k: v.upper() for k, v in data.items() if v}

From two lists?

Use zip: dict(zip(keys, values)) or {k: v for k, v in zip(keys, values)}

Set comprehension?

Also available: {x % 3 for x in range(10)} creates a set with unique values.

Functions

Basic Function

Functions with default parameters

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

greet("Alice")  # "Hello, Alice!"
greet("Bob", "Hi")  # "Hi, Bob!"

Basic Function

*args, **kwargs?

*args captures positional args as tuple, **kwargs captures keyword args as dict. Useful for flexible function signatures.

Type hints?

def greet(name: str, greeting: str = 'Hello') -> str: - helps IDEs and type checkers, but not enforced at runtime.

Mutable defaults?

Never use mutable default args like def f(items=[]). Use None instead: def f(items=None): items = items or []

Lambda Functions

Anonymous functions for simple operations

square = lambda x: x**2
numbers = [1, 2, 3, 4]
squared = list(map(square, numbers))  # [1, 4, 9, 16]

Lambda Functions

When to use?

Short, throwaway functions - especially as arguments to sorted(), map(), filter(). Keep them simple and readable.

Limitations?

Single expression only, no statements. For anything complex, use regular def functions.

Better alternatives?

Often list comprehensions are cleaner: [x**2 for x in numbers] instead of list(map(lambda x: x**2, numbers))

Decorators

Modify function behavior without changing its code

def uppercase_decorator(func):
    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        return result.upper()
    return wrapper

@uppercase_decorator
def greet(name):
    return f"hello, {name}"

greet("alice")  # "HELLO, ALICE"

Decorators

Common uses?

Logging, timing, authentication, caching (@functools.lru_cache), and retry logic. Very powerful for cross-cutting concerns.

@functools.wraps?

Always use @wraps(func) in wrapper to preserve the original function's name and docstring.

Built-in decorators?

@property, @staticmethod, @classmethod, @abstractmethod, @dataclass are commonly used built-in decorators.

Classes

Basic Class

__init__ is the constructor method

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        return f"Hi, I'm {self.name}"

person = Person("John", 30)
print(person.greet())  # "Hi, I'm John"

Basic Class

Why self?

self refers to the instance. Unlike other languages, Python requires explicit self as first parameter in instance methods.

Dataclass alternative?

For data containers, use @dataclass: @dataclass class Person: name: str; age: int - auto-generates __init__, __repr__, etc.

Private attributes?

Use _name for 'protected' (convention) or __name for name mangling (becomes _ClassName__name). Python trusts developers.

Inheritance

Inherit from parent class using super()

class Student(Person):
    def __init__(self, name, age, student_id):
        super().__init__(name, age)
        self.student_id = student_id

    def study(self):
        return f"{self.name} is studying"

Inheritance

Multiple inheritance?

Python supports it: class A(B, C). Method Resolution Order (MRO) determines which method is called. Use sparingly.

Composition vs inheritance?

Prefer composition ('has-a') over inheritance ('is-a') for flexibility. Student HAS a schedule vs Student IS a Person.

Abstract classes?

Use ABC: from abc import ABC, abstractmethod. Forces subclasses to implement certain methods.

Visual Concepts

Python Data Types

Overview of Python's built-in data types

Exception Handling Flow

How try/except blocks work in Python

List vs Tuple vs Set

Comparison of Python collection types

List [] Ordered Mutable Duplicates OK Indexable [1, 2, 2, 3] Tuple () Ordered Immutable Duplicates OK Indexable (1, 2, 2, 3) Set {} Unordered Mutable No Duplicates Not Indexable {1, 2, 3}

Find this helpful?

Check out more cheatsheets and learning resources, or get in touch for personalized training.