Python 2.7 Tutorial Chinese version

Source: Internet
Author: User

A loop can have an ELSE clause, which executes when the loop iterates through the entire list (for a) or when the execution condition is false (for the while), but the loop is not executed when the break is aborted.

The following example program for searching prime numbers demonstrates this clause

  for-  N in range (2, ten):
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, 'isa primenumber'

= 5def F (arg=i):print= 6print 5.

When a parameter such as **name is introduced, it receives a dictionary (see typesmapping), which contains all the keyword parameters that do not appear in the formal parameter list.

There may also be a combination of form parameters such as *name (described in the next section), which receives a tuple (described in detail in the next sections) and contains all the parameter values that are not present in the formal parameter list.

(*name must appear before **name) For example, we define a function like this

 def cheeseshop (Kind, *arguments, * *keywords):  Print "-- don't have any", Kind,"?"    Print "--I ' m Sorry, we ' re all out of", Kind forArginchArgumentsPrintArgPrint "-"* 40Keys=Keywords.keys () keys.sort ( ) forkwinchKeysPrintkw":", keywords[kw]
It could is called like this: it can call
Cheeseshop ("Limburger","It ' s very runny, sir.","It ' s really very, very runny, sir.", shopkeeper='Michael Palin', Client="John Cleese", Sketch="Cheese Shop Sketch")

andOf course it wouldPrint: Of course, it will print as follows--does any Limburger?--I'm Sorry, we're all out of Limburgerit's very runny, sir.It's really very, very runny, sir.----------------------------------------Client:john cleeseshopkeeper:michael Palinsketch:cheese shop Sketch

For practical purposes, there are several features commonly found in functional programming languages such as Lisp that have been added to Python.

With the Lambda keyword, you can create short, anonymous functions. Here is a function that returns the and of its two parameters: Lambda A, b:a+b.

The Lambda form can be used for any function object that is required. They can only have a single expression for syntax restrictions.

Semantically, they are only a grammatical technique in the definition of common functions. Similar to nested function definitions, lambda forms can reference variables from outside scopes.

defreturnLambda x:x + n ... >>> f = make_incrementor ($)>>> F (0)42>>> F (1)43

You can also use the list as a queue, the queue as a specific data structure, the first entry of the element is first released (FIFO). However, the list is inefficient in this way.

Adding and popping up from the end of the list is relatively slow (because, for an element, you move all the elements in the entire list).

To implement a queue, use Collections.deque, which is designed for quick insertion and deletion at both ends. For example

>>>From   collections Import  deque >>> queue = Deque (["Eric","John","Michael"])>>> Queue.append ("Terry")#Terry arrives>>> Queue.append ("Graham")#Graham arrives>>>Queue.popleft () #The first to arrive now leaves'Eric'>>> Queue.popleft ()#The second to arrive now leaves'John'>>> queue#Remaining Queue in order of arrivalDeque (['Michael','Terry','Graham'])

filter (function, sequence) returns a sequence (sequence) that includes all elements in a given sequence that return a value of true after all call function (item). (The same type is returned, if possible).

If the sequence (sequence) is a string (string) or a tuple (tuple), the return value must be the same type, otherwise it is always list.

def return  and x% 3 ! = 0 ... >>> filter (F, Range (2)) [5, 7, 11, 13, 17, 19, 23]

map (function, sequence) invokes function (item) for each element and returns the returned value as a linked list. For example, the following program calculates the cubic

def return x*x*x ... >>> Map (cube, range (1, one)) [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

You can pass in multiple sequences, and the function must have a corresponding number of arguments, which in turn will invoke the function with the corresponding elements on each sequence (if some sequences are shorter than others, use None instead).

If none is passed as a function, the parameter is returned as an alternative. For example

>>> seq = range (8)defreturn x+y ... >>>2, 4, 6, 8, 10, 12, 14]

Reduce (func, sequence) returns a single value, which is constructed by calling function functions first with the first two elements of the sequence, then invoking the return value and the third argument, and sequentially executing.

For example, the following program calculates the sum of integers 1 to 10

def return x+y ... >>> reduce (Add, range (1, one))55

Python 2.7 Tutorial Chinese version

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.