[Life Bitter short Python song] -- Python functional programming 01, python01

Source: Internet
Author: User

[Life Bitter short Python song] -- Python functional programming 01, python01

People who have some knowledge about Python should know that Python is not a functional programming language, but a language that supports multiple paradigms, which also enables functional programming in Python,

For those who have learned Python Functional Programming, I suggest you read a book named "Python Functional Programming" (Functional Programming in Python). You should be able to get a lot from this book;

How can we say that a function is an encapsulation supported by Python's built-in support? By splitting a large segment of code into a function and calling a function at a layer-by-layer, we can break down complicated tasks into simple tasks, this is called process-oriented programming. A function is the basic unit of process-oriented programming.

First, let's take an example: Calculate the Fibonacci sequence (any number is the numerical sequence of the sum of the first two numbers)

 

1 fibs=[0,1]2 num =input('How many Fibonacci do you want?')3 for i in range(num-2):4     fibs.append(fibs[-2]+fibs[-1])5 print(fibs)

The above example shows that programmers are very lazy and repeated problems should be solved in an abstract way;

Functions in Python mainly learn the following:

1. How to define

2. docized Functions

3. Parameter magic (parameter transfer, keyword parameter, collection parameter, and inverse parameter collection process)

4. Job domain

5. Recursion

6. Functional Programming (lambda expressions, map functions, filter functions, reduce functions, partial, Groupby, Compose, Currying)

Map (func, seq [, seq...])

Filter (func, seq)

Reduce (func, seq [, initial])

Sum (seq)

Apply (func [, args [, kwargs])

1. define functions using def statements in Python

def hello(name):    return 'Hello.'+name+'!'

2. docized Functions

def square(x):    'Calculates the square of the number x.'    return x*x

 

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.