常见的数字函数
函数名 | 描述 |
---|---|
abs(x) | 返回数字的绝对值,如abs(-10) 返回 10 |
fabs(x) | 返回数字的绝对值,如math.fabs(-10) 返回10.0 |
ceil(x) | 返回数字的上入整数,如math.ceil(4.1) 返回 5 |
floor(x) | 返回数字的下舍整数,如math.floor(4.9)返回 4 |
round(x [,n]) | 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。 |
exp(x) | 返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045 |
log(x) | 如math.log(math.e)返回1.0,math.log(100,10)返回2.0 |
log10(x) | 返回以10为基数的x的对数,如math.log10(100)返回 2.0 |
max(x1, x2,…) | 返回给定参数的最大值,参数可以为序列。 |
min(x1, x2,…) | 返回给定参数的最小值,参数可以为序列。 |
modf(x) | 返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。 |
pow(x, y) | x**y 运算后的值。 |
sqrt(x) | 返回数字x的平方根 |
cmp(x, y) | 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1 |
举例:
- abs和fabs的区别:
- fabs()的使用需要导入数学模块(math),而abs()不需要。
- fabs()和abs()返回的数据类型不同,abs()返回的是整型,fabs()返回的是浮点型。
>>> b=fabs(-21)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'fabs' is not defined
(错误)
>>> abs(-21)
21
>>> import math (导入数学模块)
>>> a=abs(-9) (整型)
>>> b=math.fabs(-12) (浮点型)
>>>
KeyboardInterrupt
>>> a,type(a)
(9, <class 'int'>)
>>> b,type(b)
(12.0, <class 'float'>)
>>>
拓展dir()和dir(参数):
- dir()不带参数,查看当前环境下所有的变量名称。
- dir(参数),返回对应的属性或者方法。
>>> import math
>>> dir(math)
['__doc__', '__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']
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'math']
>>> from math import *
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__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', 'math', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>>
指数
- 指数的基本概念:
- 正整数指数幂:实数a自乘n次得到的实数b,b=a*a*a*a…*a(n∈N,且n>1)称为实数a的n次幂,n为自然数,数a的n次幂用 b = a n {\rm{b}} = {a^n} b=an表示,记作 b = a n {\rm{b}} = {a^n} b=an,数a称为幂的底,数n称为幂指数。注意: b = a 1 {\rm{b}} = {a^1} b=a1
>>>import operator
然后:
>>> operator.eq("a","a");
True
lt(a,b) 相当于 a<b 从第一个数字或字母(ASCII)比大小
less than
le(a,b)相当于a<=b
less and equal
eq(a,b)相当于a==b 字母完全一样,返回True,
equal
ne(a,b)相当于a!=b
not equal
gt(a,b)相当于a>b
greater than
ge(a,b)相当于 a>=b
greater and equal
函数的返回值是布尔
python2使用,python3已取消
随机函数
-
模块导入
# 方法1 >>> import random # 方法2 >>> from random import * (不推荐使用)
-
查看对应的方法和属性
>>> import random >>> dir(random) ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_itertools', '_log', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate'] >>>
-
random模块常用的功能
- 1.random.random():用于随机生成一个0到1的浮点数n(0<=n<=1)
>>> import random
>>> num=random.random()
>>> print(num)
0.7993767512776093
>>> num=random.random()
>>> print(num)
0.6414719430881838
>>>
- 2.random.uniform(a,b);用于生成指定范围内的随机数,其中之一是上限,另一个是下限:
- 如果a>b,生成的随机数n:a<= n <=b;
- 如果a<b,生成的随机数n:b<= n <=a;
>>> import random
>>> random.uniform(10,1)
2.3134409917065106
>>> random.uniform(10,5)
9.126252038489847
>>>
-
3.random.randint(a,b):随机生成a到b范围内的整数n(a<= n <=b):
>>> import random >>> random.randint(1,5) 4 >>> random.randint(1,10) 6 >>>
-
4.random.randrange([start],[stop],[step]);从指定范围内,按指定基数递增的集合中获取一个随机数。
集合为{start,start+step,start+2step,…,start+nstep}
>>> random.randrange(10,30,2) 18 >>> random.randrange(50,30,-2) 48 >>> random.randrange(50,30,-5) 35 >>> random.randrange(10,50,5) 35 >>>
-
5.random.choice(sequence):从序列中随机获取一个元素:
>>> str=('adada fcxda love') >>> random.choice(str) 'd' >>> lst=('afafa','dad','tsdsdq','rgdsfqwd') >>> random.choice(lst) 'dad' >>>
-
6.random.shuffle(x[,random]):用于将一个列表中的元素打乱,即将列表的元素随机排列。
>>> p=['A','B','C','D','E'] >>> random.shuffle(p) >>> p ['D', 'A', 'C', 'B', 'E'] >>> p=['A','B','C','D','E'] >>> random.shuffle(p) >>> p ['C', 'A', 'B', 'E', 'D']
-
7.random.sample(sequence,k):从指定序列中随机获取指定长度的片段并随机排列,注意:
sample函数不会修改原有序列。
>>> li=[12,3,4,64,6,4,46,] >>> random.sample(li,5) [6, 4, 46, 12, 64] >>> random.sample(li,6) [12, 3, 6, 4, 4, 46] >>> random.sample(li,3) [64, 4, 12] >>>
字符串
什么是字符串
字符串是Python中最常用的数据类型。我们可以使用引导(‘或“)来创建字符串。事实上,在Python中,加了引导的字符都被认为是字符串!
Python中单个字符串多个字符用引导包裹后,统称为字符串,没有java或者c中字符串的说法
mag='''I' m Chen xie'''
print(mag)
mag1="""I' m Chen xie"""
print(mag1)
mag2="""
轻轻的我走了,
正如我轻轻地来;
我轻轻的招手,
作别西天的云彩。
"""
print(mag2)
# I' m Chen xie
# I' m Chen xie
# 轻轻的我走了,
# 正如我轻轻地来;
# 我轻轻的招手,
# 作别西天的云彩。