📚

Overview of Built-in Python Types

Mar 23, 2025

Built-in Types Overview

Main Built-in Types

  • Numerics: Integers, floating-point numbers, complex numbers.
  • Sequences: Lists, tuples, range objects.
  • Mappings: Dictionaries.
  • Classes, Instances, Exceptions.

Collection Classes

  • Mutable Collections: Some can change elements in place; methods return None instead of the instance.

Comparison and String Conversion

  • Comparison: Objects can be compared for equality.
  • Truth Value Testing: Objects evaluated to False include None, False, zero of any numeric type, empty sequences and collections.
  • String Conversion: Use repr() or str().

Truth Value Testing

  • Default truthiness: Objects are True unless defined otherwise with __bool__() returning False or __len__() returning 0.

Boolean Operations

  • Operations:
    • x or y: Returns x if x is True, else y.
    • x and y: Returns x if x is False, else y.
    • not x: Returns True if x is False, else False.

Comparisons

  • Eight Operations: <, <=, >, >=, ==, !=, is, is not.
  • Chaining: Comparisons can be chained, e.g., x < y <= z.

Numeric Types

  • Types: int, float, complex.
  • Precision: Integers unlimited precision, floats typically double precision.
  • Operations: Addition, subtraction, multiplication, division, floor division, modulus.

Bitwise Operations

  • Operations on Integers: Bitwise or, xor, and, shifts left/right, bit inversion.

Sequence Types

  • Types: list, tuple, range.
  • Common Operations: Length, concatenation, repetition, indexing, slicing.
  • Additional Methods for Lists: Sorting, appending, inserting, removing.

Text Sequence Type

  • Type: str (unicode strings).
  • String Manipulation: Methods for case conversion, searching, replacing, splitting, joining.

Set Types

  • Types: set, frozenset.
  • Operations: Union, intersection, difference, symmetric difference.

Mapping Types

  • Type: dict.
  • Dictionary Operations: Access and modify keys/values, iterate over keys/values/items.

Type Annotation Types

  • GenericAlias and Union: Used for type hints in function annotations.

Other Types

  • Modules: Attributes can be accessed and modified.
  • Functions and Methods: Objects that can be called with arguments.
  • Code Objects: Represent compiled Python code.

Special Attributes

  • Attributes: __name__, __module__, __doc__.

Integer String Conversion

  • Limit: Protection against excessive computation by limiting digit length in conversions.