Python 数字 – int、float 和complex – 码微 | 您所在的位置:网站首页 › intricate和complex区别 › Python 数字 – int、float 和complex – 码微 |
数字用于在程序中存储数值。
Python 支持三种类型的数字:int、float 和complex。
Python 2 也支持“long”,但在 Python 3 中已弃用。
在Python中,数字也是一个对象。它们的数据类型是 – int、float 和complex。
有一些内置函数可以创建数字——int()、float() 和complex()。
我们还可以通过直接将值分配给变量来创建数字。
复数主要用于几何、微积分和科学计算。
我们可以通过实现__int__()、__float__()和__complex__()方法来定义对象的数字表示。
如何在 Python 中创建数字变量?
x = 10
y = 10.55
z = 1 + 2j
复数有两部分——实部和虚部。虚部用“j”后缀表示。 如何查找数字的类型?我们可以使用 type() 函数找到数字的类型。 print(type(x)) print(type(y)) print(type(z))输出: Python 数字 1. 整数整数是整数。它们可以是积极的,也可以是消极的。它们必须没有小数值。 我们可以使用 int() 函数来获取对象的整数表示。该对象必须实现返回整数的 __int__() 方法。 让我们看一下在 Python 中创建整数的一些示例。 x = 10 print(type(x)) x = int("10") print(type(x)) class Data: id = 0 def __init__(self, i): self.id = i def __int__(self): return self.id d = Data(10) x = int(d) print(x) print(type(x))输出: 10String类提供了__int__()方法,这就是为什么我们可以使用int()方法轻松地将字符串转换为int。 如果对象没有实现 __int__() 方法,则 int() 函数会抛出 TypeError。 Python Int 类型错误一般来说,整数是以 10 为基数定义的。但是,我们也可以以二进制、八进制和十六进制格式定义它们。 i = 0b1010 print(i) # 10 i = 0xFF print(i) # 255 i = 0o153 print(i) # 107 2. 浮动浮点数包含小数点。它可以是正值,也可以是负值。 我们可以使用 float() 函数来获取对象的浮点表示形式。该对象必须实现返回浮点数的 __float__() 方法。 x = 10.50 print(x) print(type(x)) x = float("10.50") print(x) print(type(x)) class Data: id = 0.0 def __init__(self, i): self.id = i def __float__(self): return float(self.id) d = Data(10.50) x = float(d) print(x) print(type(x)) d = Data(10) x = float(d) print(x) print(type(x))输出: 10.5 10.5 10.5 10.0String 提供了 __float__() 方法实现。这就是为什么我们可以轻松地将字符串转换为浮点数。 如果对象没有实现 __float__() 方法,我们会收到如下错误消息: TypeError: float() argument must be a string or a number, not 'Data'如果对象 __float__() 方法不返回浮点数,我们会收到如下错误消息: TypeError: Data.__float__ returned non-float (type int)我们还可以使用“e”或“E”以科学记数法定义浮点数。这里“E”后面的数字指定 10 的幂。 x = 10.5e2 print(x) x = 10.5E2 print(x)输出: 1050.0 1050.0解释:10.5E2 = 10.5 * pow(10, 2) = 10.5 * 100 = 1050.0 3. 复杂复数包含两部分——实部和虚部。虚部用“j”后缀书写。 我们还可以使用complex()函数来创建复数。我们可以将两个整数或浮点参数传递给complex()函数。第一个参数是实部,第二个参数是复数部分。 x = 1 + 2j print(x) print(type(x)) x = -1 - 4j print(x) print(type(x)) x = complex(1, 2) print(x) print(type(x)) x = complex(1) print(x) print(type(x)) x = complex(-1, -2.5) print(x) print(type(x))输出: (1+2j) (-1-4j) (1+2j) (1+0j) (-1-2.5j)我们还可以通过定义 __complex__() 方法来获取对象复数表示。此方法必须返回一个复数。 class Data: def __init__(self, r, i): self.real = r self.imaginary = i def __complex__(self): return complex(self.real, self.imaginary) d = Data(10, 20) c = complex(d) print(c) print(type(c))输出: Python 复数我们还可以将字符串转换为复数。实部和虚部之间不应有任何空白。 c = complex("1+2j") # works fine c = complex("1 + 2j") # ValueError: complex() arg is a malformed string我们可以使用“实数”属性得到复数的实部。我们可以使用“imag”属性获得复数的虚部。 c = 10 + 20j print(c.real) # real part print(c.imag) # imaginary part其他一些复数方法是: Conjugate():返回复共轭数。虚部的符号相反。 abs():返回复数的大小。 c = 1 + 2j print(c.conjugate()) # (1-2j) print(abs(c)) # 2.23606797749979 Python 数字类型转换我们可以使用 float() 函数将 int 转换为 float。类似地,我们可以使用 int() 函数将 float 转换为 int。 我们可以使用complex()函数将int或float转换为复数,虚部为0j。 我们无法将复数转换为 int 或 float。 i = 10 f = 10.55 # int to float conversion f1 = float(i) print(f1) print(type(f1)) # float to int conversion i1 = int(f) print(i1) print(type(i1)) # int and float to complex number conversion c = complex(i) print(c) print(type(c)) c = complex(f) print(c) print(type(c))输出: 10.0 10 (10+0j) (10.55+0j) 结论数字是任何编程语言不可或缺的一部分。Python 支持三种类型的数字:int、float 和complex。Python 中的数字也是类型的对象——int、float 和complex。我们可以使用 int()、float() 和 complex() 函数将对象转换为数字。复数主要用于几何和科学计算。 参考: Python 数字文字 API 文档 |
CopyRight 2018-2019 实验室设备网 版权所有 |