Skip to main content
Python is a high-level, interpreted, object-oriented programming language celebrated for its clean syntax and remarkable readability. Before you write your first FastAPI endpoint or train your first AI model, you need a firm grip on Python’s fundamentals — how variables work, how data is typed, how operators behave, and how execution flows through your programs. This page walks you through every building block you need, with concrete examples and the nuances that trip up beginners.

Python Fundamentals

A few key characteristics distinguish Python from other languages:
  • Python is dynamically typed — you never declare a variable’s type explicitly.
  • Python uses indentation instead of curly braces {} to define code blocks.
  • Everything in Python is an object, including functions and classes.
  • Variables store references to objects, not the objects themselves.
  • Python follows the PEP 8 style guide for consistent, readable code.

Variables

Variables store references to objects in memory. You create them simply by assigning a value.

Multiple Assignment

Python lets you assign several variables in one line:

Variable Swapping

Swapping two values requires no temporary variable in Python:

Object Identity

Two variables can reference the same underlying object:
Use == to compare values and is to compare object identity. Never use is to compare numbers or strings in regular code.

Everything is an Object

In Python, every value — number, string, list, function, class — is a first-class object with a type and a unique identity:

Small Integer Caching

CPython caches integers in the range -5 to 256 as a performance optimisation. This means two variables assigned the same small integer may point to the identical object:

Floating-Point Precision

Floats are stored in binary, so some decimal values cannot be represented exactly:
Use round() or math.isclose() when comparing floating-point numbers to avoid precision surprises.

Data Types

Python provides a rich set of built-in types:

Type Conversion

Convert between types using built-in functions:

Operators

Arithmetic Operators

Comparison Operators

Comparison operators return True or False:

Logical Operators

Truthy and Falsy Values

Every Python object has a truth value. Falsy values include False, None, 0, 0.0, "", [], (), {}, set(). Everything else is truthy.
Because and and or return operands rather than booleans, you can use them for concise default values:

Assignment Operators

Identity and Membership Operators

Bitwise Operators

Bitwise operators work on the binary representation of integers:

Control Flow

if / elif / else

match…case (Python 3.10+)

Python’s structural pattern matching is a powerful alternative to long elif chains:

for Loop

while Loop

break, continue, and pass

for…else and while…else

The else block runs only when the loop completes without hitting a break:

Python Nuances

  • Everything in Python is an object — including functions and classes.
  • Variables store references, not values directly.
  • Use == for value comparison; use is only for identity checks (e.g., x is None).
  • Small integers (-5 to 256) are cached by CPython.
  • Floating-point arithmetic is not always exact — use math.isclose() for comparisons.
  • Indentation defines code blocks — use 4 spaces consistently.
  • Lists are mutable; tuples are immutable.
  • and and or return operands, not only True/False.
  • Truthy/falsy values let you write concise conditionals.