python内置数据结构

一、python数据结构分类

  • 数值型 int、float、complex、bool
  • 序列对象:字符串str、列表list、tuple
  • 键值对:集合set、字典dict


1、数值型

  • int,float,comlpex,bool都是class,1,5.0,2+3j都是对象即实例
  • int:python3d int就是长整型,且没有大小限制,受限与内存区域的大小
  • float:有整数部分和小数部分组成,支持十进制和科学计数法表示,只有双精度型
  • copmplex:有实数和虚数部分组成,实数和虚数部分都是浮点数,3+4.2j
  • bool:int的子类,仅有2个实例True,False对应1和0,可以和整数直接运算


2、类型转换

  • int(x) 返回一个整数
  • float(x)返回一个浮点数
  • complex(x),返回一个复数
  • bool(x)返回布尔值

3、处理数字的函数

函数有:int(), round(), floor(), ceil(), min(), max(), pow(), math.sqrt()

  1. round() 五舍六入
  • >>> round(2.5)
  • 2
  • >>> round(2.1)
  • 2
  • >>> round(2.8)
  • 3
  • >>> round(2.6)
  • 3
  1. floor() 地板向下取整,天花板ceil()向上取整
  • >>> import math
  • >>> math.floor(2.5)
  • 2
  • >>> math.floor(2.3)
  • 2
  • >>> math.ceil(2.5)
  • 3
  • >>> math.ceil(2.3)
  • 3
  1. int() 取整数部分,和//整除一样
  • >>> int(2.0)
  • 2
  • >>> int(2.1)
  • 2

4、进制函数,返回值是字符串

  • bin(),oct(),hex()

5、类型判断

  1. type(obj),返回类型,而不是字符串
  2. isinstance(obj,class_or_tuple),返回布尔值

举例:

  • >>> type('ab')
  • <class 'str'>
  • >>> type(123)
  • <class 'int'>
  • >>> isinstance(6,str)
  • False
  • >>> isinstance(6,(str,bool,int))
  • True
  • >>> type(1+True)
  • <class 'int'>
  • >>> type(1+True+2.2)
  • <class 'float'>

 

posted @ 2019-08-15 00:25  江左风华  阅读(446)  评论(0)    收藏  举报