Python data types
Speaking of data types, we should mention that Python is a language with inferred strong dynamic typing (More about type systems).
Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.
At a hardware level, a variable is a reference to a location in memory.
There are three distinct numeric types: int, float, complex
int - or integer, is a whole number, positive or negative, without decimals, of unlimited length (up to the maximum available memory of the system).
float - or floating point number is a number, positive or negative, containing a decimal point or an exponent sign “e” (indicate the power of 10: 35e3, 12E4). It is accurate up to 15 decimal places.
float includes nan and inf/-inf.
complex - or complex numbers (x+yj, where x - real part, y - imaginary part and j - imaginary unit).
long - it is used to hold longer integers.
In addition, Booleans are a subtype of integers.
Booleans represent one of two values: True or False (constant objects, case sensitive).
The bool() function allows you to evaluate any value, and return True or False.
True and false value it’s not only the True and False.
True value is:
- any non-zero number
- any non-empty string
- any non-empty object
False value is:
- empty string
- empty object
There are three basic sequence types: str, list, tuple, range and binary sequence types: bytes, bytearray, memoryview.
Like all data types, sequences can be mutable or immutable.
This operations are supported by most sequence types, both mutable and immutable.:
hash() - the only operation that immutable sequence types generally implement that is not also implemented by mutable sequence types. This support allows immutable sequences, such as tuple instances, to be used as dict keys and stored in set and frozenset instances.
Operations supported by mutable sequence types:
Strings are immutable ordered sequences of Unicode code points. In python there is no character data type (char), a character is a string of length one. String literals are written in a variety of ways:
Strings implement all of the common sequence operations, along with the additional methods of class str().
Lists are mutable ordered sequences, typically used to store collections of homogeneous items. Since lists are indexed, lists can have items with the same value.
Tuples are immutable ordered sequences, typically used to store collections of heterogeneous data. Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set or dict instance).
Tuples are generally faster than the list data type in Python.
The range type represents an immutable ordered sequence of numbers and is commonly used for looping a specific number of times in for loops.
Ranges implement all of the common sequence operations except concatenation and repetition.
Testing range objects for equality with == and != compares them as sequences.
The start, stop and step attributes.
Bytes objects are immutable ordered sequences of single bytes. The syntax for bytes literals is largely the same as that for string literals, except that a b prefix is added.
Bytearray objects are a mutable counterpart to bytes objects.
Memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying.
A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.
Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.
There are currently two built-in set types, set and frozenset.
The set type is mutable — the contents can be changed using methods like add() and remove(). Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set.
The frozenset type is immutable and hashable — its contents cannot be altered after it is created. It can therefore be used as a dictionary key or as an element of another set.
A dictionary is ordered, mutable collection of key: value pairs which do not allow key duplicates.
- Dictionary keys are arbitrary, immutable (hashable) values.
- The values in dictionary items can be of any data type.
Dictionaries are ordered.
Dictionaries and dictionary view objects (dict.keys(), dict.values(), dict.items()) are now reversible.
Dictionaries can be created by several means:
In other programming languages, a dictionary-like data type might be called an associative array, hash, or hash table.
Python documentation - Built-in Types
Understand How Much Memory Your Python Objects Use