concept datum type in category python

appears as: data types, data type, data types, The data types
Practices of the Python Pro

This is an excerpt from Manning's book Practices of the Python Pro.

Throughout this book, I’ll assume you have a foundational knowledge of Python syntax, data types, and classes. You’ve seen it and played with it, but you don’t need to have won awards with it. (Do they have those?). Anyone with a bit of programming under their belt and a few hours of learning and using Python on their own should have no problem with the code in this book. You’re going to go through this book with Python as the conduit for designing bigger, better software. That being said, what you learn here will, with any luck, be applicable to any language you choose to use. You’ll find that many software design concepts transcend any particular technology.

4.2. Performance and data types

Although your code should be designed with time and space complexity in mind, it will ultimately be built on Python’s existing data types. The following sections cover a number of use cases, as well as which data types are best suited for them.

4.2.3. Space complexity of operations on data types

Now that you’re familiar with the time complexity of some of Python’s built-in data structures, I’ll teach you a couple of tricks for using them. The data types we’ve seen so far are all iterables—objects that support iteration over their contents (in a for loop, for example). Iteration over a set of elements is nearly always going to be O(n) in time complexity; going through every element takes more time if there are more elements. But what about space complexity?

For the data types we’ve seen so far, all their contents are stored in memory together. If a list has 10 elements, it takes roughly 10 times more space in memory than a list with a single element, as shown in figure 4.4. This means their space complexity is also O(n). This can be problematic, the same way reading 7.6 billion records into memory is problematic. If we don’t need all that data at once, we might be able to find a more efficient approach.