简体   繁体   中英

Built-in data types of python

These are the main built-in data types that I know in Python:

  • Numbers
  • Strings
  • Lists
  • Tuples
  • Dictionaries
  • Boolean
  • Sets

My question is, are integers and float numbers considered to be the same data type? Or are they two separate built-in data types?

Thanks!

Quoting the python library reference:

There are four distinct numeric types: plain integers, long integers, floating point numbers, and complex numbers. In addition, Booleans are a subtype of plain integers. Plain integers (also just called integers) are implemented using long in C, which gives them at least 32 bits of precision. Long integers have unlimited precision. Floating point numbers are implemented using double in C. All bets on their precision are off unless you happen to know the machine you are working with.

According to the Python documentation :

The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions.

Numeric Types: int , float , complex

Sequence Types: list , tuple , range

Text Sequence Type: str

Binary Sequence Types: bytes , bytearray , memoryview

Set Types: set , frozenset

Mapping Types:dict

Other Built-in Types:

Modules, Classes and Class Instances, Functions, Methods, Code Objects, Type Objects, the Null Object ( None ), the Ellipsis Object, the NotImplemented Object, Boolean Values ( True and False ), Internal Objects.

Answering your question:

Are integers and float numbers considered to be the same data type?

There are three distinct numeric types: integers, floating point numbers, and complex numbers. Floating point numbers are usually implemented using double in C.

Probably you are a bit confused because mathematically speaking any number of type int and any number of type float belong to the set of the real numbers. The numbers module defines a hierarchy of numeric abstract base classes: Number , Complex , Real , Rational , and Integral . However, none of the types defined in this module can be instantiated.

You can use these classes to check if a specific number is an instance of them:

In[1]: import numbers

In [2]: isinstance(10, numbers.Integral)
Out[2]: True

In [3]: isinstance(10.5, numbers.Integral)
Out[3]: False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM