Home > Backend Development > Python Tutorial > This article will help you understand the numeric types of Python data types.

This article will help you understand the numeric types of Python data types.

Release: 2023-07-25 14:02:33
forward
1752 people have browsed it

1. Numeric type

Numeric type is used to store numerical values ​​in the mathematical sense .

Number types are immutable types. The so-called immutable type means that once the value of the type is different, it is a brand new object. Numbers 1 and 2 represent two different objects respectively. Reassigning a variable to a numeric type will create a new numeric object.


#The relationship between Python variables and data types.

A variable is just a reference to an object or a codename, name, call, etc. The variable itself has no concept of data type. Similar to 1, [2, 3, 4], only objects such as "haha" have the concept of data type.

For example:

a = 1 # 创建数字对象1。


a = 2 # 创建数字对象2,并将2赋值给变量a,a不再指向数字对象1
Copy after login

Here, what has changed is the point of variable a, not the number object 1 Becomes a digital object 2. Beginners may be confused, but it doesn't matter, we try to understand it. <br/>

<br/>

2. Python supports three different number types (integers, floating point numbers and complex numbers)

1. Integer (Int)

is usually called an integer type, which is a positive or negative integer without a decimal point. The integer type of Python3 can be used as a Long type (longer integer type), so Python3 does not have the Long type of Python2.

For example: 1, 100, -8080, 0, etc.

When representing numbers, sometimes we also use octal or hexadecimal:

Hexadecimal is prefixed with 0x And 0-9, a-f means, for example: 0xff00, 0xa5b4c3d2.

Octal is represented by 0o prefix and 0-7, such as 0o12.

Python's integer length is 32 bits, and memory space is usually allocated continuously.

什么是空间地址?

空间地址(address space)表示任何一个计算机实体所占用的内存大小。比如外设、文件、服务器或者一个网络计算机。地址空间包括物理空间以及虚拟空间。

例 :

print(id(-2))


print(id(-1))


print(id(0))


print(id(1))


print(id(2))
Copy after login

This article will help you understand the numeric types of Python data types.

从上面的空间地址看,地址之间正好差32。为什么会这样?

因为Python在初始化环境的时候就在内存里自动划分了一块空间,专门用于整数对象的存取。当然,这块空间也不是无限大小的,能保存的整数是有限的,所以你会看到id(0)和id(10000)之间的地址差别很大。

>>> id(0)
1456976928
>>> id(10000)
45818192
Copy after login

This article will help you understand the numeric types of Python data types.

小整数对象池:<br/>

Python初始化的时候会自动建立一个小整数对象池,方便我们调用,避免后期重复生成!

这是一个包含262个指向整数对象的指针数组,范围是-5到256。也就是说比如整数10,即使我们在程序里没有创建它,其实在Python后台已经悄悄为我们创建了。

验证一下小整数对象池的存在

在程序运行时,包括Python后台自己的运行环境中,会频繁使用这一范围内的整数,如果每需要一个,你就创建一个,那么无疑会增加很多开销。创建一个一直存在,永不销毁,随用随拿的小整数对象池,无疑是个比较实惠的做法。

print(id(-6))
print(id(-5))
print(id(-4))
print(id(255))
print(id(256))
print(id(257))
Copy after login

This article will help you understand the numeric types of Python data types.<br/>

从id(-6)和id(257)的地址,我们能看出小整数对象池的范围,正好是-5到256。<br/>

除了小整数对象池,Python还有整数缓冲区的概念,也就是刚被删除的整数,不会被真正立刻删除回收,而是在后台缓冲一段时间,等待下一次的可能调用。

>>> a = 1000000>>> id(a)45818160>>> del a       # 删除变量a>>> b = 1000000>>> id(b)45818160
Copy after login

给变量a赋值了整数1000000,看了一下它的内存地址。然后我把a删了,又创建个新变量b,依然赋值为1000000,再次看下b的内存地址,和以前a存在的是一样的。<br/>

del是Python的删除关键字,可以删除变量、函数、类等等。

这一段内容,可能感觉没什么大用,但它对于理解Python的运行机制有很大帮助。

2. Floating point numbers (float)

Floating point numbers are decimals, such as 1.23, 3.14, -9.01, etc. But for very large or very small floating point numbers, they are generally expressed in scientific notation, replacing 10 with e, 1.23x10^9 is 1.23e9, or 12.3e8, 0.000012 can be written as 1.2e-5, and so on.

3. Complex numbers ((complex))

Complex numbers are composed of real part and imaginary part. You can use a bj, or complex(a ,b) means that the real part a and the imaginary part b of the complex number are both floating point types. Regarding complex numbers, it is usually difficult to encounter them without doing scientific calculations or other special needs.

Number type conversion:

Sometimes, we need to convert the type of numbers. Python provides us with convenient built-in data type conversion functions.

int(x): Convert x to an integer. If x is a floating point number, the decimal part is truncated.

float(x): Convert x to a floating point number.

complex(x): Convert x to a complex number, with the real part being x and the imaginary part being 0.

complex(x, y):将 x 和 y 转换到一个复数,实数部分为 x,虚数部分为 y。

转换过程中如果出现无法转换的对象,则会抛出异常,比如int("haha"),你说我把字符串“haha”转换为哪个整数才对?

a = 10.53b = 23print(int(a))
print(float(a))
print(complex(a))
print(complex(a, b))
Copy after login

This article will help you understand the numeric types of Python data types.

<br/>

三、math库(数学计算)

科学计算需要导入math这个库,它包含了绝大多数我们可能需要的科学计算函数,一般常用的函数主要包括abs()、exp()、fabs()、max()、min()等,这里就不再赘述了,感兴趣的小伙伴可以自行百度下。

下面是两个常用数学常量:

下面是一些应用展示,注意最后的角度调用方式:

import mathprint(math.log(2))
print(math.cos(30))
print(math.cos(60))print(math.sin(30))
print(math.sin(math.degrees(30)))
print(math.sin(math.radians(30)))
Copy after login
<br/>
Copy after login

This article will help you understand the numeric types of Python data types.

四、总结

    本文详细的讲解了Python基础 ( 数字类型 )。介绍了有关Python 支持三种不同的数字类型。以及在实际操作中会遇到的问题,提供了解决方案。

The above is the detailed content of This article will help you understand the numeric types of Python data types.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Constant##Description
pi##Mathematical constant pi (pi, generally expressed as π Represents)
eMathematical constant e, e is Natural constants (natural constants).