Language cheatsheet
Python 3 Cheatsheet
Run scripts, manage virtual environments and packages, and test and profile Python code.
Running Python
- Check the Python version — Print the interpreter version.
- Run a script — Run a Python file. On Windows, py or python usually replaces python3.
- Run a one-line command — Run a snippet from the command line.
- Open the interactive shell — Start the REPL. Exit with exit() or Ctrl+D.
- Serve the current folder — Start a simple web server on port 8000.
- Pretty-print JSON — Format a JSON file for reading.
Virtual environments
- Create a virtual environment — Create an isolated environment in the .venv folder.
- Activate it — Use the environment's Python and packages in this shell. Once active, python and pip refer to the environment, so the pip commands below use python. On Windows run .venv\Scripts\activate.
- Deactivate it — Leave the virtual environment.
- Upgrade pip — Upgrade pip inside the active virtual environment.
Packages with pip
- Install a package — Install a package from PyPI.
- Install a specific version — Install an exact version instead of the latest one.
- Install from a requirements file — Install everything listed in requirements.txt.
- Upgrade a package — Install the newest version of a package.
- Uninstall a package — Remove a package from the environment.
- List installed packages — Show packages and their versions.
- Show package details — Show a package's version, dependencies and location.
- Find outdated packages — List packages that have newer versions.
- Write a requirements file — Save the installed packages and exact versions.
- Check for broken dependencies — Verify that installed packages have compatible dependencies.
Testing and tools
- Run tests with pytest — Run every test found under the current folder. Needs pytest installed.
- Run one test — Run a single test function and show print output.
- Run unittest tests — Find and run tests using the standard library.
- Debug a script — Run a script under the pdb debugger.
- Time a snippet — Measure how long a statement takes.
- Profile a script — Show which functions take the most time.
Related cheatsheets
- C++ 23 cheatsheet — Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
- C# 14 cheatsheet — Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- Go 1 cheatsheet — The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.