Python—Number
变量赋值
时就创造了Number对象:
var1=1
var2=2
删除Number的引用:
del var1[var2[,var3[...[,varN]]]]
用del语句删除单个或多个对象
:
del var
del var_a,var_b
Python支持四种不同的数值类型:
整型(Int) - 通常被称为是整型或整数,是正或负整数,不带小数点。
长整型(long integers) - 无限大小的整数,整数最后是一个大写或小写的L。
浮点型(floating point real values) - 浮点型由整数部分与小数部分组成,浮点型也可以使用科学计数法表示(2.5e2 = 2.5 x 102 = 250)
复数(complex numbers) - 复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型。
注:
长整型也可以使用小写"L",但是还是建议您使用大写"L",避免与数字"1"混淆。Python使用"L"来显示长整型。
Python还支持复数,复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型
Python—Number类型转换
Python—math,cmath模块
Python 中数学运算常用的函数基本都在math 模块
、cmath 模块
中。
Python math
模块提供了许多对浮点数
的数学运算函数。
Python cmath
模块包含了一些用于复数
运算的函数。
cmath
模块的函数跟 math
模块函数基本一致,区别是 cmath 模块运算的是复数
,math 模块运算的是数学运算
。
要使用 math 或 cmath 函数必须先导入:
import math
查看 math
查看包中的内容:
>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>>
查看cmath
查看包中的内容
>>> import cmath
>>> dir(cmath)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cos', 'cosh', 'e', 'exp', 'inf', 'infj', 'isclose', 'isfinite', 'isinf', 'isnan', 'log', 'log10', 'nan', 'nanj', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau']
>>>
Python数学函数
pow() 函数
语法:
import math
math.pow(x,y)
内置pow()函数
pow(x,y[,z])
函数是计算 x 的 y 次方,如果 z 在存在,则再对结果进行取模
,其结果等效于 pow(x,y) %z
。
注意:pow() 通过内置
的方法直接调用,内置方法会把参数作为整型
,而 math 模块则会把参数转换为float
。
示例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import math # 导入 math 模块
print "math.pow(100, 2) : ", math.pow(100, 2)
# 使用内置,查看输出结果区别
print "pow(100, 2) : ", pow(100, 2)
输出结果:
math.pow(100, 2) : 10000.0
pow(100, 2) : 10000
log() 函数
语法:
import math
math.log(x[, base])
x -- 数值表达式。
base -- 可选,底数,默认为 e。
示例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import math # 导入 math 模块
print "math.log(100.12) : ", math.log(100.12)
print "math.log(100.72) : ", math.log(100.72)
print "math.log(119L) : ", math.log(119L)
print "math.log(math.pi) : ", math.log(math.pi)
print "math.log(10,2) : ", math.log(10,2)
输出结果:
math.log(100.12) : 4.60636946656
math.log(100.72) : 4.61234438974
math.log(119L) : 4.77912349311
math.log(math.pi) : 1.14472988585
math.log(10,2) : 3.32192809489
Python随机数函数
choice()函数
语法:
import random
random.choice( seq )
注意: choice()是不能直接访问的,需要导入 random
模块,然后通过random 静态对象调用该方法。
参数:
seq -- 可以是一个列表,元组或字符串。
示例:
#!/usr/bin/python
import random
print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9])
print "choice('A String') : ", random.choice('A String')
输出结果:
choice([1, 2, 3, 5, 9]) : 2
choice('A String') : n
randrange() 函数
语法:
import random
random.randrange ([start,] stop [,step])
注意: randrange()是不能直接访问的,需要导入random
模块,然后通过 random 静态对象调用该方法。
参数:
start -- 指定范围内的开始值,包含在范围内。
stop -- 指定范围内的结束值,不包含在范围内。
step -- 指定递增基数。
示例:
#!/usr/bin/python
import random
# 输出 100 <= number < 1000 间的偶数
print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2)
# 输出 100 <= number < 1000 间的其他数
print "randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3)
输出结果:
randrange(100, 1000, 2) : 976
randrange(100, 1000, 3) : 520
Python三角函数
Python数学常量
pi 数学常量 pi(圆周率,一般以π来表示)
e 数学常量 e,e即自然常数(自然常数)