python的其他函数(一)

Python随机数函数

random.choice函数

import random
print("the number is",random.choice([1,2,3,4,5,6,5,6,9,6,7,]))

输出结果:
the number is 6

the number is 9

the number is 7

the number is 2

the number is 6

the number is 5

每次运行该程序得出的结果都是随机的

random.randrange模块

import math
# 输出 100 <= number < 1000 间的偶数
print "the number is ", random.randrange(100, 1000, 2)

# 输出 100 <= number < 1000 间的其他数
print "the number is: ", random.randrange(100, 1000, 3)

random.random函数:在0-1之间随机生成一个数

import random
 
# 生成第一个随机数
print "random() : ", random.random()
 
# 生成第二个随机数
print "random() : ", random.random()

shuffle() 方法将序列的所有元素随机排序

import random

list = [20, 16, 10, 5]
random.shuffle(list)
print "随机排序列表 : ",  list

uniform() 方法将随机生成下一个实数,它在 [x, y] 范围内。

import random
 
print "uniform(5, 10) 的随机数为 : ",  random.uniform(5, 10)
 
print "uniform(7, 14) 的随机数为 : ",  random.uniform(7, 14)

运行结果:
uniform(5, 10) 的随机数为 : 6.98774810047
uniform(7, 14) 的随机数为 : 12.2243345905

三角函数

print "sin(3) : ",  math.sin(3)
print "cos(3) : ",  math.cos(3)
print "tan(3) : ",  math.tan(3)

acos(x) 返回x的反余弦弧度值。
asin(x) 返回x的反正弦弧度值。
atan(x) 返回x的反正切弧度值。
atan2(y, x) 返回给定的 X 及 Y 坐标值的反正切值。
cos(x) 返回x的弧度的余弦值。
hypot(x, y) 返回欧几里德范数 sqrt(xx + yy)。
sin(x) 返回的x弧度的正弦值。
tan(x) 返回x弧度的正切值。
degrees(x) 将弧度转换为角度,如degrees(math.pi/2) , 返回90.0
radians(x) 将角度转换为弧度

都是可以直接导入变量x的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值