Functional programming of Python

Source: Internet
Author: User

I. Definition and use

1, function of the role of programming: reduce the repetition rate of code, enhance the readability of the Code

2. Format: def function name (parameter):

function body

return value

def represents the keyword, and when the code executes to Def, it knows that it is a function, the program places it in memory and does not execute it, only when the function is called.

Function name: the names of functions, which are called later by function names

Parameters: providing data for functions

Function Body: Performs a series of operations, such as: printing, calculating 0-100 and etc.

Return value: Once the function has finished executing, it can return the data to the call

3. Return value

function is a function module, whether the function succeeds or not, it is necessary to tell the caller by the return value

Two points to be aware of:

1) If there is no return, the default return is None

2) Once the return is encountered, the code within the function return does not execute the

4, parameters (provide data for the function, function according to different data to perform different functions)

1) arguments, formal parameters

1, formal parameters: In the definition of the function, the parentheses do not have the actual value of the parameters

2, the actual parameters: when the function is called, the parentheses have the actual value of the parameters

2) General Parameters

When called, the number of parameters is consistent with the definition and corresponds to order one by one

3) Specify parameters

When called, the actual value is assigned directly to the parameter, so that no position one by one corresponds to the

def main (b):    print (a) main (1,100)                    #普通调用, a corresponds to 1, b corresponds                to b=1,a=100 #指定参数的调用, a corresponds to 100,b 1

4) Default parameters (default parameters to be placed at the end of the function, such as: def Fun (a=1,b)x def fun (b,a=1)√)

When the function is defined, the parameter is specified as a default value, and when the call is not passed in, the Ⅹ is used with the default value, and if the passed-in argument uses the argument

Example: Def fun1 (a,b=1)

FUN1 (2) #没传入实参, B equals default value 1

FUN1 (2,3) #传入实参, b equals argument 3

5) Dynamic Parameters

1. Add * in front of the parameter to allow multiple parameters to be passed in

A, direct invocation, each argument is treated as an element of a

def main (*a):    

b, add * before the argument, each element of the argument is equal to the element in the formal parameter

def main (*a):    print (A,type (a)) main (*[1, 2]) results: (1, 2) <class ' tuple ' >

2, before the parameter plus * *, allow incoming key value pairs (**kwargs:kwargs is the dictionary type)

A. Call directly

b, before the parameter plus * *

3, set two parameters *args (arguments parameter), **kwargs (key word arguments keyword parameter)

Call the same two, the key value pair is put into Kwargs, and vice versa into the args

5. Local variables and global variables

Global variables

Capitalize so that it is easy to distinguish

To modify, the former plus global

Local variables

lowercase, can only be called inside the function

Second, the parameter transfer

Parameter passing in Python is passing a reference to a value to a formal parameter

There are two different ways to choose from other languages:

1. Pass a reference, point to the same value

2. Re-create a value and pass it to the formal parameter

Third, the judgment object is not a string, list, tuple or dictionary data types

Isinstance (x,a_tuple): Determine if the object is not the corresponding data type

Input: x Indicates the object name, A_tuple denotes the class name (can be: str, int, tuple, list, dict, etc.)

Function: Determine object type

Output: Returns True if the data type of the object is the same as the following class name, otherwise false

>>> isinstance (12,int) true>>> isinstance (' a ', int ') false>>> isinstance ({1: '},dict ') True

  

  

Functional programming of Python

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.