|
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# author : liuhefei
# Time : 2019/11/9
# desc: Python数字函数
import math
import random
if __name__ == "__main__":
# 数学函数
# 1. abs(x) 返回数字x的绝对值,如abs(-10) 返回 10 (内置函数)
# abs() 还可以运用在复数中。
print("abs(-100) = ", abs(-100))
print("abs(-3.1415) = ", abs(-3.1415))
print("abs(-4j-6) = ", abs(-4j-6))
# 2. ceil(x) 返回数字x的上入整数(向上取整),如math.ceil(4.1) 返回 5
print("math.ceil(4.1) = ", math.ceil(4.1))
print("math.ceil(-4.6) = ", math.ceil(-4.6))
print("math.ceil(100.12) = ", math.ceil(100.12))
print("math.ceil(100.72) = ", math.ceil(100.72))
# 3. exp(x) 返回e的x次幂(e^x),其中e是自然常数,如math.exp(1) 返回2.718281828459045
print("math.exp(1) = ", math.exp(1))
print("math.exp(10) = ", math.exp(10))
print("math.exp(-100.25) = ", math.exp(-100.25))
# 4. fabs(x)返回数字x的绝对值,如math.fabs(-10) 返回10.0
# fabs() 函数只对浮点型跟整型数值有效。
print("math.fabs(-23.35) = ", math.fabs(-23.35))
print("math.fabs(-123) = ", math.fabs(-123))
# 5. floor(x)返回数字的下舍整数(向下取整),其结果值小于或等于x。如math.floor(4.9)返回4
print("math.floor(4.9) = ", math.floor(4.9))
print("math.floor(4.1) = ", math.floor(4.1))
print("math.floor(-78.18) = ", math.floor(-78.18))
# 6. log(x)用于获取x的自然对数,x > 0。如math.log(math.e)返回1.0,math.log(100,10)返回2.0
print("math.log(math.e) = ", math.log(math.e))
print("math.log(10) = ", math.log(10))
# log(x, y) 用于获取x,y的对数,其中x是指数,y是底数
print("math.log(100, 10) = ", math.log(100, 10)) # 指数是底数的2次方
print("math.log(10, 10) = ", math.log(10, 10)) # 指数与底数相等
# 7. log10(x) 返回以10为基数的x的对数,其中x > 0,如math.log10(100)返回 2.0
print("math.log10(100) = ", math.log10(100)) # 底数为10,指数为100
print("math.log10(25.88) = ", math.log10(25.88))
# 8. max(x1, x2,...) 返回给定参数的最大值,参数可以为序列。(内置函数)
print("max(1, 0, -9, 23, 4, 7, -99, 8) = ", max(1, 0, -9, 23, 4, 7, -99, 8))
print("max(-9, -8, -7, -3) = ", max(-9, -8, -7, -3))
# 9. min(x1, x2,...) 返回给定参数的最小值,参数可以为序列。(内置函数)
print("min(-99, 0, -87, -20, -45, 100) = ", min(-99, 0, -87, -20, -45, 100))
print("min(10, 30, 60, 90) = ", min(10, 30, 60, 90))
# 10. modf(x) 返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。
print("math.modf(100.88) = ", math.modf(100.88))
print("math.modf(-100.12) = ", math.modf(-100.12))
# 11. pow(x, y) 返回x的y次方的值, 其中y>0 (内置函数)
print("pow(2, 4) = ", pow(2, 4))
print("pow(-2, 3) = ", pow(-2, 3))
# pow(x, y, z) 计算x的y次方,再对结果进行z取模,其结果等效于pow(x,y) %z
print("pow(2, 4, 2) = ", pow(2, 4, 2))
print("pow(4, 2, 3) = ", pow(4, 2, 3))
# 12. math.pow(x, y)内置方法会把参数作为整型,而math模块则会把参数转换为float。
print("math.pow(4, 5) = ", math.pow(4, 5))
print("math.pow(-6, 2) = ", math.pow(-6, 2))
# 13.round(x [,n])返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数(内置函数)
print("round(50.23456) = ", round(50.23456))
print("round(66.659, 1) = ", round(66.659, 1))
print("round(80.264, 2) =", round(80.264, 2))
print("round(100.000056, 3) = ", round(100.000056, 3))
print("round(-100.000056, 3) = ", round(-100.000056, 3))
# 13. sqrt(x) 返回数字x的平方根
print("math.sqrt(100) = ", math.sqrt(100))
print("math.sqrt(10) = ", math.sqrt(10))
print("\n")
# 随机函数
# 1. random.choice(seq) 返回一个列表,元组或字符串的随机项。
# 比如random.choice(range(10)),从0到9中随机挑选一个整数返回。
print("random.choice(range(10)) = ", random.choice(range(10)))
print("random.choice([2, 7, 9, 0, 1, 4, 3, 6, 5]) = ",
random.choice([2, 7, 9, 0, 1, 4, 3, 6, 5]))
print("random.choice string = ",
random.choice("天国虽美,没有了你,万杯觥筹,只不过是提醒寂寞罢了"))
print("random.choice tuple = ", random.choice(("你好", "很好笑", "好开心", "好难过")))
# 2. random.randrange ([start,] stop [,step])
# 从指定范围内,按指定基数递增的集合中获取一个随机数,基数默认值为 1
# 参数: start -- 指定范围内的开始值,包含在范围内。
# stop -- 指定范围内的结束值,不包含在范围内。
# step -- 指定递增基数。
# 从 1-100 中选取一个奇数
print("random.randrange(1,100, 2) = ", random.randrange(1, 100, 2))
print("random.randrange(100) = ", random.randrange(100)) # 从 0-99 选取一个随机数
# 返回100到1000之间的随机数
print("random.randrange(100, 1000) = ", random.randrange(100, 1000))
# 3. random.random() 随机生成下一个实数,它在[0,1)范围内。
print("random.random() : ", random.random())
# 4. random.seed([x]) 改变随机数生成器的种子seed。x -- 改变随机数生成器的种子seed。
# 其中的 x 可以是任意数字,如10,这个时候,先调用它的情况下,
# 使用 random() 生成的随机数将会是同一个。
random.seed()
print("使用默认种子生成随机数:", random.random())
print("使用默认种子生成随机数:", random.random())
random.seed(10)
print("使用整数 10 种子生成随机数:", random.random())
random.seed(100)
print("使用整数 100 种子生成随机数:", random.random())
random.seed("HelloWorld", 2)
print("使用字符串种子生成随机数:", random.random())
# 5. random.shuffle(lst) 将序列的所有元素随机排序
print("随机排序:", random.shuffle([0, 9, 7, 1, 4, 5, 6, 2, 3]))
print("随机排序:", random.shuffle([0, 9, 7, 1, 4, 5, 6, 2, 3]))
# 6. uniform(x, y) 随机生成下一个实数,它在[x,y]范围内。
print("random.uniform(10, 100) = " , random.uniform(10, 100))
print("random.uniform(-10, 10) = ", random.uniform(-10, 10))
print("\n")
# 三角函数
# 1. math.sin(x) 返回的x弧度的正弦值。数值在 -1 到 1 之间。x是一个数值。
print("math.sin(30) = ", math.sin(30))
print("math.sin(-30) = ", math.sin(-30))
print("math.sin(0) = ", math.sin(0))
print("math.sin(math.pi) = ", math.sin(math.pi))
print("math.sin(math.pi/2) = ", math.sin(math.pi / 2))
# 2. math.asin(x) 返回x的反正弦弧度值。x -- -1到1之间的数值。如果x是大于1,会产生一个错误。
print("math.asin(0.64) = ", math.asin(0.64))
print("math.asin(0) = ", math.asin(0))
print("math.asin(-1) = ", math.asin(-1))
print("math.asin(1) = ", math.asin(1))
# 3. math.cos(x) 返回x的弧度的余弦值。-1 到 1 之间, x是一个数值
print("math.cos(30) = ", math.cos(30))
print("math.cos(45) = ", math.cos(45))
print("math.cos(60) = ", math.cos(60))
print("math.cos(90) = ", math.cos(90))
print("math.cos(math.pi) = ", math.cos(math.pi))
print("math.cos(2*math.pi) = ", math.cos(2 * math.pi))
print("math.cos(0) = ", math.cos(0))
print("math.cos(-90) = ", math.cos(-90))
print("math.cos(-60) = ", math.cos(-60))
# 4. math.acos(x)返回x的反余弦弧度值。x -- -1到1之间的数值。如果x是大于1,会产生一个错误。
print("math.acos(0.64) = ", math.acos(0.64))
print("math.acos(0) = ", math.acos(0))
print("math.acos(-1) = ", math.acos(-1))
print("math.acos(1) = ", math.acos(1))
# 5. math.tan(x) 返回x弧度的正切值。数值在 -1 到 1 之间。x是一个数值。
print("math.tan(30) = ", math.tan(30))
print("math.tan(-30) = ", math.tan(-30))
print("math.tan(0) = ", math.tan(0))
print("math.tan(math.pi) = ", math.tan(math.pi))
print("math.tan(math.pi/2) = ", math.tan(math.pi / 2))
print("math.tan(math.pi/4) = ", math.tan(math.pi / 4))
# 6. math.atan(x) 返回x的反正切弧度值。x -- 一个数值。
print("math.atan(0.64) = ", math.atan(0.64))
print("math.atan(0) = ", math.atan(0))
print("math.atan(10) = ", math.atan(10))
print("math.atan(-60) = ", math.atan(-60))
print("math.atan(-1) = ", math.atan(-1))
print("math.atan(1) = ", math.atan(1))
# 7. math.atan2(y, x) 返回给定的 X 及 Y 坐标值的反正切值。x,y -- 一个数值。
print("math.atan2(-0.50,-0.50) = ", math.atan2(-0.50, -0.50))
print("math.atan2(0.50,0.50) = ", math.atan2(0.50, 0.50))
print("math.atan2(5,5) = ", math.atan2(5, 5))
print("math.atan2(-10,10) = ", math.atan2(-10, 10))
print("math.atan2(10,20) = ", math.atan2(10, 20))
# 8. math.hypot(x, y) 返回欧几里德范数 sqrt(x*x + y*y)。x,y -- 一个数值。
print("math.hypot(3, 2) = ", math.hypot(3, 2))
print("math.hypot(-3, 6) = ", math.hypot(-3, 6))
print("math.hypot(0, 4) = ", math.hypot(0, 4))
# 9. math.degrees(x)将弧度转换为角度,返回一个角度值。如degrees(math.pi/2) , 返回90.0
print("math.degrees(30) = ", math.degrees(30))
print("math.degrees(-30) = ", math.degrees(-30))
print("math.degrees(0) = ", math.degrees(0))
print("math.degrees(math.pi) = ", math.degrees(math.pi))
print("math.degrees(math.pi/2) = ", math.degrees(math.pi / 2))
print("math.degrees(math.pi/4) = ", math.degrees(math.pi / 4))
# 10. math.radians(x) 将角度转换为弧度,返回一个角度的弧度值。x是一个数值
print("radians(30) = ", math.radians(30))
print("radians(-30) = ", math.radians(-30))
print("radians(0) = ", math.radians(0))
print("radians(math.pi) = ", math.radians(math.pi))
print("radians(math.pi/2) = ", math.radians(math.pi / 2))
print("radians(math.pi/4) = ", math.radians(math.pi / 4))
|