Data type
In reality, according to the different performance of things, and then can be the corresponding classification. For example, when shopping in the supermarket, the supermarket will be based on the product
Different characteristics of the classification: food, daily necessities, household appliances, such as ... When you are learning math, you can also categorize values: integers, decimals,
Score, plural ... Similarly, in Python, the values can be categorized differently depending on the characteristics of the data.
In Python, data can be divided into the following types:
Numeric type
Integer type (int)
Floating-point type (float)
Plural type (complex)
Boolean type (BOOL)
Sequence type
Listing (list)
Tuple (tuple)
String (str)
Bytes (bytes)
Mapping Type (DICT)
Collection type (SET)
Other built-in types
Duck type
In Python, strictly speaking, a variable is not of type, and the type of the variable depends entirely on the type of object it is associated with, which
The difference between languages is large (C,java, etc.). Therefore, it is customary to say that the type of Python is "duck type", which is the embodiment of a dynamic type.
Description
1, because the variable must be associated with a specific object, you can determine the type of the variable. Therefore, when defining a variable, you cannot just give the variable
Name, you must also give the object that the variable is associated with.
2, Python2 also supports the long integer type (long), which is canceled in Python3.
Integer type
An integer type (int) is the most commonly used type. Supports four types of representations
Binary (using 0b or 0 B prefixes)
Octal (using 0o or 0O prefixes)
Decimal (no prefix)
Hex (using 0x or 0X prefixes, 10 ~ 15 using a ~ f or a ~ f)
Binary conversion
You can call the appropriate function to convert between four types of binary.
Bin () Other binary conversion into binary
Oct () Other binary converted to octal
int () Other binary converted to decimal
Hex () Other binary converts to hex
Separator
When the value is large (especially binary), the readability of the program is affected, and you can use it between the values
A _ is delimited to improve the readability of the program (Python3.6).
Boolean type
A Boolean type (BOOL) represents a condition that has two values: True and False. Where true indicates that the condition is set,
Is true, and false indicates that the condition is not valid, which is false. A Boolean type is a subtype of an integer type. In fact, true is 1,
False is 0. Thus, you can also use Boolean types to participate in mathematical operations, as are the results of using 1 and 0 to participate in the operation.
Floating-point types
A floating-point type (float) is a mathematical decimal type. For example: 1.0,-2.5, etc., are floating-point types. On the binary representation, the floating-point class
Type only supports decimal notation (this differs from integer type). In addition to the regular representation, floating-point types can also be used mathematically
In the form of a number method, for example:
1.5e30
Description
Decimal integer types cannot start with 0, but floating-point types can start with 0.
Special values for floating-point types
A floating-point type has two special values: NaN (not A number) and infinity (infinity), where infinity can be divided into positive infinity
(+infinity) and negative infinity (-infinity). These values can be obtained by using the float function.
Nan has a characteristic that oneself does not equal oneself, therefore, cannot pass = = To judge a value is not Nan.
Inaccuracy of floating-point data types
When using floating-point types, be aware that floating-point types are only approximate storage in computers, and floating-point types are imprecise. Therefore, to avoid
Do not do the following:
1, to avoid the magnitude difference between a large number of floating-point calculation.
2. Avoid making equal comparisons.
Use of decimal
Given the uncertainty of floating-point types, you can use the decimal class in the Decimalmo module if you need to perform a precise calculation
, this class provides accurate floating-point calculations.
In addition, you can specify the calculation precision of decimal, which is the number of significant digits that are retained by the calculation result, which defaults to a 28-bit valid number.
Plural type
The plural type (complex) in Python is similar to a mathematical complex number. The numeric value containing the imaginary part (using the J or J suffix) is the plural type.
For example:
3 + 5j,-2-3j (the imaginary part suffix in mathematics is i).
The complex number is divided into the real part and the imaginary Part two parts, can obtain the real part and the imaginary part of the plural by the real and the Imag property respectively.
Type conversions
The type of the variable can be obtained through the type function, which is exactly the type of the object that the variable is associated with.
Type conversion function
Python provides a number of conversion functions that can be cast between different types.
int (value)
Converts a numeric type or string to an integer (int) type. If there are no parameters, return 0.
Float (value)
Converts a numeric type or string to a floating-point (float) type. If there are no parameters, return 0.
Complex (real, imag)
Creates a complex number based on the real and imaginary parts (optional). If there are no parameters, return 0j.
BOOL (value)
Returns true if the argument is true, otherwise false is returned.
Attention:
1. Although you can use a conversion function for type conversion, not all two types can be converted.
For example, a complex type cannot be converted to an integer or float type by int or float.
2. In addition, the INT function can also provide a second argument that specifies the binary to use when parsing the transformation. At this point, the first parameter
Need to be a string type (or a byte, etc.) and cannot be a numeric type. For example:
Int ("300", 16)
is parsed accordingto the 16 binary, the final function returns a result of 768 instead of 300. If you do not specify the section
Two parameters, the default is decimal.
Basic syntax for Python