1 Basic knowledge
?? The Python language, like other programming languages, also supports arithmetic (add, subtract, multiply, divide), and parenthesis operators. In the Python language, numbers are divided into integers and floating-point number. An integer is a number that has no fractional part, and a floating-point number is a fraction of a decimal number. For example, the following code is a standard arithmetic expression.
2 + 44 * 5 + 205.3 / 7(30 + 2) * 12
?? If you want to calculate a two-digit division, regardless of whether the numerator and denominator are integers or floating-point numbers, the result of using the division operator (/) is a floating-point number. For example, 1/2 of the results are calculated as the result of 0.5,2/2 is 1.0. To allow the Python interpreter to perform an integer division, you can use the divisible operator, which is two slashes (//). After using the divide operator, the result of 1//2 is 0,2//2 is 1.
?? The divisible operator not only performs an integer division of integers, but also performs the division of floating-point numbers, and the result is a floating-point number, as long as one of the numerator denominators is a floating-point number when performing the division operation. For example, 1.0//2 is calculated as the result of 0.0,2.0//2 is 1.0.
?? In addition to the arithmetic character, Python provides two special operators:% (take the remainder operator) and * * (power operator). The take-up operator is used to perform a take-off operation on integers and floating-point numbers. For example, 5 of 2 is calculated as 1, and 5.0 of 2 results in 1.0. As can be seen from this point,% and//is similar, as long as the numerator denominator has a floating-point number, the result is a floating-point number.
?? The power operator is used to calculate the power of a number. For example, 2 * * 3 is calculated as 8,3.2 * * 2 results in 10.24.
?? Up to now, we have introduced a total of 8 operators, which are parentheses (...). ) plus (+), minus (-), multiply (*), divide (/), divide (//), take remainder (%), and power operator (* *). Where the minus sign (-) can also be used for the minus sign (unary operator), 9 operators are now involved. Since there are so many operators involved, there is a priority problem, that is, there are several different operators in the same expression that need to be evaluated first, and if the precedence is the same, they are executed in left-to-right order.
?? The order of precedence for these 9 operators is as shown. The higher the precedence, the greater the precedence of the operators on the same row.
?? The following code shows how operators in the Python language are used, and when writing Python code, you should be aware of the precedence of operators.
print(2 + 4) # 运算结果:6print(126 - 654) # 运算结果:-528print(6 + 20 * 4) # 运算结果:86print((20 + 54) * 30) # 运算结果:2220print(1/2) # 运算结果:0.5print(1//2) # 运算结果:0print(3/2) # 运算结果:1.5print(3//2) # 运算结果:1print(4**3) # 运算结果:64print(3 + 5 * -3 ** 4 - (-5)**2) # 运算结果:-427# 用变量操作数值x = 30y = 50k = 10.2print(x + y * k) # 运算结果:540.0
2 large integers
?? For signed 32-bit integers, the maximum value that can be represented is 2147483647 (2^31-1), the minimum value that can be represented is-2147483648 ( -2^31), and if this range is exceeded, a signed 32-bit integer overflows. In the Python language, however, you can handle very large integers, not limited by the number of digits. For example, the output of the following expression exceeds the range of 32-bit integers.
print(2 ** 35) # 输出2的35次幂,输出结果是34359738368
?? Let's change a bigger number to see if it overflows.
print(2**630 * 100000) # 2的630次幂再乘10万
?? The output of the above line of code is as follows:
4455508415646675018204269146191690746966043464109921807206242693261010905477224010259680479802120507596330380442963288389 34443820446820117016861457004122479321483854917994624031530682836582400000
?? It is clear that the Python language can still handle the results of 2**630 * 100000 correctly. Therefore, using numbers in the Python language does not need to worry about overflow, because the Python language can handle very large numbers, which is one of the main reasons why many people use the Python language for scientific calculations and data analysis.
Binary, octal, and hexadecimal
?? The Python language can represent binary, octal, and hexadecimal numbers. Indicates that the 3 decimal numbers must be preceded by 0, followed by a different alphabetic notation. The letter representing the binary is B, which means that the letter of octal is O (this is the English letter small write O, do not mix with the number 0), which means that the hexadecimal letter is x. Therefore, the correct notation for binary numbers is 0b110011, the correct notation for octal numbers is 0o56432, and the correct notation for hexadecimal numbers is 0xf765a.
?? In addition to these 3 kinds of binaries, the preceding chapters have been using decimal. Therefore, the Python language can represent a total of 4 binary: binary, octal, decimal, and hexadecimal. The Python language provides a number of functions for converting between these 4 binary numbers.
?? If you are converting from another binary to a decimal, you need to use the INT function, which has two parameters, meaning the following:
? The 1th parameter: A string type that represents the binary, octal, or hexadecimal number to be converted. parameter values only need to specify the number with the conversion, do not need to use a prefix, such as binary directly specify 11011, do not need to specify 0b11011.
? The 2nd parameter: A numeric type that represents the binary of the 1th parameter value, for example, if you want to convert the binary to decimal, the 2nd parameter value is 2.
?? The INT function returns a numeric type that represents the converted decimal number.
?? The following code converts the binary number 110011 to a decimal number, and the output returns the result.
print(int("110011",2)) // 输出结果:51
?? If you want to convert from decimal to other binary, you need to use the bin, Oct, and hex functions, respectively. The bin function is used to convert decimal numbers to binary numbers, the OCT function converts decimal numbers to octal numbers, and the hex function converts decimal numbers to hexadecimal numbers. All 3 functions receive a parameter, which is the decimal number to be converted. Note, however, that the parameter values for these 3 functions can also be binary, octal, and hexadecimal numbers, which means that these 3 functions can be exchanged between binary, octal, decimal, and hexadecimal.
?? The following code converts the decimal number 54321 to a hexadecimal number and outputs the conversion result.
print(hex(54321)) # 输出结果:0xd431
The following code shows the conversion between binary, octal, decimal, and hexadecimal numbers in the Python language.
print(0b110011) # 输出二进制数print(0o123) # 输出八进制数print(0xF15) # 输出十六进制数print(bin(12)) # 十进制转二进制,输出结果:0b1100print(int("10110",2)) # 二进制转十进制,输出结果:22print(int("0xF35AE",16)) # 十六进制转十进制,输出结果:996782print(hex(54321)) # 十进制转十六进制,输出结果:0xd431print(bin(0xF012E)) # 十六进制转二进制,输出结果:0b11110000000100101110print(hex(0b1101101)) # 二进制转十六进制,输出结果:0x6dprint(oct(1234)) # 十进制转八进制,输出结果:0o2322print(int("76532", 8)) # 八进制转十进制,输出结果:32090
The program runs as shown in the results.
"Python from rookie to master" began to reprint, please pay attention to
Formatted output of 4 numbers
?? When you output a number, you sometimes need to format it. For example, in output 12.34, you only want to retain the 1 digits after the decimal point, that is, 12.3, or the integer bit by 6 bits of output, less than the previous 0, that is, 000012.34. The Format function is provided in the Python language for formatting numbers. The Format function has two parameters, meaning the following:
? 1th parameter: The number to be formatted.
? 2nd parameter: format string.
?? The return value of the Format function is the string after the number is formatted.
?? The following code demonstrates the use of the Format function for formatting numbers.
x = 1234.56789# 小数点后保留两位数,输出结果:'1234.57'print(format(x, '0.2f')) # 数字在12个字符长度的区域内右对齐,并保留小数点后1位数字,# 输出结果:' 1234.6' print(format(x, '>12.1f')) # 数字在12个字符长度的区域内左对齐,并保留小数点后3位数字,紧接着输出20,# 输出结果:'1234.568 20'print(format(x, '<12.3f'), 20)# 数字在12个字符长度的区域内右对齐,并保留小数点后1位数字,数字前面补0,# 输出结果:'0000001234.6'print(format(x, '0>12.1f'))# 数字在12个字符长度的区域内左对齐,并保留小数点后1位数字,数字后面补0,# 输出结果:'1234.6000000'print(format(x, '0<12.1f'))# 数字在12个字符长度的区域内中心对齐,并保留小数点后2位数字,紧接着输出3,# 输出结果:' 1234.57 3'print(format(x, '^12.2f'),3)# 每千位用逗号(,)分隔,输出结果:1,234.56789print(format(x, ','))# 每千位用逗号(,)分隔,并保留小数点后2位数字,输出结果:1,234.57print(format(x, ',.2f'))# 用科学计数法形式输出数字,输出结果:1.234568e+03print(format(x, 'e'))# 用科学计数法形式输出数字,尾数保留小数点后2位数字,输出结果:1.23E+03print(format(x, '0.2E'))
The program runs as shown in the results.
"Python from rookie to Master" has been published, began to serial, buy send video lessons
Python from rookie to expert (5): Digital