Skip to content

Latest commit

 

History

History
114 lines (95 loc) · 4.72 KB

1. Built-in Data Types.md

File metadata and controls

114 lines (95 loc) · 4.72 KB

Built-in Data Types

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!


1. The Python Data Type Taxonomy

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)

2. Numeric Types (int, float, complex)

Int (Integer)

Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. Input:

b = 20              # int
y = 35656222554887711
z = -3255522

Float (Floating Point)

Float is a number, positive or negative, containing one or more decimals. Input:

c = 20.5            # float
y = -35.59
z = 35e3            # 35 * 10^3

Complex

Complex numbers are written with a "j" as the imaginary part. Input:

d = 1j              # complex
x = 3+5j

3. Text Type (str)

Strings in Python are surrounded by either single quotation marks, or double quotation marks. Input:

a = "Hello World"   # str
b = 'World'

4. Boolean Type (bool)

Booleans represent one of two values: True or False. Input:

j = True            # bool
k = False

5. None Type (NoneType)

None is used to define a null variable or an object. In Python, None is an object of its own datatype. Input:

k = None            # NoneType

6. Binary Types (bytes, bytearray, memoryview)

Binary types are advanced objects used to manipulate raw binary data. Input:

l = b"Hello"        # bytes
m = bytearray(5)    # bytearray
n = memoryview(bytes(5)) # memoryview