Python Cheat Sheet
Robin te Hofstee4 min readViews6

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
Noneto 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_caseconvention
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 breakexits the loop,continueskips to the next iteration- Be careful with
whileto avoid creating an infinite loop
Functions
- Define functions with
def - Always use
()to call a function - Add
returnto send values back - Create anonymous functions with the
lambdakeyword
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 methodselfrefers to the instance
Exceptions
- When Python encounters an error, it creates an exception
- Use specific exception types when possible
elseruns if no exception occurredfinallyalways 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
inkeyword - Collections may look similar, but each data structure solves specific needs
Comprehensions
- Think of comprehensions as condensed
forloops - Comprehensions are generally faster than equivalent loops
File I/O
- Open files with
open(), ideally inside awithstatement 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
- The official third-party package repository is the Python Package Index (PyPI)
Miscellaneous
| Truthy | Falsy |
|---|---|
-42 | 0 |
3.14 | 0.0 |
"John" | "" |
[1, 2, 3] | [] |
("apple", "banana") | () |
{"key": None} | {} |
None |
Share this post

Written by Robin te Hofstee
Technology enthusiast with a deep love for programming. Sharing practical guides on React, Next.js, Linux and self-hosting.
Related Posts
Setting Up Python Virtual Environments
4
Learn how to set up and manage Python virtual environments for your projects, ensuring isolated and reproducible development.
How to Set Up a Python Web Server
7
A detailed tutorial on setting up a Python web server using Flask and deploying it locally.


