Python常用的一些内建函数和math模块函数

一:Python内建函数

 1 # abs取绝对值
 2 num = -10
 3 print(abs(num))
 4 
 5 # max 求最大值
 6 print(max(6, 9, 2, 12, 8))
 7 
 8 # min求最小值
 9 print(min(-1, 2, 5, 0, 7))
10 
11 print(min([1, 3, 7, 0, 2]))
12 
13 # round 计算一个数值的四舍五入的
14 
15 pi = 3.14
16 print(round(pi))
17 
18 # round(number[, ndigits])
19 # ,ndigits
20 # 表示四舍五入的位数,可以省略
21 
22 pi = 3.147
23 print(round(pi, 2))
24 
25 # pow(x,y)
26 # 返回x的y次幂
27 # x**y
28 
29 print(pow(2, 3))

输出结果为:

10
12
-1
0
3
3.15
8

二:math模块函数

 1 # 导入模块math
 2 import math
 3 
 4 # ceil(x)函数返回一个大于或等于 x 的的最小整数。 也叫上取整
 5 
 6 pi = 3.14
 7 print(round(pi))
 8 
 9 print("math.ceil(pi)::", math.ceil(pi))
10 
11 
12 # floor(x) 返回数字的下舍整数,小于或等于 x。 也叫向下取整
13 print("math.floor(pi)::", math.floor(pi))
14 
15 
16 # sqrt() 方法返回数字x的平方根。
17 print("math.sqrt(16)::", math.sqrt(16))
18 
19 # log(x,base) 以base为基数  x为对数
20 
21 print("math.log(10000, 10)", math.log(10000, 10))
22 # 10的多少次方是10000

输出结果为:

3
math.ceil(pi):: 4
math.floor(pi):: 3
math.sqrt(16):: 4.0
math.log(10000, 10) 4.0

math的一些三角函数:

 

 1 # 正弦函数
 2 # sin() 返回的x弧度的正弦值。
 3 # 返回的x弧度的正弦值,数值在 -1 到 1 之间。
 4 
 5 result = math.sin(30)
 6 print("sin(30)::", result)
 7 
 8 print("sin(math.pi/2)::", math.sin(math.pi/2))
 9 
10 
11 # degrees() 将弧度转换为角度。
12 # 以下是 degrees() 方法的语法:
13 # import math
14 #
15 # math.degrees(x)
16 
17 # degrees()是不能直接访问的,需要导入 math 模块,然后通过 math 静态对象调用该方法。
18 
19 print("degress(3)::", math.degrees(3))
20 
21 print("degress(math.pi/2)::", math.degrees(math.pi/2))
22 
23 print("degrees(math.pi/4) : ",  math.degrees(math.pi/4))
24 
25 
26 # radians() 方法将角度转换为弧度。
27 # 以下是 radians() 方法的语法:
28 # import math
29 #
30 # math.radians(x)
31 
32 # radians()是不能直接访问的,需要导入 math 模块,然后通过 math 静态对象调用该方法。
33 
34 print("radians(3) : ",  math.radians(3))
35 print ("radians(math.pi/2) : ",  math.radians(math.pi/2))

输出结果为:

sin(30):: -0.9880316240928618
sin(math.pi/2):: 1.0
degress(3):: 171.88733853924697
degress(math.pi/2):: 90.0
degrees(math.pi/4) : 45.0
radians(3) : 0.05235987755982989
radians(math.pi/2) : 0.027415567780803774

 三:随机函数

# random() 方法返回随机生成的一个实数,它在[0,1)范围内。0可以取到,但是1是取不到的
# import random
# random.random()
# random()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。

import random
print("random() :", random.random())

# choice() 方法返回一个列表,元组或字符串的随机项。
# choice的用法
# import random
# random.choice( seq  )
# seq -- 可以是一个列表,元组或字符串

# 注意:choice()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。

print("choice用法::", random.choice([1, 3, 9, 2, 6, 8]))

print("choice('A String') : ", random.choice('A String'))


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

# 以下是 uniform() 方法的语法:
# import random
#
# random.uniform(x, y)
# x -- 随机数的最小值。
# y -- 随机数的最大值。

# 注意:uniform()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。

print("uniform-->2到8之间的随机浮点数::", random.uniform(2,8))


# randint()方法将随机生成一个整数,它在[x,y]范围内  注意这是随机出来的一个整数
print("randint-->2到8之间随机出来的一个整数::", random.randint(2, 8))


# randrange() 方法返回指定递增基数集合中的一个随机数,基数缺省值为1
# 以下是 randrange() 方法的语法:
# import random
#
# random.randrange ([start,] stop [,step])   (stop取不到)

# start -- 指定范围内的开始值,包含在范围内。
# stop -- 指定范围内的结束值,不包含在范围内。
# step -- 指定递增基数。

# 注意:randrange()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。

# 从 1-100 中选取一个奇数
print("randrange(1,100, 2) : ", random.randrange(1, 100, 2))

# 从 0-99 选取一个随机数
print("randrange(100) : ", random.randrange(100))

输出结果为:

random() : 0.2922334094703323
choice用法:: 3
choice('A String') :
uniform-->2到8之间的随机浮点数:: 2.8683731762130527
randint-->2到8之间随机出来的一个整数:: 8
randrange(1,100, 2) : 87
randrange(100) : 84

原文地址:https://www.cnblogs.com/cxq0017/p/9316842.html