基础:Python 数字类型变量

温故而知新

Python 可以自动转换类型,我们称之为动态类型,本文主要是介绍数字类型的变量和方法。

➜  www python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a=34
>>> type(a) # a 是 init 型
<type 'int'>
>>> a=3.4
>>> type(a) # a 是 float 型
<type 'float'>
>>> 0.3*3 # 会丢失精度
0.8999999999999999
>>> print 0.3*3 # print 会把结果人性化的显示
0.9
>>> 0.3/3
0.09999999999999999
>>> print 0.3/3
0.1
>>> from decimal import Decimal as D #调用 decimal 来解决精度问题
>>> D('0.3')*D(3)
Decimal('0.9')
>>> print D('0.3')*D(3)
0.9
>>> D('0.3')/D(3)
Decimal('0.1')
>>> print D('0.3')/D(3)
0.1

其中from decimal import Decimal as D 是指调用 decimal 库并把 decimal 设置为别名 D

>>> 1/2**10000 # 数字太大无法显示
  0L
  >>> 1.0/2**10000 #直接报错了
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  OverflowError: long int too large to convert to float
  >>>

要表示1的10000次方

>>> D(1)/D(2**10000)
Decimal('5.012372749206452009297555934E-3011')

decimal 的缺点就是执行时间比 float 长

➜  www python -mtimeit -s "from decimal import Decimal as D" "D('1.2')+D('3.4')"
10000 loops, best of 3: 24.7 usec per loop #  decimal 的时间最长
➜  www python -mtimeit -s "from decimal import Decimal as D" "1.2+3.4"
100000000 loops, best of 3: 0.0157 usec per loop #两个 float 直接相加时间最段
➜  www python -mtimeit -s "from decimal import Decimal as D" "float('1.2')+float('3.4')"
1000000 loops, best of 3: 0.338 usec per loop # 加了字符串转换也会增加时间

和数字相关的库

1.math

>>> import math
>>> math.pi 
3.141592653589793
>>> math.sqrt(80)
8.94427190999916
>>> math.log10(2**1000)
301.0299956639812
>>> math.pow(1,5) 
1.0
>>> math.factorial(5)
120

2.random

>>> import random
>>> random.random()
0.1797989878351397
>>> random.choice([1,2,3])
2
>>> random.randint(2,5)
2
>>> random.uniform(4,5)
4.374124742261563
>>> random.gauss(2,34)
87.84062004735456

3.dir

>>> dir(math)
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

4.help

>>> help(math)

random.random()可以生成一个0-1之间的随机数,比如说我们希望产生一个概率中奖概率20%,那么可以random.random()>0.8 当他是 true 就是中奖了,否则没中奖。

>>> random.random()
0.6586755279600274
>>> random.random()
0.7146088640324266
>>> random.random()
0.9007269952467781
>>> random.random()
0.9163585110005992
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
True
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
True
>>> random.random()>0.8
False
>>> random.random()>0.8
True
>>> random.random()>0.8
True
>>>

科学计算

1.numpy 产生数组和矩阵,正态分布的随机数,矩阵运算,举证求逆,转置等操作
2.scipy 拟合,线性插值,样条插值,积分,微分,触非线性方程,滤波器设计