• 数据类型


    基本的数据类型——数字

           布尔型:

                          1. bool型只有两个值:True和False

                          2. 之所以将布尔值归为数字,因为我们习惯用1 表示True,0表示False。

          整型

          浮点型

          复数

    基本的数据类型——字符串

                         字符串是一个有序的字符的集合,用于存储和表示基本的文本信息,' '或'' ''或''' '''中间包含的内容称之为字符串

                         字符串的单引号和双引号都无法取消特殊字符的含义,如果想让引号内所有字符均取消特殊意义,在引号前面加r,如name=r'l hf'

    基本的数据类型——列表

                         定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素

                         

    常用操作:

    1. 索引

    1 color = ["red","blue","green","white"]
    2 索引:
    3 print('color[0]')

    2. 切片

    1 color = ["red","blue","green","white"]
    2 print("color[0:2]")
    3 >>>red,blue
    4 
    5 print("color[1:3]")
    6 >>>blue,green

    倒着切
    color = ["red","blue","green","white"]
    color[-1:-5:-1]

    字符串也可以反转
    n = "进击的小吴"
    n[::-1]
    >>>n = "吴小的击进"

    3. 追加

    1 color = ["red","blue","green","white"]
    2 color.append("black")
    3 >>>color = ["red","blue","green","white","black"]

    插入
    color = ["red","blue","green","white"]
    color.insert(2,"pink")
    print(color.insert(2,"pink"))
    >>>
    color = ["red","blue","pink","green","white"]
    
    

    4. 删除

    1 color = ["red","blue","green","white"]
    2 color.remove("white")  #删除从左到右的第一个值
    3 
    4 color = ["red","blue","green","white"]
    5 color.pop()
    >>>"white"
    6 >>>color = ["red","blue","green"]

    color = ["red","blue","green","white"]
    print("del color[red]")
    >>>color = ["blue","green","white"]

    5. 长度

    1 len(color)
    2 >>>3

    6. 包含和循环

    >>> 'red' in color
    True
    >>> 'pink' in color
    False
    
    #循环:为什么是“i”?
    >>> for i in color:
        print(i)

    7. 排序

    1 a = [2,3,4,9,7,15]
    2 a.sort()
    3 print(a.sort())
    >>>a = [2,3,4,7,9,15]

     8. 查询

    1 color = ["red","blue","green","white"]
    2 color.index("blue")
    3 >>>print(color.index("blue"))
    4 >>>1

    color = ["red","blue","green","white"]
    color.count("red")
    >>>1
                  
  • 相关阅读:
    Android url中文编码问题
    android studio error configuration with name default not found
    Android studio 配置JNI环境
    Github获取仓库最新Release版本号API
    微信小程序开发——设置默认图片、错误加载图片
    微信小程序开发-rem转换rpx小工具
    微信小程序开发小技巧——单击事件传参、动态修改样式、轮播样式修改等
    解决vue-router嵌套路由(子路由)在history模式下刷新无法渲染页面的问题
    VUE图片懒加载-vue lazyload插件的简单使用
    vue项目微信分享之后路由链接被破坏怎么办
  • 原文地址:https://www.cnblogs.com/jiajin-wu/p/11723540.html
Copyright © 2020-2023  润新知