python的数据结构分类,以及数字的处理函数,类型判断

python的数据结构分类:

数值型

  int:python3中都是长整形,没有大小限制,受限内存区域的大小

  float:只有双精度型

  complex:实数和虚数部分都是浮点型,1+1.2J

  bool:int的子类,仅有2个实例,True、False对应1和0,可以整数直接运算

序列对象

  字符串 str、列表list、tuple

键值对

  集合set、字典dict

数字的处理:

round():四舍六入,五找偶数

In [1]: round(1.5)   
Out[1]: 2
In [2]: round(1.6)
Out[2]: 2
In [3]: round(1.4)
Out[3]: 1
In [4]: round(2.4)
Out[4]: 2
In [5]: round(2.5)
Out[5]: 2
In [6]: round(2.6)
Out[6]: 3

floor()地板、ceil()天花板

In [1]: import math
In [2]: math.floor(2.5)
Out[2]: 2
In [3]: math.floor(3.7)
Out[3]: 3
In [4]: math.floor(4.2)
Out[4]: 4
In [5]: math.ceil(2.3)
Out[5]: 3
In [6]: math.ceil(2.7)
Out[6]: 3
In [7]: math.ceil(7.1)
Out[7]: 8

min()max()

In [1]: min(1,2,3,7,9)
Out[1]: 1
In [2]: min(3,6,8,2,9)
Out[2]: 2
In [3]: max(1,2,3,7,9)
Out[3]: 9
In [4]: max(3,6,8,2)
Out[4]: 8

pow(x,y) 等价于x**y   x的y次方

In [5]: pow(2,3)
Out[5]: 8
In [6]: pow(3,4)
Out[6]: 81
In [7]: pow(5,5)
Out[7]: 3125
In [8]: 3**4
Out[8]: 81

math.sqrt()  开立方

In [10]: import math
In [11]: math.sqrt(3)
Out[11]: 1.7320508075688772
In [12]: math.sqrt(9)
Out[12]: 3.0

进制函数返回值是字符串

bin()转为二进制、oct()转为八进制、hex()转为十六进制

In [13]: bin(10)
Out[13]: '0b1010'
In [14]: oct(10)
Out[14]: '0o12'
In [15]: hex(10)
Out[15]: '0xa'

math.pi  圆周率   math.e 自然常数

In [16]: math.e
Out[16]: 2.718281828459045
In [17]: math.pi
Out[17]: 3.141592653589793

类型判断 

type(obj),返回类型,而不是字符串

In [1]: a=5
In [2]: type(a)
Out[2]: int
In [3]: b="ok"
In [4]: type(b)
Out[4]: str
In [5]: type(type(a))
Out[5]: type
In [6]: type(b) == str
Out[6]: True

 

isinstance(obj,class_or_tuple),返回布尔值,第二个参数可以为一个类或者一个tuple

In [1]: a=5
In [2]: b="ok"
In [3]: isinstance(a,int)
Out[3]: True
In [4]: isinstance(a,str)
Out[4]: False
In [5]: isinstance(b,str)
Out[5]: True
In [6]: isinstance(b,(str,int,bool))
Out[6]: True

 

posted on 2016-12-05 09:34  Left-knife  阅读(419)  评论(0编辑  收藏  举报

导航