Home  >  Article  >  Backend Development  >  Let’s briefly talk about several common data types in Python

Let’s briefly talk about several common data types in Python

高洛峰
高洛峰Original
2017-02-13 13:41:251266browse

Variables in Python do not need to be declared. Each variable must be assigned a value before use. The variable will not be created until the variable is assigned a value. In Python, a variable is a variable, it has no type. What we call "type" is the type of the object in memory that the variable refers to.

As the name suggests, a computer is a machine that can do mathematical calculations. Therefore, computer programs can naturally handle various numerical values. However, computers can process much more than just numerical values. They can also process a variety of data such as text, graphics, audio, video, web pages, etc. Different data require the definition of different data types. In Python, the following data types can be processed directly:

1. Integers

Python can handle integers of any size, including negative integers of course. In Python programs, integers are expressed exactly the same as in mathematics, for example: 1, 100, -8080, 0, etc.

Since computers use binary, it is sometimes more convenient to use hexadecimal to represent integers. Hexadecimal is represented by the 0x prefix and 0-9, a-f, for example: 0xff00, 0xa5b4c3d2, etc.

2. Floating-point numbers

Floating-point numbers are also decimals. They are called floating-point numbers because when expressed in scientific notation, the decimal point of a floating-point number The position is variable, for example, 1.23x10^9 and 12.3x10^8 are equal. Floating point numbers can be written mathematically, such as 1.23, 3.14, -9.01, etc. But for very large or very small floating point numbers, they must be expressed in scientific notation. Replace 10 with e. 1.23x10^9 is 1.23e9, or 12.3e8, 0.000012 can be written as 1.2e-5, and so on.

The way integers and floating-point numbers are stored inside the computer are different. Integer operations are always accurate (is division also accurate? Yes!), while floating-point operations may have rounding errors.

3. String

A string is any text enclosed in '' or "", such as 'abc', "xyz", etc. Please note that '' or "" itself is just a way of expression, not part of the string. Therefore, the string 'abc' only has 3 characters: a, b, c.

4. Boolean value

The representation of Boolean value and Boolean algebra is exactly the same. A Boolean value has only two values: True and False, either True or False , in Python, you can directly use True and False to represent Boolean values ​​(please pay attention to the case), or you can calculate them through Boolean operations.

Boolean values ​​can be operated with and, or and not.

The and operation is an AND operation. Only when everything is True, the result of the AND operation is True.

The or operation is an OR operation. As long as one of them is True, the result of the or operation is True.

The not operation is a negation operation. It is a unary operator that turns True into False and False into True.

5. Null value

The null value is a special value in Python, represented by None. None cannot be understood as 0, because 0 is meaningful, and None is a special null value.

In addition, Python also provides a variety of data types such as lists and dictionaries, and also allows the creation of custom data types. We will continue to talk about it later

More briefly talk about several data types in Python For articles related to common data types, please pay attention to 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