In-situ variable type and immutable type in-situ immutable type are also called hashable type, and in-situ variable type is also called non-hash type.
In-situ and immutable types
In-situ immutable type is also called hashable type, and in-situ immutable type is also called non-hashable type.
In-situ immutable type:
Numeric Type: int, float, decimal. Decimal, fractions. Fraction, complex
String type: str, bytes
Tuple
Frozenset
Boolean: True, False
None
Variable type:
List
Dict
Set
How can I check whether the field is variable?
Hash still returns the hash value of the in-situ immutable type. if you call a function of the in-situ variable type, a TypeError is returned.
Only the hash type can be used as the dict key.
Only the hash type can be put into the set, so the set itself cannot be nested in the set.
Example:
>>> Hash (B 'aaa ')
6904179387427091653
>>> Hash (bytearray (B 'aaa '))
Traceback (most recent call last ):
File" ", Line 1, in
TypeError: unhashable type: 'bytearray'
>>> Hash (frozenset ({1, 2, 3 }))
-7699079583225461316
>>> Hash ({1, 2, 3 })
Traceback (most recent call last ):
File" ", Line 1, in
TypeError: unhashable type: 'set'
How to verify whether the operation is in-situ change.
The built-in function id () returns the actual storage address of an object in the memory.
>>> L = [1]
>>> Id (L)
49689480
>>> L. append (2)
>>> Id (L)
49689480 # Modify the storage address in the same place.
>>> S = 'A'
>>> Id (s)
47072456
>>> S + = 'BB'
>>> Id (s)
49700008 # because it cannot be modified in the original place, a new memory address is opened for storage after the string changes.
>>>
Order of data storage
Ordered Data types
Ordered data types are called sequences. they support indexing, partitioning, addition, multiplication, calculation length, and comparison operations.
The size of each element is compared in sequence. if the types are different during comparison, an error occurs.
List
Tuple
Str
Bytes
Unordered data types
Dict
Set
The above is the summary of Python data types. for more related articles, please follow the PHP Chinese network (www.php1.cn )!