<span id="7ztzv"></span>
<sub id="7ztzv"></sub>

<span id="7ztzv"></span><form id="7ztzv"></form>

<span id="7ztzv"></span>

        <address id="7ztzv"></address>

            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]

            ??????????????????????k????????????????????????????????????

            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
            <span id="7ztzv"></span>
            <sub id="7ztzv"></sub>

            <span id="7ztzv"></span><form id="7ztzv"></form>

            <span id="7ztzv"></span>

                  <address id="7ztzv"></address>

                      ŷ