python字符串作为函数参数_python字符串函数

数学函数

1.abs(x)

abs(x)函数返回 x(数字)的绝对值,如果参数是一个复数,则返回它的大小。

ContractedBlock.gif

ExpandedBlockStart.gif

1 #abs(x)函数返回数字的绝对值。2 #语法 abs(x)3 #!/usr//bin/env python

4 # -*- coding:utf-8 -*-

5 # Author:xiaoweicheng6

7 print ("abs(-40) : ", abs(-40))8 print ("abs(100.10) : ", abs(100.10))

View Code

2.ceil(x) 函数返回一个大于或等于 x 的的最小整数。

ContractedBlock.gif

ExpandedBlockStart.gif

1 import math #导入 math 模块

2

3 print ("math.ceil(-45.17) :", math.ceil(-45.17))4 print ("math.ceil(100.12) :", math.ceil(100.12))5 print ("math.ceil(100.72) :", math.ceil(100.72))6 print ("math.ceil(math.pi) :", math.ceil(math.pi))

View Code

3.exp(x) 方法返回x的指数,ex

ContractedBlock.gif

ExpandedBlockStart.gif

1 import math #导入 math 模块

2

3 print ("math.exp(-45.17) :", math.exp(-45.17))4 print ("math.exp(100.12) :", math.exp(100.12))5 print ("math.exp(100.72) :", math.exp(100.72))6 print ("math.exp(math.pi) :", math.exp(math.pi))

View Code

4.fabs(x)返回数字的绝对值

fabs() 方法返回数字的绝对值,如math.fabs(-10) 返回10.0。

fabs() 函数类似于 abs() 函数,但是他有两点区别:

abs() 是内置函数。 fabs() 函数在 math 模块中定义。

fabs() 函数只对浮点型跟整型数值有效。 abs() 还可以运用在复数中。

ContractedBlock.gif

ExpandedBlockStart.gif

1 import math #导入 math 模块

2

3 print ("math.fabs(-45.17) :", math.fabs(-45.17))4 print ("math.fabs(100.12) :", math.fabs(100.12))5 print ("math.fabs(100.72) :", math.fabs(100.72))6 print ("math.fabs(math.pi) :", math.fabs(math.pi))

View Code

5.floor()函数返回数字的下舍整数,小于或等于 x

ContractedBlock.gif

ExpandedBlockStart.gif

1 import math #导入 math 模块

2

3 print ("math.floor(-45.17) :", math.floor(-45.17))4 print ("math.floor(100.12) :", math.floor(100.12))5 print ("math.floor(100.72) :", math.floor(100.72))6 print ("math.floor(math.pi) :", math.floor(math.pi))

View Code

6.log() 方法返回x的自然对数,x > 0。

ContractedBlock.gif

ExpandedBlockStart.gif

1 import math #导入 math 模块

2

3 print ("math.log(100.12) :", math.log(100.12))4 print ("math.log(100.72) :", math.log(100.72))5 print ("math.log(math.pi) :", math.log(math.pi))

View Code

7.log10() 方法返回以10为基数的x对数,x>0。

ContractedBlock.gif

ExpandedBlockStart.gif

1 import math #导入 math 模块

2

3 print ("math.log10(100.12) :", math.log10(100.12))4 print ("math.log10(100.72) :", math.log10(100.72))5 print ("math.log10(119) :", math.log10(119))6 print ("math.log10(math.pi) :", math.log10(math.pi))

View Code

8.max() 方法返回给定参数的最大值,参数可以为序列。

ContractedBlock.gif

ExpandedBlockStart.gif

1 print ("max(80, 100, 1000) :", max(80, 100, 1000))2 print ("max(-20, 100, 400) :", max(-20, 100, 400))3 print ("max(-80, -20, -10) :", max(-80, -20, -10))4 print ("max(0, 100, -400) :", max(0, 100, -400))

View Code

9.min() 方法返回给定参数的最小值,参数可以为序列。

ContractedBlock.gif

ExpandedBlockStart.gif

1 print ("min(80, 100, 1000) :", min(80, 100, 1000))2 print ("min(-20, 100, 400) :", min(-20, 100, 400))3 print ("min(-80, -20, -10) :", min(-80, -20, -10))4 print ("min(0, 100, -400) :", min(0, 100, -400))

View Code

10.modf() 方法返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。

ContractedBlock.gif

ExpandedBlockStart.gif

1 import math #导入 math 模块

2

3 print ("math.modf(100.12) :", math.modf(100.12))4 print ("math.modf(100.72) :", math.modf(100.72))5 print ("math.modf(119) :", math.modf(119))6 print ("math.modf(math.pi) :", math.modf(math.pi))

View Code

11.pow() 方法返回 xy(x的y次方) 的值。

ContractedBlock.gif

ExpandedBlockStart.gif

1 import math #导入 math 模块

2

3 print ("math.pow(100, 2) :", math.pow(100, 2))4 #使用内置,查看输出结果区别

5 print ("pow(100, 2) :", pow(100, 2))6 print ("math.pow(100, -2) :", math.pow(100, -2))7 print ("math.pow(2, 4) :", math.pow(2, 4))8 print ("math.pow(3, 0) :", math.pow(3, 0))

View Code

12.round() 方法返回浮点数x的四舍五入值。

ContractedBlock.gif

ExpandedBlockStart.gif

1 print ("round(70.23456) :", round(70.23456))2 print ("round(56.659,1) :", round(56.659,1))3 print ("round(80.264, 2) :", round(80.264, 2))4 print ("round(100.000056, 3) :", round(100.000056, 3))5 print ("round(-100.000056, 3) :", round(-100.000056, 3))

View Code

13.sqrt() 方法返回数字x的平方根

ContractedBlock.gif

ExpandedBlockStart.gif

1 import math #导入 math 模块

2

3 print ("math.sqrt(100) :", math.sqrt(100))4 print ("math.sqrt(7) :", math.sqrt(7))5 print ("math.sqrt(math.pi) :", math.sqrt(math.pi))

View Code

你可能感兴趣的:(python字符串作为函数参数)