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

What are the built-in data types in Python?

silencement
silencementOriginal
2019-06-12 13:34:558474browse

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