Summary of Python data types

Source: Internet
Author: User
Tags hashable
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.