concept Python in category algorithms

appears as: Python, The Python, Python
Algorithms and Data Structures for Massive Datasets

This is an excerpt from Manning's book Algorithms and Data Structures for Massive Datasets.

One typical way to process the dataset to solve the tasks above is to create some type of a key-value dictionary, for example set, map, or unordered_map in C++, HashMap in Java, or dict in Python, etc. Key-value dictionaries are implemented either with a balanced binary tree (such as a red-black tree in C++ map), or, for faster insert and retrieval operations, a hash table (such as dict in Python or unordered_map in C++.) For simplicity, we will assume for the rest of this example that we are working with hash tables.

2.4.2   Usage scenario: How Python’s dict does it

Most situations when we use hash tables while programming include key-value dictionaries that associate a piece of information with another piece of information (word with a count, for example,). For standard libraries of C++ and Java, these are respectively unordered_map and HashMap; both of these libraries use chaining for collision resolution. For Python, the classical key-value dictionary is dict. Here is one way to create an instance of dict:

Dict = {flour: 5, cauliflower: 2, milk: 19}

Python’s default implementation, CPython, implemens dict as follows (here we only focus on the case when keys are integers): for the table size m=2i, the hash function is h(x) = x mod 2i (that is, the last i bits of binary representation of  x.) According to CPython, this works well in a number of common cases, such as a sequence of consecutive numbers (it does not create collisions). But if in the combination with this hash function, one used linear probing, this would lead to clustering, and having very long runs of consecutive items. So, in order to avoid long runs Python employs the following probe sequence:

Classic Computer Science Problems in Python

This is an excerpt from Manning's book Classic Computer Science Problems in Python.

In Python, sometimes for the sake of simplicity (which is a legitimate goal, of course), the developer is shielded from thinking in bits. There is no 64-bit unsigned integer type, and there is no 16-bit unsigned integer type. There is just a single int type that can store numbers of arbitrary precision. The function sys.getsizeof() can help you find out how many bytes of memory your Python objects are consuming. But due to the inherent overhead of the Python object system, there is no way to create an int that takes up less than 28 bytes (224 bits) in Python 3.7. A single int can be extended one bit at a time (as we will do in this example), but it consumes a minimum of 28 bytes.

Type hints are a way of annotating the expected types of variables, function parameters, and function return types in Python. In other words, they are a way that a programmer can indicate the type that is expected in a certain part of a Python program. Most Python programs are written without type hints. In fact, before reading this book, even if you are an intermediate Python programmer, it is very possible that you have never seen a Python program with type hints.

Because Python does not require the programmer to specify the type of a variable, the only way to figure out the type of a variable without type hints is through inspection (literally reading the source code up to that point or running it and printing the type) or documentation. This is problematic because it makes Python code harder to read (although some would say the opposite, and we will get to that later in this appendix). Another issue is that because Python is very flexible, it allows the programmer to use the same variable to refer to multiple types of objects, which can lead to errors. Type hints can help prevent this style of programming and alleviate these errors.

Now that Python has type hints, we call it a gradually typed language—meaning that you can use type annotations when you want to, but they are not required. In this short introduction I hope to convince you (despite perhaps your resistance to how they fundamentally change the look of the language) that having type hints available is a good thing—a good thing that you should take advantage of in your code.