python3 math,random

import math
import random


# 数学常量
# π :3.141592653589793
print(math.pi)

# e :2.718281828459045
print(math.e)

# 数学函数
# 绝对值
print(abs(-12))   #12
print(abs(-12.0)) #12.0

# 浮点数向上取整,返回数字的上入整数,如math.ceil(4.1) 返回 5
print(math.ceil(4.1)) #5

# 浮点数向下取整,返回数字的下舍整数,如math.floor(4.9)返回 4
print(math.floor(4.9)) #4

# 返回e的x次幂(e^x),如math.exp(1) 返回2.718281828459045
print(math.exp(3))

# 返回以2为基数的对数
print(math.log2(64))

# 返回以10为底的对数, lg(N)
print(math.log10(1000))

# 返回以e为底的对数值, ln(N)
print(math.log(math.e))
print(math.log(1000))

# 返回以自然数为底的对数值 x=logₐ(N), ₐ:对数的底数, N:真数, x:以ₐ为底N的对数
print(math.log(1000, 3)) #以3为底1000的对数

# 返回以e为底(1+x)的对数值;  log1p(e) = log(1+e) = 1.3132616875182228
print(math.log1p(math.e))
print(math.log(1+math.e))

# 平方根 √x
print(math.sqrt(4))

# 幂运算xʸ: x的y次方
print(math.pow(2, 3))

# 截断出整数部分, trunc(3.9) = 3
print(math.trunc(3.9))

# 数学表达式:Γ(x+1)=xΓ(x),  gamma(x+1) = x*gamma(x)
print(math.gamma(5+1))      # 120.0
print(5 * math.gamma(5))    # 120.0



# 递归求gcd函数
# def gcd(a,b):
#     if a%b == 0:
#         return b
#     else :
#         return gcd(b,a%b)

# HCF(highest common factor):最高公因子  or  GCD(greatest common divisor):最大公约数
n = 60
m = 48
print(math.gcd(n, m)) # 12

# LCM(least common multiple):最小公倍数
print(math.lcm(n, m))

# 公式 n*m = lcm(n, m) * gcd(n, m)
print(n*m)
print(math.lcm(n, m) * math.gcd(n, m))




# python 三角函数
# 正弦
print(math.sin(45))
# 余弦
print(math.cos(45))
# 正切
print(math.tan(45))

# 反正弦
print(math.asin(1))
# 反余弦
print(math.acos(1))
# 反正切
print(math.atan(1))

# 反双曲正弦值的双曲正弦
print(math.asinh(0.13))
# 反双曲余弦值的双曲余弦
print(math.acosh(1.23))
# 反双曲正切值的双曲正切
print(math.atanh(-0.28))

# 给定x,y坐标值的反正切值
print(math.atan2(2, 4))

# 双曲函数
# sinhx = [ e^x - e^(-x) ] / 2,x∈duR
# coshx = [ e^x + e^(-x) ] / 2,x∈R
# tanhx = sinhx / coshx = [ e^x - e^(-x) ] / [ e^x + e^(-x) ],x∈R
# 反双曲函数zhi(双曲函数的反函数)
# arsinhx = ln[ x + √( x^2 + 1 ) ],x∈R
# arcoshx = ln[ x + √( x^2 - 1 ) ],x∈[1,+∝)
# artanhx = ln[ √( 1 - x^2 ) / ( 1 - x ) ] = (1/2) ln[ ( 1 + x ) / ( 1 - x ) ],x∈[-1,1)


# hypot 欧几里德范数sqrt(x^2+y^2+...n^2), sqrt(x*x + y*y)
print(math.hypot(2, 3, 4, 5))


# 弧度是角的度量单位, 定义:弧长等于半径的弧,其所对的圆心角为1弧度,单位rad。
# 弧度 = 弧长/半径
# 1弧度 = 180/π = 57.295779513082320876798154814105 ≈ 57.3rad
# 1角度 = π/180 = 0.01745329251994329576923690768489 ≈ 0.01745°
# π弧度  = 180°
# 2π弧度 = 360°
# 一周的弧度数为2πr/r=2π,360°角=2π弧度
# 角度转弧度    弧度=(PI/180)*角度
# 弧度转角度    角度=(180/PI)*弧度  

# 弧度转角度
print(math.degrees(math.pi))

#角度转弧度
print(math.radians(10))


# python 随机函数
# random
# 给随机数一个种子值,默认是系统时钟
random.seed(1234567890)

# 生成一个[0, 1.0]之间的随机小数
random.random()

# uniform(a, b) 生成一个[a, b]之间的随机小数
random.uniform(0, 100)

# randint(a, b) 生成一个[a, b]之间的随机整数
random.randint(0, 100)

# randrange(a, b, c) 生成一个[a, b]之间以c递增的随机数
random.randrange(0, 100, 2)

# choice(<list>) 从列表中随机返回一个元素
list = [0,1,2,3,4,5,6,7,8,9,0]
random.choice(list)

# shuffle(<list>) 将列表中元素随机打乱
random.shuffle(list)

# sample(<list, k>) 从指定列表中随机获取k个元素
random.sample(list, 3)


猜你喜欢

转载自blog.csdn.net/u013420428/article/details/112251478