Python类型和运算--数字

在Python中,数字并不是一个真正的对象类型,而是一组相似类型的分类。不仅包括通常的数字类型(整数和浮点数),黑包括数字表达式,以及高级的数字编程。


基本数字常量

数字                                常量

1234, -24                         整数(无穷大小)

1.23, 3.14e10                  浮点数

0177, 0x9ff, 0b1010          Python2.6中八进制,十六进制,二进制常量

0o177, 0x9ff, 0b1010        Python3.4中八进制,十六进制,二进制常量


1. 整数和浮点数常量、

整数以十进制字符串出现,浮点数带有一个小数点,可以用科学记数法表达(1.0e10或1.0E10)


2. 十六进制,八进制和二进制常量

十六进制以0x或0X开头,八进制以0或0o开头,二进制以0b或0B开头。

内置函数hex(I),oct(I),bin(I)分别可以表示这三种进制。另外,int(str, base)可以把字符串str转换为以base为进制的整数。


3. 复数

实部 + 虚部:2+3j,5+2J

内置函数complex(real, imag)来创建复数


4. 编辑其他数字类型

通过调用导入模块的函数来创建,例如分数。


内置数学工具和扩展

表达式操作符:+ - * ** / >> &等

内置数学工具:pow abs round int hex bin

round(number, ndigit)

>>>round(3.1415926,2)
3.14
>>>round(3.1415926)#默认为0
3
>>>round(3.1415926,-2)
0.0
>>>round(3.1415926,-1)
0.0
>>>round(314.15926,-1)
310.0

公用模块:random match

例如:

print random.randint(1220)  #生成的随机数n: 12 <= n <= 20   print random.randint(2020)  #结果永远是20   #print random.randint(20, 10)  #该语句是错误的。下限必须小于上限。 

常用方法:

is_integer, is_digit


OperationResultNotesFull documentation
x + ysum of x and y  
x - ydifference of x and y  
x * yproduct of x and y  
x / yquotient of x and y  
x // yfloored quotient of x andy(1) 
x % yremainder of x / y(2) 
-xx negated  
+xx unchanged  
abs(x)absolute value or magnitude ofx abs()
int(x)x converted to integer(3)(6)int()
float(x)x converted to floating point(4)(6)float()
complex(re, im)a complex number with real partre, imaginary part im.im defaults to zero.(6)complex()
c.conjugate()conjugate of the complex numberc  
divmod(x, y)the pair (x // y, x % y)(2)divmod()
pow(x, y)x to the power y(5)pow()
x ** yx to the power y(5) 

表达式和操作符

OperationResultNotes
x | ybitwise or of x andy 
x ^ ybitwise exclusive or ofx and y 
x & ybitwise and of x andy 
x << nx shifted left by n bits(1)(2)
x >> nx shifted right by n bits(1)(3)
~xthe bits of x inverted 


变量和基本表达式

1. 变量在第一次创建时赋值

2. 变量在表达式中使用将被替换为它们的值

3. 变量在表达式中使用以前必须被赋值

4. 变量像对象一样不需要在一开始进行声明

也就是说,这些赋值会让变量a和b自动生成:

%python

>>> a = 3

>>> b = 4

数字显示的格式与C语言很相似

>>> num = 1/3.0

>>> '%e'%num

'3.333333e-01'


除法:传统除法,Floor除法和真除法

'Python 2.6:X/Y 表示传统除法,10/4==2;X//Y Floor除法,10/4 == 2

Python 3.0: X/Y表示真除, 10/4 == 2.5;X//Y Floor除法,10//4 == 2


位操作

与C语言相似,

>>> x = 1

>>> x >> 2

4

>>> x | 2

3

>>> x & 1

1

小数数字

由于浮点型缺乏精确性,因为用来存储数值的空间有限,例如,

>>> 0.1 + 0.1 + 0.1 - 0.3

5.551115123125783e-17

可以是用小数对象

>>> from decimal import Decimal

>>> Decimal(0.1) + Decimal(0.1) + Decimal(0.1) - Decimal(0.3)

Decimal('0.0')

还可以用来设置全局精度

>>> import decimal

>>> decimal.getcontext().prec = 4


分数类型

>>> from fractions import Fraction

>>> x = Fraction(1,3)

>>> (2.5).as_integer_ratio()

(5, 2)


集合

Python 2.4 引入了一种新的类型--集合(set),这种类型和数学上的集合有着本质的相似

1. Python 2.x的集合

>>> x = set('abcde')
>>> y = set('bdxyz')
>>> x
set(['a', 'c', 'b', 'e', 'd'])
>>> y
set(['y', 'x', 'b', 'd', 'z'])

>>> 'e' in x

True

>>> x - y
set(['a', 'c', 'e'])
>>> x&y
set(['b', 'd'])
>>> x^y
set(['a', 'c', 'e', 'y', 'x', 'z'])
>>> x|y
set(['a', 'c', 'b', 'e', 'd', 'y', 'x', 'z'])

set可以进行添加删除操作这里不再赘述

2. Python 3.x 的集合

>>> x = set('abcde')
>>> y = set('bdxyz')
>>> x
{'a', 'b', 'c', 'd', 'e'}
这个集合看起来更像是无值的字典
>>> {x**2 for x in [1,2,3,4]}
{16, 1, 9, 4}


转载于:https://www.cnblogs.com/wujiazhong/p/9526990.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值