๐Ÿ“ฆ Python Fundamentals
Module 2 ยท Topic 2

Data Types โ€” Complete Guide

Python has rich built-in data types โ€” from numbers and strings to lists, tuples, dictionaries, and sets. Every value in Python is an object with a type.

Theory

Python's Built-in Data Types:

Numeric Types:
| Type | Example | Description |
|------|---------|-------------|
| int | 42, -17, 0 | Integers of unlimited size |
| float | 3.14, -0.001, 2.0 | 64-bit floating point (IEEE 754) |
| complex | 3+4j, 1j | Complex numbers with real and imaginary parts |
| bool | True, False | Boolean (subclass of int: True=1, False=0) |

Sequence Types:
| Type | Example | Mutable? |
|------|---------|----------|
| str | "hello", 'world' | โŒ Immutable |
| list | [1, 2, 3] | โœ… Mutable |
| tuple | (1, 2, 3) | โŒ Immutable |
| range | range(10) | โŒ Immutable |

Mapping & Set Types:
| Type | Example | Description |
|------|---------|-------------|
| dict | {"a": 1, "b": 2} | Key-value pairs, ordered (3.7+) |
| set | {1, 2, 3} | Unordered unique elements |
| frozenset | frozenset({1, 2}) | Immutable set |

Special Types:

  • NoneType โ€” None represents the absence of a value
  • bytes โ€” Immutable sequence of bytes: b"hello"
  • bytearray โ€” Mutable sequence of bytes

Key Insight โ€” Mutable vs Immutable:

  • Immutable (int, float, str, tuple, frozenset): Cannot be changed after creation. Operations create NEW objects.
  • Mutable (list, dict, set, bytearray): Can be modified in place.

This distinction is critical for understanding function arguments, dictionary keys, and shared references.

Syntax
# Check type
type(value)

# Check if instance of a type
isinstance(value, int)
isinstance(value, (int, float))  # Multiple types

# Common type constructors
int(), float(), str(), bool(), list(), tuple(), dict(), set()