Home >Backend Development >Python Tutorial >Summary of Python data types
In-place variable types and immutable types
In-place immutable types are also called hashable types, and in-place mutable types are also called unhashable types.
In-place immutable types:
Number types: int, float, decimal.Decimal, fractions.Fraction, complex
String types: str, bytes
tuple
frozenset
Boolean types: True, False
None
In-place mutable type:
list
dict
set
How to actually check whether the in-place mutable
hash will still return the hash value of the in-place immutable type , if this function is called on an in-place mutable type, a TypeError will be returned.
Only types that can be hashed can be used as dict keys.
Only types that can be hashed can be placed in a set, so the set itself cannot be nested in a set.
Example:
>>> hash(b'aaa')
6904179387427091653
>>> hash(bytearray(b'aaa'))
Traceback (most recent last call):
File "
TypeError: unhashable type: 'bytearray'
>>> hash(frozenset({1,2,3}))
-7699079583225461316
>> ;> hash({1,2,3})
Traceback (most recent call last):
File "
TypeError: unhashable type: 'set'
How to actually check whether the operation is changed in place.
The built-in function id() will return the actual storage address of an object in memory.
>>> L=[1]
>>> id(L)
49689480
>>> L.append(2)
>>> id(L)
49689480 #Modify in place, so you can see that the storage address has not changed.
>> #Since it cannot be modified in place, when the string changes, a new memory address is opened for storage.
>
The size of each element will be compared in sequence during comparison. If the types are found to be different during the comparison, an error will pop up.
list
tuple
bytes
dict
set
The above is a summary of Python's data types. For more related articles, please pay attention to PHP Chinese website (www.php.cn)!