Home > Backend Development > Python Tutorial > Summary of Python data types

Summary of Python data types

黄舟
Release: 2016-12-16 16:40:32
Original
1460 people have browsed it

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 " ", 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 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

str

bytes

Unordered data type


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)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template