[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