Numeric constants
python supports the following types of digital
- Integer (positive and negative integers): python integer having infinite precision
- Float
- 0x123 hexadecimal number
- Octal number 0o123
Binary 0b1010101
The basic numeric constants
- 1234,-2,0,999999
- 1.23,1., 3.14e-10,4E210,4.0e + 210 # 4e210 is the 4 * 10 ^ 210
- Plural: 3 + 4j
- In case the judge sentences the wrong stop, will not judge the statement following
- Formatting of decimal places
print('{:.2}'.format(12.11234)) 1.2e+01
division
- x / y: traditional division, the fractional part will be retained
- x // y: floor divider, the fractional part will be removed
- x% y: modulo division, modulo
In addition to the cut-off difference except the floor
- math.floor (-5/2)> -3 increased fractional part discarded
math.trunc (-5/2)> -2 simply to retain the integer part
Special hexadecimal number
- oct (64)> 0100 is converted into a string of octal
- hex (64)> 0x40 converted to a string of 16 hexadecimal
- bin (64)> 0b1000000 converted to binary string
int (x, y)> y x to the hexadecimal number, y in [2,8,10,16]
Bit operating
- << similar exponentiation x = 1, x << 2> x = 4
-
Similar to the square root x = 4, x >> 2, x = 2
Some methods of math
- math.sin
- Math.PI
- math.e
- math.sqrt () square root
- power pow 4 (2,4) 2
- ads () absolute value
- sum()
max&min
random module
import random
random.random() #随机生成一个数字
random.choice([1,2,3,4,5]) #从列表中随机选出一个数字
random.randint(1,10) #从1到10随机产生一个数字,括号左右均包括