Skip to main content
Python is a high-level, interpreted, object-oriented programming language known for its simple syntax and readability.

Python Fundamentals

Some important characteristics of Python:
  • Python is dynamically typed (no need to declare variable types).
  • Python uses indentation instead of braces ({}) to define blocks.
  • Everything in Python is an object.
  • Variables store references to objects.
  • Python follows the PEP 8 style guide.

Variables

Variables store references to objects.

Multiple Assignment

Variable Swapping

Object Identity

Two variables may reference the same object.
Use == to compare values and is to compare object identity.

Everything is an Object

In Python, every value is an object, including numbers, strings, lists, dictionaries, functions, and classes.

Small Integer Caching

Python caches small integers (typically -5 to 256) to improve performance.
Never use is to compare numbers or strings. Use == instead.

Floating-Point Precision

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

Data Types

Numeric

  • int
  • float
  • complex

Boolean

  • bool

Text

  • str

Sequence

  • list
  • tuple
  • range

Mapping

  • dict

Set

  • set
  • frozenset

Binary

  • bytes
  • bytearray

Special

  • None

Type Conversion

Operators

Arithmetic Operators

Arithmetic Operators

Example:
Example:

Comparison Operators

Comparison Operators

Comparison operators compare two values and return either True or False. Example:
Example:

Logical Operators

Logical operators are used to combine or negate conditions. They return either True or False based on the operands. Example:
Logical operators work with truthy and falsy values.

Truthy and Falsy Values

In Python, every object has a truth value. Falsy values include:
  • False
  • None
  • 0, 0.0
  • "" (empty string)
  • []
  • ()
  • {}
  • set()
Everything else is truthy, such as:
  • Non-zero numbers
  • Non-empty strings
  • Non-empty lists, tuples, dictionaries, and sets
Unlike many programming languages, and and or return one of their operands instead of only True or False.
  • and returns the first falsy value, otherwise the last value.
  • or returns the first truthy value, otherwise the last value.
A common use is providing default values.

Assignment Operators

Assignment operators are used to assign values to variables. Some operators also perform an operation before assigning the result back to the variable. Example:

Identity Operators

Membership Operators

Bitwise Operators

Bitwise Operators

Bitwise operators perform operations on the binary representation of integers. They are commonly used in systems programming, networking, embedded programming, and optimization. Example:
Shift Operators
  • << shifts bits to the left, effectively multiplying by 2 for each shift.
  • >> shifts bits to the right, effectively dividing by 2 (for positive integers) for each shift.
Note: Bitwise operators work only with integer values and are less commonly used in everyday Python programming compared to arithmetic and logical operators.

Operator Precedence

Use parentheses to improve readability.

Control Flow

if

if…else

if…elif…else

match…case (Python 3.10+)

Python supports Structural Pattern Matching using match.

for Loop

Looping through a collection:

while Loop

break

Terminates the loop immediately.

continue

Skips the current iteration.

pass

Acts as a placeholder when no code is required.

for…else

The else block executes only if the loop completes without encountering a break.

while…else

Works the same way as for...else.

Python Nuances

Some Python-specific behaviors every beginner should know:
  • Everything in Python is an object.
  • Variables store references to objects, not values.
  • Use == for value comparison and is for identity comparison.
  • Small integers (-5 to 256) are cached.
  • Floating-point arithmetic is not always exact.
  • Indentation defines code blocks.
  • Lists are mutable; tuples are immutable.
  • Functions are first-class objects.
  • and and or return values, not just True or False.
  • Truthy and falsy values simplify conditional expressions.
  • Multiple assignment and unpacking are built into the language.
  • Python emphasizes readability and simplicity (PEP 8).