本篇内容主要讲解“Python数字类型有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python数字类型有哪些”吧! 一、数字Python中数字类型包括: 整数 浮点数 复数 固定精度的十进制数 有理分数 集合 布尔类型 无穷的整数精度 各种数字内置函数和模块
二、内置数学工具表达式操作符: | 操作符 | 描述 |
|---|
| yield | 生成器函数发送协议 | | lambda args:expression | 生成匿名函数 | | x if y else z | 三元表达式 | | x or y | 逻辑或(存在短路算法) | | x and y | 逻辑与(存在短路算法) | | not x | 逻辑非 | | x in y , x not in y | 成员关系 | | x is y ,x is not y | 对象实体测试 | | x<y,x<=y,x>y,x>=y,x==y,x!=y | 比较大小 | | x|y | 位或,集合并集 | | x^y | 位异或,集合对称差 | | x&y | 位与,集合交集 | | x<<y,x>>y | 左移或者右移y位 | | x+y,x-y | 加减法、合并删除 | | x*y,x%y,x/y,x//y | 乘,取余数,除,地板除 | | -x,+x | 一元减法 | | ~x | 按位求补(取反) | | x**y | 幂运算 | | x[i] | 索引,函数调用 | | x[i:j:k] | 分片 | | x(...) | 调用函数 | | x.attr | 调用属性 | | () | 元组,表达式,生成器 | | [] | 列表,列表解析 | | {} | 字典,集合,集合和字典解析 |
内置数学函数
math
| 函数|常量 | 说明 | 实例 |
|---|
| e | 自然常数e | math.e 2.718281828459045
| | inf | 默认值:inf |
| | nan | 默认值:nan |
| | pi | 圆周率π | math.pi 3.141592653589793
| | tau | 6.283185307179586 |
| | acos(x) | 返回x的反三角余弦值 | math.acos(math.sqrt(2)/2) 0.7853981633974483
| | acosh(x) | 返回x的反双曲余弦函数 |
| | asin(x) | 返回x的反三角正弦值 | math.asin(0.5) 0.5235987755982989
| | asinh(x) | 返回x的反双曲正弦函数 |
| | atan(x) | 返回x的反三角正切值 | math.atan(1.7320508075688767) 1.0471975511965976
| | atan2(y, x) | 返回x/y的反三角正切值 | math.atan2(2,1) 1.1071487177940904 | | atanh(x) | 返回x的反双曲正切函数 |
| | ceil(x) | 这个方法对i向上取整 | math.ceil(5.2) 6.0
| | copysign(x, y) | 若y<0,返回-1乘以x的绝对值; 否则,返回x的绝对值
| math.copysign(5.2, -1) -5.2
| | cos(x) | 返回x(弧度)的三角余弦值 | math.cos(math.radians(45)) 0.7071067811865476
| | cosh(x) | 返回x的双曲余弦函数 |
| | degrees(x) | 弧度转度 | math.degrees(math.pi) 180.0
| | erf(x) | 返回x的误差函数 |
| | erfc(x) | 返回x的余误差函数 |
| | exp(x) | 返回e的x次方 | math.exp(2) 7.38905609893065
| | expm1(x) | 返回e的x次方减1 | math.expm1(2) 6.38905609893065
| | fabs(x) | 返回x的绝对值 | math.fabs(-5) 5.0
| | factorial(x) | 返回x的阶乘 | math.factorial(5) 120
| | floor(x) | 向下取整。 | math.floor(5.8) 5.0
| | fmod(x, y) | 返回x%y(取余) | math.fmod(5,2) 1.0
| | frexp(x) | 返回m和i,满足m乘以2的i次方 | math.frexp(3) (0.75, 2)
| | fsum(iterable) | 返回无损精度的和 |
| | gamma(x) | 返回x的伽玛函数 |
| | hypot(x, y) | 返回以x和y为直角边的斜边长 | math.hypot(3,4) 5.0
| | isinf(x) | 若x为无穷大,返回True;否则,返回False |
| | isnan(x) | 若x不是数字,返回True;否则,返回False | math.isnan(1.2e3) False
| | ldexp(x, i) | 返回x乘以2的i次方 | math.ldexp(0.75, 2) 3.0
| | lgamma(x) | 返回x的绝对值的自然对数的伽玛函数 |
| | log(x, base=None) | 返回x的以base为底的对数,base默认为e |
| | log10(x) | 返回x的以10为底的对数 | math.log10(2) 0.30102999566398114
| | log1p(x) | 返回1+x的自然对数(以e为底) | math.log1p(math.e-1) 1.0
| | log2(x) |
|
| | modf(x) | 返回x的小数和整数 | math.modf(5.2) (0.20000000000000018, 5.0)
| | pow(x, y) | 返回x的y次方 | math.pow(5,3) 125.0
| | radians(x) | 度转弧度 | math.radians(45) 0.7853981633974483
| | sin(x) | 返回x(弧度)的三角正弦值 | math.sin(math.radians(30)) 0.49999999999999994
| | sinh(x) | 返回x的双曲正弦函数 |
| | sqrt(x) | 返回x的平方根 | math.sqrt(3) 1.7320508075688772
| | tan(x) | 返回x(弧度)的三角正切值 | math.tan(math.radians(60)) 1.7320508075688767
| | tanh(x) | 返回x的双曲正切函数 |
| | trunc(x) | 返回x的整数部分 | math.trunc(5.8) 5
|
math.log(math.e) 1.0
math.log(2, 10) 0.30102999566398114
math.isinf(1.0e+308) False
math.isinf(1.0e+309) True
0.1+0.2+0.3 0.6000000000000001 math.fsum([0.1, 0.2, 0.3]) 0.6
random
| 函数|常量 | 说明 | 实例 |
|---|
| random | 用于生成一个0到1的随机符点数: 0 <= n < 1.0 |
| | uniform | random.uniform的函数原型为:random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成的随机数n: a <= n <= b。如果 a <b, 则 b <= n <= a。 | print random.uniform(10, 20) print random.uniform(20, 10) #---- 结果(不同机器上的结果不一样) #18.7356606526 #12.5798298022
| | randint | random.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b | print random.randint(12, 20) #生成的随机数n: 12 <= n <= 20 print random.randint(20, 20) #结果永远是20 #print random.randint(20, 10) #该语句是错误的。下限必须小于上限。
| | randrange | random.randrange的函数原型为:random.randrange([start], stop[, step]),从指定范围内,按指定基数递增的集合中 获取一个随机数。 | random.randrange(10, 100, 2) 结果相当于从[10, 12, 14, 16, ... 96, 98]序列中获取一个随机数。 在结果上与 random.choice(range(10, 100, 2) 等效。
| | choice | random.choice从序列中获取一个随机元素。其函数原型为:random.choice(sequence)。参数sequence表示一个有序类型。这里要说明 一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。list, tuple, 字符串都属于sequence。 | print random.choice("学习Python") print random.choice(["JGood", "is", "a", "handsome", "boy"]) print random.choice(("Tuple", "List", "Dict"))
| | shuffle | random.shuffle的函数原型为:random.shuffle(x[, random]),用于将一个列表中的元素打乱。 | p = ["Python", "is", "powerful", "simple", "and so on..."] random.shuffle(p) print p #---- 结果(不同机器上的结果可能不一样。) #['powerful', 'simple', 'is', 'Python', 'and so on...']
| | sample | random.sample的函数原型为:random.sample(sequence, k),从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列。 | list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] slice = random.sample(list, 5) #从list中随机获取5个元素,作为一个片断返回 print slice print list #原有序列并没有改变。
|
三、分数from fractions import Fraction x = Fraction(1, 3) 1/3 到此,相信大家对“Python数字类型有哪些”有了更深的了解,不妨来实际操作一番吧!这里是天达云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
|