In programming, a data type is one of the most fundamental concepts. Variables can store data of different types, and different types can do different things.
Python is completely object-oriented, and not "statically typed". You do not need to declare variables with any particular type, and you can even change their type after they have been set simply by assigning a new value!
Before diving into individual types, it is crucial to understand the overarching map of Python's data structures. Data types are split into two major categories: Primitives (single values) and Collections (containers of multiple values).
┌───────────────────────────────┐
│ PYTHON DATA TYPES │
└───────────────┬───────────────┘
│
┌──────────────────────────┴──────────────────────────┐
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ PRIMITIVE TYPES │ │ COLLECTIONS │
│ (Non-Iterables) │ │ (Iterables) │
└────────┬────────┘ └────────┬────────┘
│ │
├─► int ├─► Sequences (Ordered/Indexed)
├─► float │ ├─► list (Mutable)
├─► complex │ ├─► tuple (Immutable)
├─► bool │ ├─► str (Immutable)
└─► NoneType │ ├─► range (Immutable)
│ ├─► bytes (Immutable)
│ ├─► bytearray (Mutable)
│ └─► memoryview
│
└─► Non-Sequences (Unordered)
├─► dict (Mappings)
├─► set (Unique Sets)
└─► frozenset (Immutable Sets)
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. Input:
b = 20 # int
y = 35656222554887711
z = -3255522Float is a number, positive or negative, containing one or more decimals. Input:
c = 20.5 # float
y = -35.59
z = 35e3 # 35 * 10^3Complex numbers are written with a "j" as the imaginary part. Input:
d = 1j # complex
x = 3+5jStrings in Python are surrounded by either single quotation marks, or double quotation marks. Input:
a = "Hello World" # str
b = 'World'Booleans represent one of two values: True or False.
Input:
j = True # bool
k = FalseNone is used to define a null variable or an object. In Python, None is an object of its own datatype.
Input:
k = None # NoneTypeBinary types are advanced objects used to manipulate raw binary data. Input:
l = b"Hello" # bytes
m = bytearray(5) # bytearray
n = memoryview(bytes(5)) # memoryview