<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

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


                    ??????



                     
                    4.
                    ??????????????

                    ??????????? while ???Python ???????????н?????Щ????????????????????

                     
                    4.1 if
                    ???

                    ??????о??????????? if ??????磺

                    >>> x = int(raw_input("Please enter an integer: "))
                    >>> if x < 0:
                    ...      x = 0
                    ...      print 'Negative changed to zero'
                    ... elif x == 0:
                    ...      print 'Zero'
                    ... elif x == 1:
                    ...      print 'Single'
                    ... else:
                    ...      print 'More'
                    ...

                    ??????? 0 ????? elif ?????else ????????????“elif ” ??“ else if ”????д???????????Ч??????????????if ... elif ... elif ... ????????????????????е? switch ?? case ???

                     
                    4.2 for
                    ???

                    Python?е?for  ???????? C ?? Pascal ?????????в???? ?????????????????????????????????????Pascal??????????????????????????????????C????Python ?? for  ??????????????У??????????????е??????????????????е?????????е????????磨??а??????

                    >>> # Measure some strings:
                    ... a = ['cat', 'window', 'defenestrate']
                    >>> for x in a:
                    ...     print x, len(x)
                    ... 
                    cat 3
                    window 6
                    defenestrate 12

                    ????????????????????в??????????????????????????????????????????????????????????????????????У????磬???????????????????????????????????????????????????????????

                    >>> for x in a[:]: # make a slice copy of the entire list
                    ...    if len(x) > 6: a.insert(0, x)
                    ... 
                    >>> a
                    ['defenestrate', 'cat', 'window', 'defenestrate']

                     
                    4.3 range()
                    ????

                    ?????????????????У????ú???range()??????????????????????????????

                    >>> range(10)
                    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

                    range(10)?????????????10?????????????????????????????????????????10???б??????????????в???????Χ?е??????????????range?????????????????????????????????????????????????????????????????“????”????

                    >>> range(5, 10)
                    [5, 6, 7, 8, 9]
                    >>> range(0, 10, 3)
                    [0, 3, 6, 9]
                    >>> range(-10, -100, -30)
                    [-10, -40, -70]

                    ?????????????????????????????????range()??len()??

                    >>> a = ['Mary', 'had', 'a', 'little', 'lamb']
                    >>> for i in range(len(a)):
                    ...     print i, a[i]
                    ... 
                    0 Mary
                    1 had
                    2 a
                    3 little
                    4 lamb

                     
                    4.4 break
                    ?? continue ??? ???????е? else ???

                    break????C?е??????????????????????for??while?????

                    continue ??????C?н??????????????????????????ε?????

                    ????????????else???;??????????????????б??????for????????????false??????while?????У????????break?????????2?????С?????????????????????????????????

                    >>> for n in range(2, 10):
                    ...     for x in range(2, n):
                    ...         if n % x == 0:
                    ...            print n, 'equals', x, '*', n/x
                    ...            break
                    ...     else:
                    ...          # loop fell through without finding a factor
                    ...          print n, 'is a prime number'
                    ... 
                    2 is a prime number
                    3 is a prime number
                    4 equals 2 * 2
                    5 is a prime number
                    6 equals 2 * 3
                    7 is a prime number
                    8 equals 2 * 4
                    9 equals 3 * 3

                     
                    4.5 pass
                    ???

                    pass ????????????????????Щ????????????????????????????????????????磺

                    >>> while True:
                    ...       pass # Busy-wait for keyboard interrupt
                    ...

                     
                    4.6
                    ???庯??

                    ????????д??????????????и????????????????У?

                    >>> def fib(n):    # write Fibonacci series up to n
                    ...     """Print a Fibonacci series up to n."""
                    ...     a, b = 0, 1
                    ...     while b < n:
                    ...         print b,
                    ...         a, b = b, a+b
                    ... 
                    >>> # Now call the function we just defined:
                    ... fib(2000)
                    1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

                    ?????def ?????????????????????????????к???????????????????????????????????????п???????????????????????????п??????????????????????????ú????? ????????????????docstring?? 

                    ??Щ??????????????????????????????????????????????????;?????????м????????????????????????????????????

                    ???ú??????????????????????μ?????????е??????????洢??????????????С????ò???????????????????в?????????????????????????????????????????????????????????????????????????????????????????global???????????

                    ??????????????????????????????????????????????????????????????????????????????????????????????4.1 ?????????????????????????????μ????????????ù????б???????

                    ??????????????????????????????????????庯??????????????????????????????????????????????????????????????????????????á????????????????????

                    >>> fib
                    <function object at 10042ed0>
                    >>> f = fib
                    >>> f(100)
                    1 1 2 3 5 8 13 21 34 55 89

                    ????????fib?????????????function????????????????procedure????Python??C?????????????????з????????????????????????????????????????????????????????????????????????? None ???????????????????????????????None??????????????????д???None?????????????????????????????????????

                    >>> print fib(0)
                    None

                    ??????????????δ?????з????????????????????е?????????????????????

                    >>> def fib2(n): # return Fibonacci series up to n
                    ...     """Return a list containing the Fibonacci series up to n."""
                    ...     result = []
                    ...     a, b = 0, 1
                    ...     while b < n:
                    ...         result.append(b)    # see below
                    ...         a, b = b, a+b
                    ...     return result
                    ... 
                    >>> f100 = fib2(100)    # call it
                    >>> f100                # write the result
                    [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

                    ???????????????????????Щ?μ?Python?????

                     
                    4.7
                    ??????????

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

                     
                    4.7.1
                    ????????

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

                    def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
                        while True:
                            ok = raw_input(prompt)
                            if ok in ('y', 'ye', 'yes'): return 1
                            if ok in ('n', 'no', 'nop', 'nope'): return 0
                            retries = retries - 1
                            if retries < 0: raise IOError, 'refusenik user'
                            print complaint

                    ??????????????????μ????????ask_ok('Do you really want to quit?')??????????????ask_ok('OK to overwrite the file?', 2)??

                    ??????????????α????????????????

                    i = 5
                    
                    def f(arg=i):
                        print arg
                    
                    i = 6
                    f()

                    will print 5.

                    ??????棺????????????Ρ????????????????????????????????????????????????Щ???????磬???o????????????л??????????????

                    def f(a, L=[]):
                        L.append(a)
                        return L
                    
                    print f(1)
                    print f(2)
                    print f(3)

                    ?????????

                    [1]
                    [1, 2]
                    [1, 2, 3]

                    ??????????????????????乲??????????????????????????????д??????

                    def f(a, L=None):
                        if L is None:
                            L = []
                        L.append(a)
                        return L

                     
                    4.7.2
                    ?????????

                    ???????????????????????????????????“keyword = value”?????磬???μ??????

                    def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
                        print "-- This parrot wouldn't", action,
                        print "if you put", voltage, "Volts through it."
                        print "-- Lovely plumage, the", type
                        print "-- It's", state, "!"

                    ?????????μ?????????????

                    parrot(1000)
                    parrot(action = 'VOOOOOM', voltage = 1000000)
                    parrot('a thousand', state = 'pushing up the daisies')
                    parrot('a million', 'bereft of life', 'jump')

                    ??????????????????Ч???

                    parrot()                     # required argument missing?????????????
                    parrot(voltage=5.0, 'dead')  # non-keyword argument following keyword????????????з??????????
                    parrot(110, voltage=220)     # duplicate value for argument?????????????????????
                    parrot(actor='John Cleese')  # unknown keyword??δ???????

                    ??????????б??е????????????????????????????????????????ж??????????????????????????????????????????????θ?????????????????????????ε??????????λ?ú?????????????????????????????????????????????????????

                    >>> def function(a):
                    ...     pass
                    ... 
                    >>> function(0, a=0)
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in ?
                    TypeError: function() got multiple values for keyword argument 'a'

                    ??????????? **name ??????????????????????????????????δ??????????????б??е????????????????????????????????? *name ???????????????????????????????л???????????????????????г?????????????б??е?????????*name ?????? **name ??????? ???磬?????????????????????

                    def cheeseshop(kind, *arguments, **keywords):
                        print "-- Do you have any", kind, '?'
                        print "-- I'm sorry, we're all out of", kind
                        for arg in arguments: print arg
                        print '-'*40
                        keys = keywords.keys()
                        keys.sort()
                        for kw in keys: print kw, ':', keywords[kw]

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

                    cheeseshop('Limburger', "It's very runny, sir.",
                               "It's really very, VERY runny, sir.",
                               client='John Cleese',
                               shopkeeper='Michael Palin',
                               sketch='Cheese Shop Sketch')

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

                    -- Do you have any Limburger ?
                    -- I'm sorry, we're all out of Limburger
                    It's very runny, sir.
                    It's really very, VERY runny, sir.
                    ----------------------------------------
                    client : John Cleese
                    shopkeeper : Michael Palin
                    sketch : Cheese Shop Sketch

                    ???sort()??????????????????????????????????????????????????δ??????

                     
                    4.7.3
                    ????????

                    ??????????????????????ú?????????????????????Щ???????????????????????Щ????????????????????????????????????

                    def fprintf(file, format, *args):
                        file.write(format % args)

                     
                    4.7.4 Lambda
                    ???

                    ???????????????м??????????????????Lisp?г????????????Python?????lambda???????????????С?????????????????????????????????????????????“lambda a, b: a+b”?? Lambda ????????????κ???????????????????????????????????????????????????????????????????????????е?????????ɡ????????????????壬lambda?????????????Χ???????????

                    >>> def make_incrementor(n):
                    ...     return lambda x: x + n
                    ...
                    >>> f = make_incrementor(42)
                    >>> f(0)
                    42
                    >>> f(1)
                    43

                     
                    4.7.5
                    ????????

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

                    ??????????????????????顣??????????????????????????????????????????????????????????????????????????????????????????????????????????д?????????????β??

                    ????????????ж??У???????????????????????????????????????????????????????????????????????????????Ч????

                    Python??????????????е??????????????????????????????????????????????????????????????????????????????о????????????????????????????????????????????????????????????????????????????????????“????”?????????????????? ???ж???????????????????????????????е????????????????????????????????????????????8????????????????ò????????????????μ????????????

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

                    >>> def my_function():
                    ...     """Do nothing, but document it.
                    ... 
                    ...     No, really, it doesn't do anything.
                    ...     """
                    ...     pass
                    ... 
                    >>> print my_function.__doc__
                    Do nothing, but document it.
                    
                        No, really, it doesn't do anything.



                    ???

                    ... ??????4.1
                    ???????????????????????????????????????????????????????????????????????κθ?????????в???????????

                    Previous Page

                    Up One Level

                    Next Page

                    Python ???

                    Contents

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


                    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>

                                      这里只有精品视频