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: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 returnTrue or False:
Logical Operators
Truthy and Falsy Values
Every Python object has a truth value. Falsy values includeFalse, None, 0, 0.0, "", [], (), {}, set(). Everything else is truthy.
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 longelif chains:
for Loop
while Loop
break, continue, and pass
for…else and while…else
Theelse block runs only when the loop completes without hitting a break:
Python Nuances
Key things every beginner should know
Key things every beginner should know
- Everything in Python is an object — including functions and classes.
- Variables store references, not values directly.
- Use
==for value comparison; useisonly for identity checks (e.g.,x is None). - Small integers (
-5to256) 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.
andandorreturn operands, not onlyTrue/False.- Truthy/falsy values let you write concise conditionals.