加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python内置数据结构

发布时间:2020-12-20 10:59:48 所属栏目: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就是长整型,且没有大小限制,受限

一、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‘>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读