search
HomeBackend DevelopmentPython TutorialWhat are the built-in data types in Python?

A data type is a collection of values ​​and a set of operations defined on this value. The basis of all languages ​​​​is data structure, so laying a good foundation will be beneficial to subsequent learning.

What are the built-in data types in Python?

Commonly used built-in python Data types include: numbers, strings, Bytes, lists, tuples, dictionaries, sets, Boolean, etc.

Number types

are used to store mathematics Values, such as integers, floating point numbers, complex numbers, etc. Numeric types are immutable types in Python, which means that after a variable is assigned a different value, it no longer points to the original memory. Python is based on a memory management mechanism.

1. Integer (int)

is usually called an integer type, including positive and negative numbers. python3 does not distinguish the type of integers in terms of length, that is, there is no longer a long integer type. .

Numbers also have octal and hexadecimal representation:

Hexadecimal: prefix 0x and 0-9, a-f representation, for example: 0xff00

Octal: prefix 0o and 0-7 represent, for example: 0o17

The integer length of Python is 32 bits, which is usually a continuously allocated memory space. When Python is initialized, it will automatically create a small integer object pool, between -5 and 256, which is convenient for calling and avoids repeated generation later.

In addition to the small integer object pool, Python also has an integer buffer, which is the integer that has just been deleted. It will not be deleted and recycled immediately, but will be buffered in the background for a period of time, waiting for the next possible call.

For example

a = 3453453
print(id(a))---->内存编号33402576
del a      #已经删除
b = 3453453 #将3453453赋值给b
print(id(b))----->内存编号33402576

2. Floating point number (float)

Floating point number is a decimal, such as 1.23, 1.0, etc., usually a large or small float Points are expressed in scientific notation, and 10 is represented by e. For example: 1.23*10^9 can be expressed as 1.23e10.

3. Complex number (complex)

The complex number consists of the sum of the real part It is composed of imaginary number part, such as a bj, or complex(a,b). Rarely used.

4. Numeric type conversion

int(x): Convert x to an integer. If x is a floating point number, keep the integer part. Decimal is used by default in int(), and you can specify the system, convert the number in the specified base system into a decimal number.

For example: the three commonly used bases are 2/8/16. For example: int("0b10", 2) converts the binary number 0 or 0 into a decimal number and outputs it, and the result is 2.
float(x): Convert x to a floating point number
complex(x) or complex(x, y): Rarely used

5. Calculation

In addition to the , -, *, /, **, // and % operators, python also provides libraries for scientific calculations, such as math. After importing the math library, the commonly used functions are:

abs(x):返回x的绝对值,类型随x
fabs(x):返回x的绝对值,类型是浮点数
ceil(x):取x的上入整数,如math.ceil(4.1)返回5
floor(x):取x的下入整数,如math.floor(4.9)返回4
round(x [,n]):默认返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的n位。例如round(1.23456, 3)返回1.235
exp(x):返回e的x次幂,e是自然常数
sqrt(x):返回x的平方根,返回值是float类型
modf(x):返回x的整数部分和小数部分,两部分的符号与x相同,整数部分以浮点型表示。例如math.modf(4.333),返回元组(0.3330000000000002, 4.0)
log10(x):返回以10为基数的x的对数,返回值类型是浮点数
log(x,y):返回以y为基数的x的对数,返回值类型是浮点数
pow(x, y):返回x的y次幂,即x**y
max(n1, n2, ...):返回最大值
min(n1, n2, ...):返回最小值

The above is the detailed content of What are the built-in data types in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Python: Automation, Scripting, and Task ManagementPython: Automation, Scripting, and Task ManagementApr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Python and Time: Making the Most of Your Study TimePython and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool