Table of Contents
1. Numeric type" >1. Numeric type
#The relationship between Python variables and data types. " >#The relationship between Python variables and data types.
2. Python supports three different number types (integers, floating point numbers and complex numbers)" > 2. Python supports three different number types (integers, floating point numbers and complex numbers)
1. Integer (Int)" >1. Integer (Int)
什么是空间地址?" >什么是空间地址?
小整数对象池:
" >小整数对象池:
2. Floating point numbers (float) " >2. Floating point numbers (float)
3. Complex numbers ((complex))" >3. Complex numbers ((complex))
三、math库(数学计算)" >三、math库(数学计算)
四、总结" >四、总结
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.

Jul 25, 2023 pm 02:02 PM
python type of data


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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles
Constant##Description
pi##Mathematical constant pi (pi, generally expressed as π Represents)
eMathematical constant e, e is Natural constants (natural constants).