Introduction to Python Types

Python uses dynamic typing. Common types include int, float, str, list, and dict.

Variables do not need type declarations — assign a value directly.

In Python, every variable is a reference to an object stored in memory. When you write x = 42, Python allocates an integer object with value 42 and binds the identifier x to that object.

Core Data Types Overview

Numeric Types

  • int: Arbitrary precision integers (e.g., 100, -5). Python 3 automatically handles large numbers without overflow.
  • float: 64-bit double-precision floating-point numbers adhering to IEEE 754 (e.g., 3.14159, 1e-4).
  • bool: Boolean subtype of int representing truth values (True and False).

Sequences and Collections

  • str: Immutable sequence of Unicode characters. Supports slicing: text[0:4] and f-strings: f"Hello {name}".
  • list: Mutable, ordered sequence of heterogeneous elements: numbers = [1, 2, 3].
  • tuple: Immutable ordered sequence: coords = (10.0, 20.0).
  • dict: Key-value hash map providing O(1) average lookup time: user = {"id": 1, "role": "admin"}.

Type Inspection and Conversion

You can inspect an object's runtime type using the built-in type() function, or verify inheritance with isinstance():

value = "1024"
print(type(value))  # <class 'str'>
number = int(value)
print(isinstance(number, int))  # True

Best Practices for Variable Naming

Follow PEP 8 conventions: use snake_case for variables and functions, UPPER_SNAKE_CASE for constants, and choose descriptive names that reveal intent without requiring inline comments.