一. 常见的数学函数
函数名 | 描述 |
---|---|
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) | py2,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1 |
举例:
- abs和fabs的区别
- fabs()的使用需要导入数学模块(math),而fabs()不需要
- 返回类型不同
举例:
import math
a = abs(-9)
b =math.fabs(-9)
>>> a,type(a)
(9, <class 'int'>)
b,type(b)
(9.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']
-
指数和对数
-
指数的基本概念:
1、正整数指数幂:实数a自乘n次得到的实数b,b=a×a×a ×a……×a, (n $ \in $ N,且 n $> 1 ) , 称 为 实 数 a 的 n 次 幂 , n 为 自 然 数 , 数 a 的 n 次 幂 用 a n 表 示 , 记 作 1),称为实数a的n次幂,n为自然数,数a的n次幂用an表示,记作 1),称为实数a的n次幂,n为自然数,数a的n次幂用an表示,记作{\rm{b}} = {a^n} , 数 a 称 为 幂 的 底 , 数 n 称 为 幂 指 数 。 注 意 : ,数a称为幂的底,数n称为幂指数。注意: ,数a称为幂的底,数n称为幂指数。注意:{\rm{b}} = {a^1}$举例: 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 函数的返回值是布尔
-
二.随机函数(random)
-
模块导入
#方法1 import random #方法2 from random import * #不推荐使用
-
产看对应的方法
举例: 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模块常用的功能
-
random.random():用于随机生成一个0到1 的浮点数n(0<=n<=0)
举例: import random num = random.random() print(num) 0.768280680652876 print(num) 0.768280680652876 num = random.random() print(num) 0.19455255905400481
-
random.uniform(a,b):用于生成指定范围内的随机浮点数,两个参数,其中之一是上限,另一个是下限:
-
如果a>b,生成的随机数n:a<=n<=b;
-
如果a<b,生成的随机数n:b<=n<=a;
举例: import random >>> random.uniform(1,10) 4.402909648366331 >>> random.uniform(10,1) 7.3268351116196335 >>>
-
-
3.random.randint(a,b):随机生成a到b范围内的整数n(a<=n<=b)
举例: random.randint(1,10)
-
4.random.randrange([start],[stop],[step]):从指定范围内按指定技术递增的集合中获取一个随机数
集合为{start,start+step,start+2*step,…,start+n*step}
举例:random.randrange(100,30,-2)
48
-
5.random.choice(sequence):从序列中随机获取一个元素
举例: lst=['python','C','C++','javascript'] strl=('I Love u') random.choice(lst) 'C++' random.choice(lst) 'python' random.choice(lst) 'javascript'
-
6.random.shuffle(x[,random]):用于将一个列表中的元素打乱,即将列表内的元素随机排列
举例: p=['a','b','c','d','e'] random.shuffle(p p ['d', 'c', 'b', 'e', 'a'] random.shuffle(p) p ['d', 'c', 'b', 'a', 'e'] random.shuffle(p) p ['c', 'e', 'a', 'd', 'b']
-
7.random.sample(sequence,k):从指定序列中随机获取指定长度的片段并随机排列。注意:sample函数不会修改原有序列
举例: li=[1,2,3,4,5,6,7] random.sample(li,4) [2, 6, 5, 4] random.sample(li,3) [7, 1, 2] random.sample(li,5) [4, 3, 5, 6, 7] li [1, 2, 3, 4, 5, 6, 7]
三.字符串
字符串是Pyhton中最常见的数据类型。我们可以使用引号(‘或“)来创建字符串。事实上,在pyhton中,加了引号的字符都被认为是字符串!
python中单个字符和多个字符用引号包裹后,统称为字符串,没有java或者C中字符的说法
name = "骚姐姐" age = "18" age_1 = 18 msg = '''I'm Tang you''' #单引号会就近重合,使用三个单引号会避免 三单引号三双引号可以换行使用 msg = """I'm Tang you""" print(type(name),type(age),type(age_1),type(msg)) <class 'str'> <class 'str'> <class 'int'> <class 'str'>