Numbers
在Python中一共有三种数字类型:
- int 整数型 (e.g. 2, 4, 20)
- bool 布尔 (e.g. False and True, acting like 0 and 1)
- float 浮点型(e.g. 5.0, 1.6)
- complex 复数型(e.g. 5+6j, 4-3j)
Basic Functions
1 | a = pow(2, 3) ## Or: 2 ** 3 |
Constants
1 | from math import e, pi |
Trigonometry三角函数
1 | from math import cos, acos, sin, asin, tan, atan, degrees, radians |
Logarithm对数
1 | from math import log, log10, log2 |
Infinity, nan
1 | from math import inf, nan, isinf, isnan |
Or:
1 | float('inf'), float('nan') |
Statistics
1 | from statistics import mean, median, variance, pvariance, pstdev |
Random
1 | from random import random, randint, choice, shuffle |
Combinatorics
- Every function returns an iterator.
- If you want to print the iterator, you need to pass it to the list() function!
1 | from itertools import product, combinations, combinations_with_replacement, permutations |
1 | 0, 1], repeat=3) product([ |
1 | """ |