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) | Py2 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1 |
##举例 abs和fabs的区别:
- fabs的使用需要导入数学模块(math),而abs不需要;
- 返回的数据类型不同。
#没有导包
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'fabs' is not defined
>>> b=fabs(-9)
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
指数和对数
指数的基本概念
-
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}
cmp()函数
python2使用,Python3已取消
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
函数的返回值是布尔
随机函数(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模块常用的功能
1、random.random();
random.random(); 用于随机生产一个0-1的浮点数n(0<=n<=1)
import random
a=random.random();
print(a)
#0.5691313076770075
2、random.uniform(a,b);
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.008290561490691
3、random.randint(a,b)
- random.randint(a,b) :随机生成a到b范围内的整数n(a<=n<=b)
>>> random.uniform(1,10)
8.61135235919285
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))
##常用的字符串操作汇总如下:
1、去除空格以及一些特殊符号(’#‘,’,‘,’|'等):
删除字符串两边的空格:str.strip()
删除字符串末尾的空格:str.lstrip()
删除字符串左边的空格:str.rstrip()
2、字符串分割(默认为空白字符):
str.split()
3、字符串查找:
str.find() #返回的是索引位置
4、计算字符串长度:
len(str)
5、字符串大小写:
str.upper() #字母大写
str.lower() #字母小写
str.capitalize() #首字母大写
str.istitle() #是否是首字母大写的
str.isupper() #字母是否便是大写
str.islower() #字母是否全是小写