美文网首页
python语法入门二

python语法入门二

作者: 是我真的是我 | 来源:发表于2019-10-10 15:37 被阅读0次
    • 元组
    • 字符串
    • 格式化
    • 序列

    元组tuple:不可变

    arr = (1, 2, 3, 4, 5)
    print(arr)
    # (1, 2, 3, 4, 5)
    print(arr[1])
    # 2
    print(arr[:3])
    # (1, 2, 3)
    
    temp = (6)
    print(temp)
    # 6
    print(type(temp))
    # <class 'int'>
    print(type(arr))
    # <class 'tuple'>
    temp2 = (6,)
    print(type(temp2))
    # <class 'tuple'>
    temp3 = 1, 2, 3
    print(temp3)
    # (1, 2, 3)
    print(type(temp3))
    # <class 'tuple'>
    
    temp4 = temp3 + ("abc",)
    print(temp4)
    # (1, 2, 3, 'abc')
    

    字符串:不可变

    str = 'pytHoN'
    print(str.capitalize())
    # Python
    
    print(str.center(10))
    #   pytHoN  .
    
    print(str.count('y'))
    # 1
    
    print(str.endswith('N'))
    # True
    
    print(str.find('t'))
    # 2
    
    print("Hello".istitle())
    # True
    
    print("AA".join("xxx"))
    # xAAxAAx
    
    print('    .  is null     .'.lstrip())
    # .  is null     .
    
    print(str.split('H'))
    # ['pyt', 'oN']
    
    print(str.swapcase())
    # PYThOn
    
    

    格式化

    str = "{0} love {1}{2}".format('I', 'py', 'thon')
    print(str)
    # I love python
    str = "{a} love {b}{c}".format(a='I', b='py', c='thon')
    print(str)
    # I love python
    str = "{0} love {b}{c}".format('I', b='py', c='thon')
    print(str)
    # I love python
    
    print("\\a")
    # \a
    print("{0}".format("不打印"))
    # 不打印
    print("{{0}}".format("不打印"))
    # {0}
    print('{0:.1f}{1}'.format(27.452, 'GB'))
    # 27.5GB
    
    print('%c' % 97)
    # a
    print('%c %c %c' % (97, 98, 99))
    # a b c
    print('%s' % 'string')
    # string
    print('%d + %d = %d' % (4, 5, 4 + 5))
    # 4 + 5 = 9
    print('%f' % 234.4234)
    # 234.423400
    print('%10.2f' % 234.4234)
    #     234.42
    print('%+d' % 34)
    # +34
    

    序列

    print(list())
    # []
    
    str = 'string'
    str = list(str)
    print(str)
    # ['s', 't', 'r', 'i', 'n', 'g']
    
    c = (1, 2, 3, 4)
    c = list(c)
    print(c)
    # [1, 2, 3, 4]
    
    print(max(1, 2, 3, 4, 5))
    # 5
    
    arr = {1, 2, 3, 4, 5}
    print(sum(arr))
    # 15
    print(sum(arr, 10))
    # 25
    
    print(list(enumerate(arr)))
    # [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
    
    a = [1, 2, 3]
    b = [4, 5, 6, 7, 8]
    print(list(zip(a, b)))
    # [(1, 4), (2, 5), (3, 6)]
    

    相关文章

      网友评论

          本文标题:python语法入门二

          本文链接:https://www.haomeiwen.com/subject/tqzopctx.html