Python from rookie to expert (5): Digital

Source: Internet
Author: User
Tags arithmetic binary to decimal decimal to binary decimal to hex hex to binary integer division
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) # result: 6
Print(126 - 654) # Operation result: -528
Print(6 + 20 * 4) # Operation result: 86
Print((20 + 54) * 30) #Operation result: 2220
Print(1/2) # Operation result: 0.5
Print(1//2) # result: 0
Print(3/2) # Operation result: 1.5
Print(3//2) # Operation result: 1
Print(4**3) # Operation result: 64
Print(3 + 5 * -3 ** 4 - (-5)**2) # Result:-427
# Operate values with variables
x = 30
y = 50
k = 10.2
Print(x + y * k) # Operation result: 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) # Output 2 to the power of 35, the output is 34359738368


Let's change a bigger number to see if it overflows.


Print(2**630 * 100000) # 2 of 630 power and then 100,000


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.


3. 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:



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



• 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)) // Output: 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))       # Output: 0xd431


The following code shows the conversion between binary, octal, decimal, and hexadecimal numbers in the Python language.


Print(0b110011) # output binary number
Print(0o123) # Output octal number
Print(0xF15) # Output hexadecimal number
Print(bin(12)) # decimal to binary, output: 0b1100
Print(int("10110",2)) #binary to decimal, output: 22
Print(int("0xF35AE",16)) # hexadecimal to decimal, output: 996782
Print(hex(54321)) # decimal to hex, output: 0xd431
Print(bin(0xF012E)) # hex to binary, output: 0b11110000000100101110
Print(hex(0b1101101)) # Binary to hexadecimal, output: 0x6d
Print(oct(1234)) # decimal to octal, output: 0o2322
Print(int("76532", 8)) # ASCII to decimal, output: 32090


The program runs as shown in the results.






"Python from rookie to master" began, 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
# Two digits after decimal point, output: '1234.57'
Print(format(x, '0.2f'))
#号 is right-aligned in the area of 12 characters, and retains 1 digit after the decimal point.
# Output result: '1234.6'
Print(format(x, '>12.1f'))
#号 is left-aligned in the 12-character length area, and retains 3 digits after the decimal point, followed by output 20,
# Output result: '1234.568 20'
Print(format(x, '<12.3f'), 20)
#号 is right-aligned in the area of 12 characters, and retains 1 digit after the decimal point. The number is preceded by 0.
# Output result: '0000001234.6'
Print(format(x, '0>12.1f'))
#号 is left-aligned in the area of 12 characters, and retains 1 digit after the decimal point. The number is followed by 0.
# Output result: '1234.6000000'
Print(format(x, '0<12.1f'))
#号 is center-aligned in the 12-character length area, and retains 2 digits after the decimal point, followed by output 3,
# Output result: '1234.57 3'
Print(format(x, '^12.2f'),3)
# Each thousand is separated by a comma (,), output: 1,234.56789
Print(format(x, ','))
# Each thousand is separated by a comma (,) and retains 2 digits after the decimal point. Output: 1,234.57
Print(format(x, ',.2f'))
# Output numbers in scientific notation, output: 1.234568e+03
Print(format(x, 'e'))
# Output numbers in scientific notation form, the mantissa retains 2 digits after the decimal point, and the output result is 1.23E+03
Print(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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.