Foreword
Python data types are not allowed to be changed, which means that if the value of the Number data type is changed, the memory space will be reallocated. Not much to say below, let’s take a look at the detailed introduction.
The following examples of Number objects will be created when variables are assigned:
var1 = 1 var2 = 10
You can also use the del statement to delete some Number object references.
You can delete single or multiple objects by using the del statement, for example:
del var del var_a, var_b
Python supports four different numeric types:
Integer (Int) - often called an integer or integer, is a positive or Negative integer without decimal point.
Long integer (long) - an integer of infinite size. The integer ends with an uppercase or lowercase L, such as: 51924361L.
Floating point type (float) -
The floating point type consists of an integer part and a decimal part. The floating point type can also be expressed using scientific notation
(2.5e2 = 2.5 x
10^2 = 250)
Complex number (complex) - A complex number consists of a real part and an imaginary part, you can use a +
bj, or complex(a,b) means that the real part a and the imaginary part b of the complex number are both floating point types.
Since I am not sure which method is required, it is best to introduce the math module when using python math functions in the future.
2. Mathematical functions that can be directly accessed:
abs(x) 返回数字的绝对值,如abs(-10) 返回 10 cmp(x, y) 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1 max(x1, x2,...) 返回给定参数的最大值,参数可以为序列。 min(x1, x2,...) 返回给定参数的最小值,参数可以为序列。 round(x [,n]) 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。
Example:
#!/usr/bin/python #coding:uft-8 import math # 导入 math 模块 print "max(80, 100, 1000) : ", max(80, 100, 1000) print "min(80, 100, 1000) : ", min(80, 100, 1000) print "round(80.23456, 2) : ", round(80.23456, 2) print "math.exp(-45.17) : ", math.exp(-45.17) print "math.pow(100, 2) : ", math.pow(100, 2)
Function Description
choice(seq)
从序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中随机挑选一个整数。
randrange ([start,] stop [,step]) 从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1
random() 随机生成下一个实数,它在[0,1)范围内。
seed([x]) 改变随机数生成器的种子seed。
shuffle(lst) 将序列的所有元素随机排序
uniform(x, y) 随机生成下一个实数,它在[x,y]范围内。
注意:
1、python的随机数函数是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。
实例:
#!/usr/bin/python # -*- coding: UTF-8 -*- import random print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9]) # 输出 100 <= number < 1000 间的偶数 print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2) # 生成第一个随机数 print "random() : ", random.random() # 生成同一个随机数 random.seed( 10 ) print "Random number with seed 10 : ", random.random() list = [20, 16, 10, 5]; random.shuffle(list) print "随机排序列表 : ", list print "uniform(5, 10) 的随机数为 : ", random.uniform(5, 10)
Python三角函数:
函数
描述
acos(x)
返回x的反余弦弧度值。
asin(x) 返回x的反正弦弧度值。
atan(x)
返回x的反正切弧度值。
atan2(y, x) 返回给定的 X 及 Y
坐标值的反正切值。
cos(x)
返回x的弧度的余弦值。
hypot(x, y) 返回欧几里德范数
sqrt(x*x + y*y)。
sin(x)
返回的x弧度的正弦值。
tan(x)
返回x弧度的正切值。
degrees(x)
将弧度转换为角度,如degrees(math.pi/2) , 返回90.0
radians(x) 将角度转换为弧度
注意:
1、Python三角函数是不能直接访问的,需要导入 math 模块,然后通过 math 静态对象调用该方法。
实例:
#!/usr/bin/python #coding: UTF-8 import math print "degrees(3) : ", math.degrees(3) print "radians(-3) : ", math.radians(-3) print "sin(3) : ", math.sin(3) print "cos(3) : ", math.cos(3) print "tan(3) : ", math.tan(3) print "acos(0.64) : ", math.acos(0.64) print "asin(0.64) : ", math.asin(0.64) print "atan(0.64) : ", math.atan(0.64) print "atan2(-0.50,-0.50) : ", math.atan2(-0.50,-0.50) print "hypot(0, 2) : ", math.hypot(0, 2)
Python数学常量:
常量 描述
pi 数学常量 pi(圆周率,一般以π来表示)
e 数学常量
e,e即自然常数(自然常数)。
注意:
1、Python数学常量也是不能直接访问的,需要导入 math 模块,然后通过 math 静态对象访问。
实例:
#!/usr/bin/python #coding: UTF-8 import math print math.pi print math.e
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能有所帮助,如果有疑问大家可以留言交流。