Home Backend Development Python Tutorial What are the built-in data types in Python?

What are the built-in data types in Python?

Jun 12, 2019 pm 01:34 PM

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
Copy after login

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, ...):返回最小值
Copy after login

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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

How to Use Python to Find the Zipf Distribution of a Text File How to Use Python to Find the Zipf Distribution of a Text File Mar 05, 2025 am 09:58 AM

This tutorial demonstrates how to use Python to process the statistical concept of Zipf's law and demonstrates the efficiency of Python's reading and sorting large text files when processing the law. You may be wondering what the term Zipf distribution means. To understand this term, we first need to define Zipf's law. Don't worry, I'll try to simplify the instructions. Zipf's Law Zipf's law simply means: in a large natural language corpus, the most frequently occurring words appear about twice as frequently as the second frequent words, three times as the third frequent words, four times as the fourth frequent words, and so on. Let's look at an example. If you look at the Brown corpus in American English, you will notice that the most frequent word is "th

How Do I Use Beautiful Soup to Parse HTML? How Do I Use Beautiful Soup to Parse HTML? Mar 10, 2025 pm 06:54 PM

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

Image Filtering in Python Image Filtering in Python Mar 03, 2025 am 09:44 AM

Dealing with noisy images is a common problem, especially with mobile phone or low-resolution camera photos. This tutorial explores image filtering techniques in Python using OpenCV to tackle this issue. Image Filtering: A Powerful Tool Image filter

How to Work With PDF Documents Using Python How to Work With PDF Documents Using Python Mar 02, 2025 am 09:54 AM

PDF files are popular for their cross-platform compatibility, with content and layout consistent across operating systems, reading devices and software. However, unlike Python processing plain text files, PDF files are binary files with more complex structures and contain elements such as fonts, colors, and images. Fortunately, it is not difficult to process PDF files with Python's external modules. This article will use the PyPDF2 module to demonstrate how to open a PDF file, print a page, and extract text. For the creation and editing of PDF files, please refer to another tutorial from me. Preparation The core lies in using external module PyPDF2. First, install it using pip: pip is P

How to Cache Using Redis in Django Applications How to Cache Using Redis in Django Applications Mar 02, 2025 am 10:10 AM

This tutorial demonstrates how to leverage Redis caching to boost the performance of Python applications, specifically within a Django framework. We'll cover Redis installation, Django configuration, and performance comparisons to highlight the bene

How to Perform Deep Learning with TensorFlow or PyTorch? How to Perform Deep Learning with TensorFlow or PyTorch? Mar 10, 2025 pm 06:52 PM

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap

How to Implement Your Own Data Structure in Python How to Implement Your Own Data Structure in Python Mar 03, 2025 am 09:28 AM

This tutorial demonstrates creating a custom pipeline data structure in Python 3, leveraging classes and operator overloading for enhanced functionality. The pipeline's flexibility lies in its ability to apply a series of functions to a data set, ge

Introduction to Parallel and Concurrent Programming in Python Introduction to Parallel and Concurrent Programming in Python Mar 03, 2025 am 10:32 AM

Python, a favorite for data science and processing, offers a rich ecosystem for high-performance computing. However, parallel programming in Python presents unique challenges. This tutorial explores these challenges, focusing on the Global Interprete

See all articles