zoukankan      html  css  js  c++  java
  • Python列表操作与深浅拷贝(5)——数字处理函数、类型判断、列表链表队列栈

    python内建数据结构

    分类

    数值型:  int  float  complex  bool

    序列对象: 字符串str  列表list  元组tuple

    键值对:  集合set  字典dict

    数值型 (list float complex bool都是class)

    int:python3 中 int 就是长整型,没有大小限制

    float:支持十进制和科学计数法表示,由C的双精度型实现

    complex:由实数和虚数部分组成,实部和虚部都是浮点数

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

    类型转换(built-in)

    int(x)返回一个整数

    int(7)
    7

    float(x)

    float(7)
    7.0

    complex(x),complex(x,y)返回一个复数

    complex(7),complex(7,8)
    ((7+0j), (7+8j))

    bool(x)返回布尔值

    bool(7)
    True

    数字的函数处理

    int()截取整数部分

    print('正数:',int(7.01),int(7.49),int(7.50),int(7.51),int(7.99),'复数:',int(-7.01),int(-7.49),int(-7.50),int(-7.51),int(-7.99))
    正数: 7 7 7 7 7 复数: -7 -7 -7 -7 -7

    // 向下取整

    print('正数:',int(7//2),int(7//3),int(7//4),int(7//5),int(7//6),'复数:',int(-7//2),int(-7//3),int(-7//4),int(-7//5),int(-7//6))
    正数: 3 2 1 1 1 复数: -4 -3 -2 -2 -2

    floor()向下取整

    import math    #导入math模块
    print('正数:',math.floor(7.01),math.floor(7.49),math.floor(7.50),math.floor(7.51),math.floor(7.99))
    print('复数:',math.floor(-7.01),math.floor(-7.49),math.floor(-7.50),math.floor(-7.51),math.floor(-7.99))
    正数: 7 7 7 7 7
    复数: -8 -8 -8 -8 -8

    ceil()向上取整

    import math
    print('正数:',math.ceil(7.01),math.ceil(7.49),math.ceil(7.50),math.ceil(7.51),math.ceil(7.99))
    print('复数:',math.ceil(-7.01),math.ceil(-7.49),math.ceil(-7.50),math.ceil(-7.51),math.ceil(-7.99))
    正数: 8 8 8 8 8
    复数: -7 -7 -7 -7 -7

    round()四舍,六入,五取最近的偶数

    print('正数:',round(7.01),round(7.49),round(6.50),round(7.50),round(7.51),round(7.99))
    print('复数:',round(-7.01),round(-7.49),round(-6.50),round(-7.50),round(-7.51),round(-7.99))
    正数: 7 7 6 8 8 8
    复数: -7 -7 -6 -8 -8 -8

    min()取最小

    两种表示方法

    min(range(5))
    min(0,1,2,3,4,5)

    max()取最大

    pow(x,y)平方,等同于x**y

    pow(2,3)
    8

    math.sqrt()开方,等同于x**0.5

    import math
    math.sqrt(2)
    1.4142135623730951

    类型判断

    type可以判断对象类型,type(a)返回值为类型,不是字符串

    type(7),type(7.1),type(bin(7)),type(False)
    (int, float, str, bool)

    isinstance(obj,class_or_tuple)判断类型,返回值为布尔,可以判断子类

    isinstance(7,str)
    False
    isinstance(7,(str,bool,int))
    True
    1 + True + 2.2    #隐式类型转换,整型向浮点型转换
    4.2

    列表list、链表、queue、stack

    list 使用[ ]表示,列表是可变的

    一个队列,开辟连续的内存空间

    列表内的个体称为元素,元素可以是任意对象(数字,字符串,对象,列表等),元素在列表内依序排列,可以使用索引

    列表查询速度快,改变数据对性能影响大

    链表是可变的,在内存中随机排列

    单向链表:有下一位的内存地址

    双向链表:有上一位和下一位的内存地址

    queue队列:不可变数据,先进先出

    stack栈:不可变数据,后进先出

  • 相关阅读:
    对象的方法
    uni-app运行
    flex取值
    阿里矢量库使用
    移动端vue项目模板
    微信分享--转载
    页面流动条
    uni-app打包
    Hibernate~DAO
    EL表达式
  • 原文地址:https://www.cnblogs.com/omgasw/p/11598219.html
Copyright © 2011-2022 走看看