• 数据类型公共方法


    数据类型公共方法

    “+” 可以用来拼接字符串、元组、列表

    >>> words1 = ["a", 'b', 'c']
    >>> wwords2 = ["d", "e", "f"]
    >>> words1 + wwords2
    ['a', 'b', 'c', 'd', 'e', 'f']
    
    >>> words1 = ("a", 'b', 'c')
    >>> words2 = ("d", "e", "f")
    >>> words1 + words2
    ('a', 'b', 'c', 'd', 'e', 'f')
    
    >>> a1 = 'words'
    >>> b1 = 'hello'
    >>> a1 + b1
    'wordshello'
    

    “-” 只能在集合

    >>> A = {'a','b','c'}
    >>> B = {'c','d','e'}
    # B的补集
    >>> A - B
    {'b', 'a'}
    

    “*” 可以用于元组、字符串、列表,不能用于字典和集合。

    # 字符串
    >>> 'a' * 3
    'aaa'
    
    # 元组
    >>> tt = ("a","b","c")
    >>> tt * 3
    ('a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c')
    
    # 列表
    >>> tt = ["a","b","c"]
    >>> tt * 3
    ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
    

    in 成员运算符,用来判断是否存在——列表、字符串、元组、字典、集合。

    >>> tt
    ['a', 'b', 'c']
    >>> "a" in tt
    True
    
    >>> tt = ("a","b","c")
    >>> "a" in tt
    True
    
    >>> tt = {"a","b","c"}
    >>> "a" in tt
    True
    
    # 字典判断的是key
    >>> persion = {"name": "aaa", "age": 23, "sex":"nam"}
    >>> "name" in persion
    True
    

    带下标的遍历

    words = ["a", 'b', 'c', 'd', 'e']
    # 可以返回一个带下标的数据。
    # 如果传入的数据是没有顺序的,返回的值也没有顺序。
    en = enumerate(words)
    for i in en:
        print(i)
    
    """
    (0, 'a')
    (1, 'b')
    (2, 'c')
    (3, 'd')
    (4, 'e')
    """
    
  • 相关阅读:
    【转】HTTP协议/IIS 原理及ASP.NET运行机制浅析【图解】
    【转】windows server 2012 R2搭建IIS服务器
    【转】新建网站(CodeFile)与新建Web应用(Codebehind)的区别
    微信小程序前端坑
    开题报告
    十大API网站
    animate使用方法
    简易音乐小程序
    如何在app.js 和其他页面中更改globalData的值
    qq音乐解析API
  • 原文地址:https://www.cnblogs.com/gshelldon/p/16414444.html
Copyright © 2020-2023  润新知