Robin.te.Hofstee

Python Cheat Sheet

Robin te Hofstee4 min readViews6
Python Cheat Sheet

A comprehensive Python cheat sheet covering basic syntax, data types, and common modules.

A condensed overview of the Python programming language: syntax, data types, variables, strings, control flow, functions, classes, errors, I/O, and more.

Based on the Python Cheat Sheet from Real Python.

Getting Started

  • Always add a space after the #
  • Use comments to explain the "why" of your code

Data Types

  • Python is dynamically typed
  • Use None to represent missing or optional values
  • Use type() to check an object's type
  • Check for a specific type with isinstance()
  • issubclass() checks if a class is a subclass

Variables & Assignment

  • Variables are created when first assigned
  • Use descriptive variable names
  • Follow the snake_case convention

Strings

  • Use double quotes for strings
  • Use "\n" to create a line break in a string
  • To write a backslash in a normal string, write "\\"

Numbers & Math

  • Use +, -, *, and / for basic arithmetic
  • // is floor division, % is modulo, and ** is exponentiation
  • / always returns a float

Conditionals

  • Python uses indentation for code blocks
  • Use 4 spaces per indentation level

Loops

  • range(5) generates 0 through 4
  • Use enumerate() to get index and value
  • break exits the loop, continue skips to the next iteration
  • Be careful with while to avoid creating an infinite loop

Functions

  • Define functions with def
  • Always use () to call a function
  • Add return to send values back
  • Create anonymous functions with the lambda keyword

Classes

  • Classes are blueprints for objects
  • You can create multiple instances of one class
  • Classes commonly encapsulate data
  • Inside a class, you provide methods for interacting with the data
  • .__init__() is the constructor method
  • self refers to the instance

Exceptions

  • When Python encounters an error, it creates an exception
  • Use specific exception types when possible
  • else runs if no exception occurred
  • finally always runs, even after errors

Collections

  • A collection is any container data structure that stores multiple items
  • If an object is a collection, you can loop through it
  • Strings are collections, too
  • Use len() to get the size of a collection
  • Check if an item is in a collection with the in keyword
  • Collections may look similar, but each data structure solves specific needs

Comprehensions

  • Think of comprehensions as condensed for loops
  • Comprehensions are generally faster than equivalent loops

File I/O

  • Open files with open(), ideally inside a with statement so they close automatically
  • Use "r" to read, "w" to write, and "a" to append

Imports & Modules

  • Prefer explicit imports over import *
  • Use aliases for long module names
  • Group imports: standard library, third-party libraries, user-defined modules

Virtual Environments

  • Virtual environments are often called "venv"
  • Use venvs to isolate project packages from the system-wide Python packages

Packages

Miscellaneous

TruthyFalsy
-420
3.140.0
"John"""
[1, 2, 3][]
("apple", "banana")()
{"key": None}{}
None
Share this post
Robin te Hofstee

Written by Robin te Hofstee

Technology enthusiast with a deep love for programming. Sharing practical guides on React, Next.js, Linux and self-hosting.