<dd id="sga4y"><optgroup id="sga4y"></optgroup></dd>
<nav id="sga4y"><code id="sga4y"></code></nav>
  • <optgroup id="sga4y"></optgroup>

    Previous Page

    Up One Level

    Next Page

    Python ???

    Contents

    ?????2. ???Python?????? ?????Python ??? ??? 4. ??????


    ????



     
    3. Python
    ??????????

    ???????????孝?????????????????????????????????“>>??“.. ????????????宏???????????????????????????????快???孝?????????????????孝???????????????????????????????快??????????????????????????????????????????????妊?

    ??????快???????????????????????宏???????????????妊?Python?快?????????“#”?????????????快??汕???????????????快??????????????????????????????????????????孝???????快?#???????#???

    ?????

    # this is the first comment
    SPAM = 1                 # and this is the second comment
                             # ... and now a third!
    STRING = "# This is not a comment."

     
    3.1
    ???????Python

    ???????????宏????Python????????????????????????“>>????????辰?????????

     
    3.1.1
    ???

    ?????????????????????????????????????????????????????????????????????????????+??-??*??/???????????快??‾??????????C??Pascal?????????????????鉸

    >>> 2+2
    4
    >>> # This is a comment
    ... 2+2
    4
    >>> 2+2  # and a comment on the same line as code
    4
    >>> (50-5*6)/4
    5
    >>> # Integer division returns the floor:
    ... 7/3
    2
    >>> 7/-3
    -3

    ??c?????????“=”????????????????????????????????

    >>> width = 20
    >>> height = 5*9
    >>> width * height
    900

    ?????????????????????????

    >>> x = y = z = 0  # Zero x, y and z
    >>> x
    0
    >>> y
    0
    >>> z
    0

    Python?????????????????????????????????????????????????????????????

    >>> 3 * 3.75 / 1.5
    7.5
    >>> 7.0 / 2
    3.5

    ??????????????????????????“j”????“J”??????????戒??????????????“(real+imagj)??????????????“complex(real, imag)??????????

    >>> 1j * 1J
    (-1+0j)
    >>> 1j * complex(0,1)
    (-1+0j)
    >>> 3+1j*3
    (3+3j)
    >>> (3+1j)*3
    (9+3j)
    >>> (1+2j)/(1+1j)
    (1.5+0.5j)

    ??????????????????????????????????????? z.real ?? z.imag ???????z??????????

    >>> a=1.5+0.5j
    >>> a.real
    1.5
    >>> a.imag
    0.5

    ??????????????????????????float(), int() ?? long()?????????????????????????????????????????????????????abs(z)??????????????????z.real????????????

    >>> a=3.0+4.0j
    >>> float(a)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: can't convert complex to float; use e.g. abs(z)
    >>> a.real
    3.0
    >>> a.imag
    4.0
    >>> abs(a)  # sqrt(a.real**2 + a.imag**2)
    5.0
    >>>

    ???????㏒??????汐????????????_?????妊?????汎???Python?????????????????????????????????????????????鉸

    >>> tax = 12.5 / 100
    >>> price = 100.50
    >>> price * tax
    12.5625
    >>> price + _
    113.0625
    >>> round(_, 2)
    113.06
    >>>

    ???????????????????????????????????????????????Python????完?????????????????????????????????

     
    3.1.2
    ?????

    ?????????Python?????????????????????????????????????????????????????

    >>> 'spam eggs'
    'spam eggs'
    >>> 'doesn\'t'
    "doesn't"
    >>> "doesn't"
    "doesn't"
    >>> '"Yes," he said.'
    '"Yes," he said.'
    >>> "\"Yes,\" he said."
    '"Yes," he said.'
    >>> '"Isn\'t," she said.'
    '"Isn\'t," she said.'

    ?????????????????????妊????????技??忌???????????????????????????快??????????

    hello = "This is a rather long string containing\n\
    several lines of text just as you would do in C.\n\
        Note that whitespace at the beginning of the line is\
     significant."
    
    print hello

    ??????? \n ?????????忌?????????忌????newline????忱“n”???????????戒???????????????????

    This is a rather long string containing
    several lines of text just as you would do in C.
        Note that whitespace at the beginning of the line is significant.

    ??????????????????“raw”?孝?\n???抉?????????孝????????????忌?????戒?n?????????????快??????????????????

    hello = r"This is a rather long string containing\n\
    several lines of text much as you would do in C."
    
    print hello

    ???????

    This is a rather long string containing\n\
    several lines of text much as you would do in C.

    ???????????????????????????”””??'''????????????????快??????????汕????????忌??????快??????????????????妊?

    print """
    Usage: thingy [OPTIONS] 
         -h                        Display this usage message
         -H hostname               Hostname to connect to
    """

    produces the following output:

    Usage: thingy [OPTIONS] 
         -h                        Display this usage message
         -H hostname               Hostname to connect to

    ?????????????????????????????????????????????????????‾?忌?????????????????????????????????? ???????????忘??????????????????????????????????????????????????????????????????print??????????????忱?????????忌????????????

    ???????????+?????????????????????????*???????

    >>> word = 'Help' + 'A'
    >>> word
    'HelpA'
    >>> '<' + word*5 + '>'
    '<HelpAHelpAHelpAHelpAHelpA>'

    ??????????????????????????????????扭???忱??“word = 'Help' 'A'?????????????????完???百???????????????????????????

    >>> import string
    >>> 'str' 'ing'                   #  <-  This is ok
    'string'
    >>> string.strip('str') + 'ing'   #  <-  This is ok
    'string'
    >>> string.strip('str') 'ing'     #  <-  This is invalid
      File "<stdin>", line 1, in ?
        string.strip('str') 'ing'
                                ^
    SyntaxError: invalid syntax

    ????????????㊣????????????????C????????????????????㊣???0????????忪???????????????????????妊?????????????????Icon????????????????????????????????????????????e????????????

    >>> word[4]
    'A'
    >>> word[0:2]
    'He'
    >>> word[2:4]
    'lp'

    ??????????????????????????????????0??????????????????????????????????

    >>> word[:2]    # The first two characters
    'He'
    >>> word[2:]    # All but the first two characters
    'lpA'

    ??C??????????Python??????????忱?????????????????????????

    >>> word[0] = 'x'
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: object doesn't support item assignment
    >>> word[:1] = 'Splat'
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: object doesn't support slice assignment

    ????????????????完????????????米????????

    >>> 'x' + word[1:]
    'xelpA'
    >>> 'Splat' + word[4]
    'SplatA'

    ????????????????????????? s[:i] + s[i:] ????s??

    >>> word[:2] + word[2:]
    'HelpA'
    >>> word[:3] + word[3:]
    'HelpA'

    ???????????????????????????????????????????????妊??????????????????????

    >>> word[1:100]
    'elpA'
    >>> word[10:]
    ''
    >>> word[2:1]
    ''

    ???????????????????????????????鉸

    >>> word[-1]     # The last character
    'A'
    >>> word[-2]     # The last-but-one character
    'p'
    >>> word[-2:]    # The last two characters
    'pA'
    >>> word[:-2]    # All but the last two characters
    'Hel'

    ????-0????0??????????????????????

    >>> word[-0]     # (since -0 equals 0)
    'H'

    ???????????????????????????????????????????????????????????

    >>> word[-100:]
    'HelpA'
    >>> word[-10]    # error
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IndexError: string index out of range

    ??????????‾????????????????????????????????????????0????????快?n????????????????n?????鉸

     +---+---+---+---+---+ 
     | H | e | l | p | A |
     +---+---+---+---+---+ 
     0   1   2   3   4   5 
    -5  -4  -3  -2  -1

    ?????????????我?????0??5??????????竹???????????????????????i??j?????????????????????????阱?

    ?????????????????????????????????縺word[1:3]???????2??

    ???迆??? len() ??????????????

    >>> s = 'supercalifragilisticexpialidocious'
    >>> len(s)
    34

     
    3.1.3 Unicode
    ?????

    ??Python2.0??????????????????????米????????????????????Unicode ??????????????????Unicode?????????? http://www.unicode.org/ ?????????????????????????????????????快??????????????????

    Unicode????????????????????快??????????????妊? ??????????????256??????????????????????????????????????????????????????????????internationalization???????忱??“i18n????“i”+18 characters +“n”????Unicode??????????????????????????????????????

    Python?忪??????Unicode????????????????????????????

    >>> u'Hello World !'
    u'Hello World !'

    ?????妊忱??“u????????????????Unicode????????????????????????????????????Python?? Unicode-Escape ???????????????

    >>> u'Hello\u0020World !'
    u'Hello World !'

    ???I?? \u0020 ???????????竹?辰???????? 0x0020 ?? Unicode?????????????

    ?????????????????????Unicode??????????????????????????Latin-1???????????????????Unicode????????256???????Lation-1???????????????????

    ????????????????????????????????????Python??Raw-Unicode-Escape ??????????????????????????? ur ?????????妊忱“u??????志???????忌???????????宏?????? \uXXXX ????Unicode?????

    >>> ur'Hello\u0020World !'
    u'Hello World !'
    >>> ur'Hello\\u0020World !'
    u'Hello\\\\u0020World !'

    ????????????????????忌????????????????????????????

    ?????宏??????????????Python?????????????????????????????????Unicode???????

    ???迆???unicode() ??????????????????????????Unicode?????????????????????????? Latin-1, ASCII, UTF-8, ?? UTF-16????????????????????????????????byte?Unicode????? ??????????? ASCII ?????????0??127???????????????????????????????????Unicode????????????忱??????????str()????????????I????????

    >>> u"abc"
    u'abc'
    >>> str(u"abc")
    'abc'
    >>> u"äöü"
    u'\xe4\xf6\xfc'
    >>> str(u"äöü")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

    ??????Unicode??????????????????????8竹??????????????Unicode????????encode()?????????????????????????????????????????妊忱??

    >>> u"äöü".encode('utf-8')
    '\xc3\xa4\xc3\xb6\xc3\xbc'

    ??????????????????????????????????Unicode????????????????uncode()?????????????????????????????

    >>> unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8')
    u'\xe4\xf6\xfc'

     
    3.1.4
    ????

    Python ???????????????????????????????????????????????????????忱????????????????????????????????????????????????????

    >>> a = ['spam', 'eggs', 100, 1234]
    >>> a
    ['spam', 'eggs', 100, 1234]

    ???????????????????????????????????????????

    >>> a[0]
    'spam'
    >>> a[3]
    1234
    >>> a[-2]
    100
    >>> a[1:-1]
    ['eggs', 100]
    >>> a[:2] + ['bacon', 2*2]
    ['spam', 'eggs', 'bacon', 4]
    >>> 3*a[:3] + ['Boe!']
    ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!']

    ??????????????????????????????????????

    >>> a
    ['spam', 'eggs', 100, 1234]
    >>> a[2] = a[2] + 23
    >>> a
    ['spam', 'eggs', 123, 1234]

    ??????????????????????????????????妊??

    >>> # Replace some items:
    ... a[0:2] = [1, 12]
    >>> a
    [1, 12, 123, 1234]
    >>> # Remove some:
    ... a[0:2] = []
    >>> a
    [123, 1234]
    >>> # Insert some:
    ... a[1:1] = ['bletch', 'xyzzy']
    >>> a
    [123, 'bletch', 'xyzzy', 1234]
    >>> a[:0] = a     # Insert (a copy of) itself at the beginning
    >>> a
    [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]

    ???迆???len()?????????????????

    >>> len(a)
    8

    ??????????????????????忱?????????????????鉸

    >>> q = [2, 3]
    >>> p = [1, q, 4]
    >>> len(p)
    3
    >>> p[1]
    [2, 3]
    >>> p[1][0]
    2
    >>> p[1].append('xtra')     # See section 5.1
    >>> p
    [1, [2, 3, 'xtra'], 4]
    >>> q
    [2, 3, 'xtra']

    ??????????????p[1]??q????????????????????????????????????

     
    3.2
    ??????

    ??????????????Python????2??2?????????????縺????????????米????????????????Fibonacci?????快??????孝?

    >>> # Fibonacci series:
    ... # the sum of two elements defines the next
    ... a, b = 0, 1
    >>> while b < 10:
    ...       print b
    ...       a, b = b, a+b
    ... 
    1
    1
    2
    3
    5
    8

    ????扶??????宏?1????


    Previous Page

    Up One Level

    Next Page

    Python ???

    Contents

    ?????2. ???Python?????? ?????Python ??? ??? 4. ???????????


    Release 2.3, documentation updated on July 29, 2003.

    See About this document... for information on suggesting changes.
    ?????, @ssv 97av