Python Book
πŸ‡ΊπŸ‡¦ Stand with UkraineπŸŽ“Training Suite
  • Book overview
  • Notes about this book
  • 1. Introduction to Python
    • What is Python
    • Basic syntax
    • Objects in Python
    • Python overview
    • Installation, IDEs etc.
    • ipython
    • Sources for self-learning
  • 2. Strings and numbers
    • Getting help
    • Introspection
    • Basic types
    • None object
    • Numbers
    • Strings
    • Unicode
    • String Formatting
    • Regular expressions
    • Sources for self-learning
  • 3. Containers
    • Data Structures
    • Lists
    • Tuples
    • Dictionaries
    • Sets
    • Conditions
    • Loops
    • Additional modules
    • Sources for self-learning
  • 4. Functions
    • Functions
    • Scopes of visibility
    • Generators
    • Lambdas
    • Type hints
    • Function internals
    • Sources for self-learning
  • 5. Functional Programming
    • Builtins
    • Iterable
    • Iterator
    • Functional Programming
    • Functools
    • Comprehensions
    • Additional modules
    • Sources for self-learning
  • 6. Code Styling
    • Zen of Python
    • Lint
    • PEP 8
    • Modules
    • Packages
    • Sources for self-learning
  • 7. OOP
    • OOP Basics
    • Code design principles
    • Classes
    • Method Resolution Order
    • Magic attributes and methods
    • Super
    • Sources for self-learning
  • 8. Decorators, Exceptions
    • Decorators
    • Exceptions
    • Sources for self-learning
  • 9. Testing
    • Basic Terminology
    • Testing theory
    • Dev unit testing vs QA automated testing
    • Best Practices
    • Doctest
    • Unittest
    • Test Runners
    • Pytest
    • Nose
    • Continuous Integration
  • 10. System Libs
    • Working with files
    • System libraries
    • Subprocess
    • Additional CLI libraries
Powered by GitBook
On this page

Was this helpful?

Edit on Git
  1. 2. Strings and numbers

Basic types

ℹ️

Type
Short description
Mutable
Example

NoneType

"Empty" value

No

None

bool

Boolean value (True or False)

No

True

int

Integer numbers

No

42

float

Floating point number

No

23.43

str

Textual data - sequense of characters

No

"Hello!"

list

Mutable sequense of any kind of objects

Yes

[1, 2, 3]

tuple

Immutable sequense of objects

No

(1, 2, 3)

set

Mutable collection of unique objects

Yes

{1, 2, 3}

frozenset

Immutable collection of unique objects

No

frozenset({1, 2, 3})

dict

The collection of key-value pairs

Yes

{"name": "Johnny", "second_name": "Walker"}

Main categories:

  • Mutable or Immutable

    • Is object allowing it's change after creation?

  • Hashable

    • Can this object be used as the key for the dictionary (has defined __hash__() and __eq__() methods)?

  • Container, sequence, iterable

    • Has this object contained elements like other objects (has methods like __iter__ or __getitem__())?

  • Callable

    • Can be invoked/run like a function or class (has method __call__() defined)?

Additional types:

  • range

  • function

  • code object

  • module

  • iterator

  • generator

  • slice object

And much more (because everything in Python is a object and thus - everything is of some specific type).

Sizes of the object of basic types:

πŸ”₯

import sys, rich

table = rich.table.Table(rich.table.Column(header="Object", style="blue"), 
                         "Type", 
                         rich.table.Column(header="Size", style="cyan", justify="right"),
                         title="Basic types memory usage", show_lines=True)
objects = [ None, True, 42, 3.1415, "Hello", [1, 2, 3], ("a", "b", "c"),
            {1, 2, 3}, frozenset({1, 2, 3}), {"x": 1, "y": 2} ]

for obj in objects:
    table.add_row(*map(str, (obj, type(obj), sys.getsizeof(obj))))
rich.console.Console().print(table)
              Basic types memory usage               
┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━┓
┃ Object               ┃ Type                ┃ Size ┃
┑━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━┩
β”‚ None                 β”‚ <class 'NoneType'>  β”‚   16 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚ True                 β”‚ <class 'bool'>      β”‚   28 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚ 42                   β”‚ <class 'int'>       β”‚   28 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚ 3.1415               β”‚ <class 'float'>     β”‚   24 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚ Hello                β”‚ <class 'str'>       β”‚   54 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚ [1, 2, 3]            β”‚ <class 'list'>      β”‚   80 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚ ('a', 'b', 'c')      β”‚ <class 'tuple'>     β”‚   64 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚ {1, 2, 3}            β”‚ <class 'set'>       β”‚  216 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚ frozenset({1, 2, 3}) β”‚ <class 'frozenset'> β”‚  216 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚ {'x': 1, 'y': 2}     β”‚ <class 'dict'>      β”‚  232 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜

πŸ”₯

Memory requirement to store elements of int type in a different collections.

For 1 million of elements:

Type
Bytes per element

Tuple

8

List

9

Set

33

Dictionary

42

Memory size may not be the only criteria to select data type. Rather, time required to perform operation on data type can be critical criteria.

import sys, rich
n = 1000000

tests = {
    "list": [*range(n)],
    "tuple": tuple(range(n)),
    "set": set(range(n)),
    "dict": dict.fromkeys(range(n))
}

table = rich.table.Table("Type", 
                         rich.table.Column(header="Total size", style="cyan", justify="right"), 
                         rich.table.Column(header="Size per element", style="red", justify="right"),
    title=f"Collections memory requirement ({n} elements)", show_lines=True
)
for type_, collection in tests.items():
#     print(f"Size of <{type_}> ({n} items): {sys.getsizeof(collection)}, {sys.getsizeof(collection) / n} per element")
    table.add_row(str(type_), str(sys.getsizeof(collection)), str(sys.getsizeof(collection) / n))
rich.console.Console().print(table)
 Collections memory requirement (1000000 
                elements)                
┏━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┓
┃ Type  ┃ Total size ┃ Size per element ┃
┑━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━┩
β”‚ list  β”‚    8000056 β”‚         8.000056 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ tuple β”‚    8000040 β”‚          8.00004 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ set   β”‚   33554648 β”‚        33.554648 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ dict  β”‚   41943136 β”‚        41.943136 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
PreviousIntrospectionNextNone object

Last updated 2 years ago

Was this helpful?