Inkdown
Start writing

Basics

Last updated on April 5, 2026

Python Basics (Engineer Cheat Sheet)

Environment & Tooling

pyenv + venv

  • pyenv is a Python version manager, similar to nvm for Node.js.
  • It helps you install and switch between multiple Python versions.
  • venv creates an isolated environment for one project, so packages from one project do not affect another.

Example:

Bash

pip + requirements.txt

  • pip is Python’s package installer.
  • requirements.txt stores the list of dependencies for a project.
  • This makes it easy for others, or your future self, to install the same packages.

Example:

Bash

Poetry (pyproject.toml)

  • Poetry is a modern dependency and project manager for Python.
  • It handles dependencies, virtual environments, and package configuration in one place.
  • pyproject.toml is the main config file Poetry uses.

Example:

Bash

Python REPL + ipython

  • REPL means Read-Eval-Print Loop.
  • It is a quick interactive shell where you can test Python code.
  • ipython is a better REPL with autocomplete, command history, and nicer output.

Example:

Bash

Syntax — What’s Different from TS/JS

Indentation, no braces

  • Python uses indentation to define code blocks.
  • It does not use {} like JavaScript or TypeScript.
  • A colon : starts a new block.

Example:

Python

Type hints

  • Type hints let you describe what type a variable or function argument should be.
  • They make code easier to understand and improve editor support.
  • They are not strictly enforced by Python at runtime by default.

Example:

Python

f-strings

  • f-strings are the cleanest way to insert variables into strings.
  • Put f before the string, then use {} inside it.

Example:

Python

None vs null / undefined

  • Python has None to represent “no value”.
  • There is no separate null or undefined.
  • The correct check is is None, not == None.

Example:

Python

Truthiness

  • In Python, some values are treated as False in conditions.
  • Common falsy values are: None, 0, "", [], {}, set().
  • Most other values are truthy.

Example:

Python

Walrus operator :=

  • The walrus operator assigns a value and uses it in the same expression.
  • It is useful when you want to avoid doing the same work twice.

Example:

Python

Collections & Comprehensions

list / dict / set / tuple

  • list: ordered, mutable collection
  • dict: key-value mapping
  • set: unordered collection of unique values
  • tuple: ordered, immutable collection

Example:

Python

List comprehensions

  • A compact way to create lists from loops.
  • Very common and very Pythonic.

Example:

Python

With condition:

Python

Dict comprehensions

  • Similar to list comprehensions, but for dictionaries.
  • Useful when transforming data into key-value form.

Example:

Python

Unpacking *args and **kwargs

  • *args collects extra positional arguments into a tuple.
  • **kwargs collects extra keyword arguments into a dictionary.
  • Useful when you do not know how many arguments will be passed.

Example:

Python

Generators (yield)

  • A generator returns values one at a time instead of all at once.
  • This makes it memory-efficient.
  • It is useful for large data, streams, and lazy processing.

Example:

Python

Functions & OOP

Default args, keyword args

  • Default arguments give a parameter a fallback value.
  • Keyword arguments let you pass values by name.
  • This makes function calls clearer and more flexible.

Example:

Python

Keyword-only arguments

  • You can force some arguments to be passed by name using *.
  • This makes calls more explicit and avoids confusion.

Example:

Python

Decorators

  • A decorator wraps a function and changes or extends its behavior.
  • Common uses: logging, authentication, caching, route handling.

Example:

Python

dataclass

  • A dataclass is a simpler way to write classes that mostly store data.
  • It automatically creates methods like __init__ and __repr__.

Example:

Python

Dunder methods

  • Dunder means “double underscore”.
  • These special methods let your class behave in custom ways.
  • Examples: __init__, __str__, __len__, __getitem__.

Example:

Python

Context managers (with)

  • A context manager handles setup and cleanup automatically.
  • It is commonly used for files, database connections, and locks.
  • Even if an error happens, cleanup still runs.

Example:

Python

Error Handling & Modules

try / except / finally

  • try contains code that may fail.
  • except handles the error.
  • finally always runs, whether there was an error or not.

Example:

Python

Custom exceptions

  • You can create your own exception types for clearer error handling.
  • This makes bigger applications easier to maintain.

Example:

Python

Imports

  • import lets you use code from another file or module.
  • This helps organize code into reusable parts.

Example:

Python

__init__.py

  • __init__.py tells Python that a folder should behave like a package.
  • It can also run package setup code or expose selected imports.

Example folder structure:

Python

Import example:

Python

Mental Model

Python is interpreted

  • Python code is executed by the Python interpreter.
  • You do not usually compile it manually like C or C++.

Python is dynamically typed

  • You do not need to declare variable types beforehand.
  • A variable can point to different types of values over time.

Example:

Python

Everything is an object

  • Numbers, strings, functions, classes, everything in Python is an object.
  • That is why Python feels very consistent.

Example:

Python

Clean code matters a lot in Python

  • Python is built around readability.
  • Simple, clear code is preferred over clever but confusing code.

What to Learn Next

async / await

  • Used for asynchronous programming.
  • Important for APIs, web servers, and I/O-heavy systems.

FastAPI

  • A modern Python web framework.
  • Great for building APIs quickly.

Pydantic

  • Used for validation and structured data handling.
  • Common in FastAPI apps.

File handling and streams

  • Important for working with files, uploads, logs, and data pipelines.

Concurrency

  • Learn the difference between threads, processes, and async tasks.

Packaging and deployment

  • Important for shipping real Python applications.

Shared via Inkdown