Python的数字函数随机函数指数对数
Python的数字函数随机函数指数对数
常见的数字函数
函数名 | 描述 |
---|---|
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 |
ads 和 fabs 的区别
- fads()需要导入数学模块;
- ads()不需要;
- 返回的数据类型不同 ads()返回 int,fads()返回float;
#没有导包
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'fabs' is not defined
>>> 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']
eq.equal判断是否相等
lt:less than
指数和对数
-
指数的基本概念
-
-
幂的运算法则
-
对数的基本概念
-
对数的性质
-
对数的恒等式和对数的换底公式
-
-
对数的运算规律
-
-
常用对数和自然对数
cmp()函数: -
Python2有使用,3已经取消;
-
Python3中
>>>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
函数的返回值是布尔
-
导入模块
#方法一 import radom #方法二 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模块常用的功能
1.random.random()
用于随机生产一个0-1的浮点数n(0<=n<=1)
import random
a=random.
ndom();
print(a)
#0.5691313076770075
2.random.uniform(a,b)
用于生成指定范围内的随机浮点数,两个参数,其中之一是上限,另一个是下限:
-
如果a>b,生成的随机数n:a<=n<=b;
-
如果a<b,生成的随机数n:b<=n<=a;
>>> import random >>> random.uniform(1,10) 8.61135235919285 >>> random.uniform(10,1) 3.00829056149069
3.random.randint(a,b)
随机生成a~b范围的整数(a<=n<=b)
>>>random.randint(1,10)
4、random.randrange([start],[stop],[step])
random.randrange([start],[stop],[step]);指定范围内,按指定基数递增的集合中获取一个随机数
-
集合为{start,start+step,start+2*step,…,start+n*step}
>>> random.randrange(10,30,2) 22
5、random.choice(sequence)
random.choice(sequence);从序列中随机获取一个元素
>>> lst=['python','c','c++']
>>> str1=('I Love Python')
>>> random.choice(lst)
'c'
>>> random.choice(str1)
'I'
>>> random.choice(str1)
'e'
>>> random.choice(str1)
' '
>>> random.choice(str1)
'n'
>>> random.choice(str1)
' '
>>> random.choice(lst)
'c'
6、random.shuffle(x[,random])
random.shuffle(x[,random]):用于将一个列表中的元素打乱,即将列表内的元素随机排列
>>> p=['A','B','C','D']
>>> random.shuffle(p)
>>> p
['D', 'A', 'B', 'C']
>>> p
['D', 'A', 'B', 'C']
>>> random.shuffle(p)
>>> p
['D', 'B', 'A', 'C']
7、random.sample(sequence,k);
random.sample(sequence,k):从指定序列中随机获取指定长度的片段并随机排列。注意:sample函数不会修改原有序列。
>>> li=[1,2,3,4,5,6,7,8]
>>> random.sample(li,5)
[3, 4, 6, 5, 2]
>>> random.sample(li,2)
[7, 8]
字符串
什么是字符串
字符串是Python中最常用的数据类型。我们可以使用引号(‘ 或 “)来创建字符串。事实上,在Python中加了引号的都是字符串
Python中单字符和多个字符用引号包括后,统称为字符串。没有Java和c语言里面的字符说法
三单引号和三双引号可以换行
>>> name="小名"
>>> age="25"
>>> age_1=15
>>> print(type(name),type(age),type(age_1))
<class 'str'> <class 'str'> <class 'int'>
msg='''I'm Xiao Ming'''
>>> print(type(msg))
msg="""I'm Xiao Ming"""
>>> print(type(msg))
msg="I'm Xiao Ming"
>>> print(type(msg))
常用的字符串操作汇总如下:
①去除空格以及一些特殊符号(’#‘,’,‘,’|'等):
删除字符串两边的空格:str.strip()
删除字符串末尾的空格:str.lstrip()
删除字符串左边的空格:str.rstrip()
②字符串分割(默认为空白字符):
str.split()
③字符串查找:
str.find() #返回的是索引位置
④计算字符串长度:
len(str)
⑤字符串大小写:
str.upper() #字母大写
str.lower() #字母小写
str.capitalize() #首字母大写
str.istitle() #是否是首字母大写的
str.isupper() #字母是否便是大写
str.islower() #字母是否全是小写