Python Learning Summary of the five--Introductory functional programming

Source: Internet
Author: User

Function-Type programming

The recent study of Python has been a bit of a snub, and the recent learning attitude and learning efficiency is really bad, and the current condition is improving.

Today, I took a brief summary of the functional programming of Python, which I have previously learned, and share it with you, and also welcome and thank you for your comments.

First, when we learn functional programming, we need to know a concept: higher-order functions . So what exactly is a higher-order function? by passing functions as arguments, such functions are called higher-order functions. functional programming refers to this highly abstract programming paradigm.

Body

Next, I'll briefly describe the usage of the following four functions:map/reduce filter sorted Lambda

Here, Map/reduce is not a map/reduce in a distributed system, but a map () function and a reduce () function built into Python.

Map: When we use the map function, the map function needs to receive two parameters, the first parameter is a function, the second argument is a sequence, then the meaning of the expression is that map will pass in the function in sequence to each element, and the result is returned as a list.

1 def f (x): 2     return x*x34print map (f,[1,2,3,4,5,6,7,8,9,10])

The result is conceivable:

See here, you will not have disdain to mean, why do I have to use the map function Ah, directly write a function, and then call can ah. Yes, it must be true to write, but will it be a bit of trouble? Look at the code below.

1 def fuc (x): 2     return x*3     4 L = []5 for in [ 1,2,3,4,5,6,7,8,9,10]:6    l.append (fuc (num))7print L

reduce: What is the difference between the reduce function and the map function? The reduce function also requires two parameters: a function and a sequence. The function in the reduce parameter must receive two parameters, so the reduce function represents a cumulative calculation of the continuation of the returned result and the next element of the sequence, such as the summation of the sequence.

1 def F2 (x, y): 2     return x+3     4print reduce (f2,[1,2,3,4,5,6,7,8,9,10])

Thinking about the definition of reduce, we can also use reduce to solve an egg-ache problem, that is, the sequence into an integer, such as [1,2,3,4,5] into 12345.

1 def fn (x, y): 2     return x*10+3     4print reduce (fn,[1,2,3,4,5,6,7,8])

Filter: The filter function filters some elements in a sequence. Like the map and reduce functions, filter also receives a function and a sequence, unlike the filter, which applies the passed function arguments to each element in the sequence, and then determines whether the element is discarded based on the return value, which is true or false.

1 def IsEven (x): 2     return x%2==03     4Print filter (iseven,[1,2,3,4,5,6,7,8,9,10])

sorted: The sorted function is used to sort the sequence, which is mentioned many times in the Python summary before, and here I briefly say the usual rule: for two elements x and Y, if x<y, returns 1 if x>y, returns 1 if X ==y, returns 0. So the default rules, we sort of the result is small to large sort, so if we want to get from large to small results, we need to rewrite the provisions of the sorted function, this and C + + is the same reason, do not put the code, their own side to realize it.

Lambda: Convenient and powerful lambda function, also known as anonymous function, it does not need to explicitly define the function, but the anonymous function has some limitations, that is, there can be only one expression, without writing return, the return value is the result of the expression.

1 Print map (Lambda x:x*x, [1,2,3,4,5,6,7,8,9,10])2Lambda x:x*x*
    3print FF (5)

Anonymous functions also have a lot of advantages, otherwise how to use it. First, the function does not have a name, there is no need to worry about a function name conflict; Second, the anonymous function is a function object, you can assign an anonymous function to a variable, and then use the variable to invoke the function.

Python Learning Summary Five-introductory functional programming

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.