Python Learning (4) Data Structure -- int float, pythonfloat
Python numeric type int float
Numeric constant
Int: A general integer, which is not long integer after python3.x. In 2.x, "L" or "l" must be added after the number, for example, 10000000000000000000000000l.
Float: floating point number. 1.0 is also a floating point number. float can be forcibly converted to int and rounded;
1 print (type (1234) 2 print (type (-24) 3 print (type (0) 4 print (type (1000000000000000000000000000000) 5 print (1000000000000000000000000000000) 6 print (type (1e + 40) # For floating point 7 print (int (1e + 20) # forced conversion to int 8 print (int (1e + 30 )) #9 print (type (1.0) for ultra-long precision loss # float10 print (int (1.999) # integer
Digital computing
Multiplier operation
1 a = 1 2 B = 2 3 c = 2.0 4 print (a + B) 5 print (type (a + B) 6 print (a-c) # output-1.0 7 print (type (a + c) # automatically converted to float 8 print (a * B) with floating point addition) 9 print (type (a * B) # The two integers are multiplied by each other, and they are still integer 10 print (type (a * c) # Add floating point type, that is, it is automatically converted to float11 print (a/B) 12 print (type (a/B) 13 print (B/a) 14 print (type (B/)) # division operation. Even if Division is performed, the result is still float type.
Note: If the print (1/0) divisor is 0, an error is returned instead of NaN. Be sure to check the exception when the divisor is 0. If you need to handle NaN, you need to import from decimal *
Decimal numbers include special values such as NaN which stands for "Not a number", positive and negative Infinity, and-0
Remainder operation, exponential power operation, absolute value, rounding
1 print (3% 2) # usually perform the remainder operation between integers. 2 print (type (3% 2) 3 print (type (3.0% 2 )) # It is not recommended that the floating point type get the remainder 4 print (2.1% 2) # Although float can also calculate the remainder, but the result has precision, the result is 0.10000000000000009 5 print (3 ** 2) # ** indicates the power operation, that is, the power operation 6 print (type (3 ** 2) # The integer power is still integer 7 print (2 **-2) 8 print (type (2 **-2) 9 print (1 **-2) 10 print (type (1 **-2 )) # The negative power is float11 print (4*0.5) 12 print (type (4*0.5) # The non-integer power is float13 print (pow )) # Another expression of the exponential power 14 print (abs (-1) # Take the absolute value 15 print (round (3.5) # rounding to take the entire 16 print (round (3.49 )) # rounded to 17 print (round (3.49, 1) # Add a parameter to show the number of digits after the decimal point. The preceding result is 3.518 print (round (3.04, 1 ))
Number comparison
Comparison operator =! ==>=<<=
1 a = 12 B = 1.03 print (a = B) # returns True if the value is compared.
Hexadecimal number
The binary number starts with 0 B, the hexadecimal number starts with 0 o (zero and lowercase o), and The hexadecimal number starts with 0x. bin () is in the form of binary output.
1 a = 0b11100 #2 base number 0b start 2 B = 0o34 #8 base number 0o start 3 c = 0x1c #16 base number 0x start 4 print (a, B, c) 5 print (bin (2) # bin () indicates binary output
Bitwise operation
Like many languages, Python bitwise operators include <>>&| ~ ^
1 print (bin (0b110 <2) # shift left 2 digits 2 print (bin (0b110> 1 )) # shift one digit to the right 3 print (0 & 1) # & computation 4 print (0 | 1) # | or operation 5 print (0 ^ 1) # ^ exclusive or operation 6 print (bin (~ 0b11 ))#~ Non-calculation, inverse of the number of symbols
Math Module
Complex mathematical computing needs to be imported into the Mathematical Module, that is, import math. Only some common constants and functions in the math module are listed. For details, refer to the official help documentation.
1 import math 2 print (math. e) # mathematical constant e 3 print (math. pi) # mathematical constant pi 4 print (math. ceil (3.00001) # rounded up to 5 print (math. floor (3.99999) # Round down 6 print (math. sqrt (9) # square root math. sqrt (x) = x ** 0.5 returns the same float Type 7 print (math. exp (1) # exp (n) math. e's n 8 print (math. log (math. e) # The Ln operation is a natural constant e (2. 71828 ......) 9 print (math. log2 (16) # Log210 print (math. log10 (1) # Log1011 print (math. degrees (math. pi) # Converts angle x from radians to degrees.12 print (math. radians (60) # Converts angle x from degrees to radians.13 print (math. sin (math. radians (30) #14 print (math. cos (math. pi/3 ))
Random Module
The random module can generate multiple random numbers. Here, we only introduce randint: random integers within the generation range. If you need other random number methods, refer to the official help documentation for details.
random.randint(a, b)
Return a random integer N such that a <= N <= b.
1 from random import randint2 for I in range (): # indicates making ten cycles 3 print (randint () # output any number from 1 to 10