python 3ベースチュートリアルノート

4664 ワード

第一章クイックハンド:基礎知識
  • 問題:unicodeとutf-8とassciの関係
  • 第二章リストとメタグループ
  • リスト要素接合:''.join(lst)
  • リストメソッド
  • #  :
    lst.append(a)  #    
    lst.insert(3, 'n')  #   
    
    #  
    lst.clear()  #   
    lst.pop()  #    
    lst.remove('a')  #          
    
    #   
    b = lst.copy()
    
    #   
    lst.count(n)
    
    #    (    a=a+b)
    a.extend(b)
    
    #   
    a.index('n')
    
    #   
    x.reverse()  #       
    x.sort()  #       
    

    第三章文字列
    1、文字列フォーマット
    #    
    >>> "i am a %s" % 'boy'
    i am a boy
    
    #   
    >>> "i am a %s %s" % ('little', 'boy')
    i am a little boy
    >>> "i am a {0} {1}".format('little', 'boy')
    i am a little boy
    >>> "i am a {a} {b}".format(a="little", b="boy")
    i am a little boy
    
    #   
    >>> phonebook
    {'Beth':'9102', 'Alice':'2341'}
    >>> "Beth's phone number is {Beth}".format_map(phonebook)
    Beth's phone number is 9102
    

    2、精度、幅
    #       
    >>> "the number is %.2f" % 1
    the number is 0.02
    
    #      +  
    >>> "the number is {:10.2f}".format(2)
    'the number is       2.00'
    

    3、文字列の方法
    # join   
    #          
    >>> '+'.join(['a','b'])
    'a+b'
    
    # replace   
    >>> 'This is a test'.replace('This', 'That')
    'That is a test'
    
    # split   
    #         
    >>> '1+2+3+4'.split('+')
    ['1', '2', '3', '4']
    
    # strip       
    #      ,       
    >>>'    i am a boy     '.strip()
    'i am a boy'
    >>> '**! i am a boy!!**  '.strip('*!')
    ' i am a boy!!**  '