<pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
    <pre id="vvttv"></pre>

      <p id="vvttv"></p>

          <p id="vvttv"></p>

                <p id="vvttv"></p>

                <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                  <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                    <p id="vvttv"></p>

                    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 !'

                    ???滻?? \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()????????????滻????????

                    >>> 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

                      <pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
                      <pre id="vvttv"></pre>

                        <p id="vvttv"></p>

                            <p id="vvttv"></p>

                                  <p id="vvttv"></p>

                                  <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                                    <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                                      <p id="vvttv"></p>

                                      这里只有精品视频