Skip to content

Python 数字

  • 数字用于在程序中存储数值。
  • Python 支持三种类型的数字——整数、浮点数和复数。
  • Python 2 也支持“long”类型,但在 Python 3 中已弃用。
  • 在Python中,数字也是一种对象。它们的数据类型有:整数(int)、浮点数(float)和复数(complex)。
  • 程序内置了创建数字的函数——int()、float() 和 complex()。
  • 我们还可以通过直接将值赋给变量来创建一个数字。
  • 复数主要用于几何学、微积分和科学计算。
  • 我们可以通过实现__int__()、float()和__complex__()方法来定义对象的数值表示。

如何在Python中创建数字变量?

python
x = 10
y = 10.55
z = 1 + 2j

复数由两部分组成——实部和虚部。虚部用后缀“j”表示。

如何判断一个数字的类型?

我们可以使用 type() 函数找到数字的类型。

python
print(type(x))
print(type(y))
print(type(z))

输出:

Python 数字

1. 整数

整数是正整数,可以是正数也可以是负数,但不能包含小数部分。

我们可以使用 int() 函数获取对象的整数表示形式。该对象必须实现 __int__() 方法,该方法返回一个整数。

让我们来看一些在 Python 中创建整数的例子。

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

输出:

shell
<class 'int'>
<class 'int'>
10
<class 'int'>

String 类提供了 int() 方法,所以我们可以使用 int() 方法轻松地将字符串转换为 int。

如果对象没有实现 int() 方法,则 int() 函数会抛出 TypeError 异常。

Python 整数类型错误

通常,整数是以 10 为基数定义的。但是,我们也可以用二进制、八进制和十六进制格式来定义它们。

python
i = 0b1010
print(i)  # 10
 
i = 0xFF
print(i)  # 255
 
i = 0o153
print(i)  # 107

2. 浮点

浮点数包含小数点。它可以是正数,也可以是负数。

我们可以使用 float() 函数获取对象的浮点数表示形式。该对象必须实现 __float__() 方法,该方法返回一个浮点数。

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

输出:

shell
10.5
<class 'float'>
10.5
<class 'float'>
10.5
<class 'float'>
10.0
<class 'float'>

String 类提供了 float() 方法的实现。这就是为什么我们可以轻松地将字符串转换为浮点数的原因。

如果对象没有实现__float__()方法,我们会收到如下错误信息:

shell
TypeError: float() argument must be a string or a number, not 'Data'

如果对象的 float() 方法没有返回浮点数,我们会收到如下错误信息:

shell
TypeError: Data.__float__ returned non-float (type int)

我们还可以用科学计数法,用“e”或“E”来定义浮点数。这里,“E”后面的数字表示10的幂。

shell
x = 10.5e2
print(x)
 
x = 10.5E2
print(x)

输出:

shell
1050.0
1050.0

解释:10.5E2 = 10.5 * pow(10, 2) = 10.5 * 100 = 1050.0

3. 复数

复数由两部分组成——实部和虚部。虚部用后缀“j”表示。

我们还可以使用 complex() 函数来创建复数。complex() 函数可以接受两个整数或浮点数参数。第一个参数是实部,第二个参数是复数部分。

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

输出:

shell
(1+2j)
<class 'complex'>
(-1-4j)
<class 'complex'>
(1+2j)
<class 'complex'>
(1+0j)
<class 'complex'>
(-1-2.5j)
<class 'complex'>

我们还可以通过定义 __complex__() 方法来获取对象复数表示。该方法必须返回一个复数。

python
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 复数

我们还可以将字符串转换为复数。实部和虚部之间不应该有任何空格。

python
c = complex("1+2j")  # works fine
 
c = complex("1 + 2j") # ValueError: complex() arg is a malformed string

我们可以利用复数的“实部”性质求出复数的实部。我们可以利用复数的“虚部”性质求出复数的虚部。

python
c = 10 + 20j
print(c.real)  # real part
print(c.imag)  # imaginary part

其他一些复数处理方法包括:

  • conjugate():返回复共轭数。虚部的符号取反。
  • abs():返回复数的模。
python
c = 1 + 2j
 
print(c.conjugate())  # (1-2j)
print(abs(c))  # 2.23606797749979

Python 数字类型转换

我们可以使用 float() 函数将 int 类型转换为 float 类型。同样,我们可以使用 int() 函数将 float 类型转换为 int 类型。

我们可以使用 complex() 函数将整数或浮点数转换为复数,虚部为 0j。

我们无法将复数转换为整数或浮点数。

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

输出:

shell
10.0
<class 'float'>
10
<class 'int'>
(10+0j)
<class 'complex'>
(10.55+0j)
<class 'complex'>

结论

数字是任何编程语言不可或缺的一部分。Python 支持三种类型的数字:整数 (int)、浮点数 (float) 和复数 (complex)。Python 中的数字也是整数、浮点数和复数类型的对象。我们可以使用 int()float()complex() 函数将对象转换为数字。复数主要用于几何和科学计算。

参考: