Python3常用的基本数学函数

import math
import random

#绝对值  abs
print(abs(-10))

#最大最小值  max  min
print(max(1,2,3,7,5,4))
print(min(5,7,8,3,1,8))

# x的n次方 pow
print(pow(10,2))
print((pow(10,-2)))

#四舍五入 round
print(round(3.146))
print(round(3.546))
print(round(3.145,2))

#向上取整  math.ceil
print(math.ceil(18.1))
print(math.ceil(18.9))

#向下取整  math.floor
print(math.floor(18.1))
print(math.floor(18.9))

#返回一个数的整数和小数部分(返回的是一个元组) math.modf
print(math.modf(21.3))

#开方  math,sqrt
print(math.sqrt(16))


#随机数

#第一种:从序列的元素中随机一个元素
print(random.choice([1,2,3,4,5,6,7,8,9]))
#第二种:在(0,1,2,3,4)中随机一个数
print(random.choice(range(10)))
#第三种:在字符串种随机一个字符
print(random.choice("dashdghj"))

#产生1-100之间的数
print(random.choice(range(100))+1)
print(random.choice(range(1,101)))

#在一个范围内随机产生
print(random.randrange(1,100,2))   #产生一个随机奇数,2是步距(递增的间距),即:1,3,5,7,9。。。

#随机生成一个【0,1)的数
print(random.random())  #一个0到1之间的数,不包含1但包含0

#将序列的所有元素打乱,随机排序
l = [1,2,3,4,5,6]
random.shuffle(l)
print(l)

#随机生成一个范围内的实数
print(random.uniform(3,9))

#三角函数
print(math.sin(45))
print(math.cos(45))

运行结果:

10
7
1
100
0.01
3
4
3.15
19
19
18
18
(0.3000000000000007, 21.0)
4.0
4
9
j
5
11
73
0.2215736702858121
[1, 3, 6, 4, 5, 2]
3.700471056603738
0.8509035245341184
0.5253219888177297

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿尔兹

如果觉得有用就推荐给你的朋友吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值