简体   繁体   English

python的内置数据类型

[英]Built-in data types of python

These are the main built-in data types that I know in Python:这些是我在 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:引用 python 库参考:

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.普通整数(也简称为整数)在 C 中使用 long 实现,这为它们提供了至少 32 位的精度。 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.浮点数是在 C 中使用 double 实现的。除非您碰巧知道您正在使用的机器,否则所有对其精度的赌注都是关闭的。

According to the Python documentation :根据Python 文档

The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions.主要的内置类型是数字、序列、映射、类、实例和异常。

Numeric Types: int , float , complex数字类型: intfloatcomplex

Sequence Types: list , tuple , range序列类型: listtuplerange

Text Sequence Type: str文本序列类型: str

Binary Sequence Types: bytes , bytearray , memoryview二进制序列类型: bytesbytearraymemoryview

Set Types: set , frozenset集合类型: setfrozenset

Mapping Types:dict映射类型: - 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.模块、类和类实例、函数、方法、代码对象、类型对象、空对象( None )、省略号对象、未实现对象、布尔值( TrueFalse )、内部对象。

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.浮点数通常在 C 中使用 double 实现。

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.可能你有点困惑,因为从数学上讲,任何类型的int和任何类型的float属于实数集。 The numbers module defines a hierarchy of numeric abstract base classes: Number , Complex , Real , Rational , and Integral . numbers模块定义了数字抽象基类的层次结构: NumberComplexRealRationalIntegral 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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