Home >Backend Development >Python Tutorial >Introduction to the basics of Python functional programming

Introduction to the basics of Python functional programming

PHPz
PHPzforward
2023-04-11 22:49:081594browse

Introduction to the basics of Python functional programming

Basic knowledge of functions

Master the basic syntax specifications and calling methods of custom functions, and master the use and calling rules of various parameters of the function.

1. Python function

  • Function (Function) is an organized, reusable code segment used to implement a single or related function.
  • Function can improve the modularity of the application and the reuse rate of the code.
  • We have already been exposed to many built-in functions provided by Python, such as print().
  • But you can also create your own functions, which are called user-defined functions.

2. Basic rules for customizing a function

You can define a function with the functions you want. The following are simple rules:

  1. A function code block begins with the def keyword, followed by the function identifier name and parentheses ( ).
  2. Any incoming parameters and independent variables must be placed between parentheses. Parameters can be defined between parentheses.
  3. The first line statement of a function can optionally use a documentation string - used to store function descriptions.
  4. The function content starts with a colon and is indented.
  5. returm [expression] ends the function and optionally returns a value to the caller.
  6. Return without expression is equivalent to returning None.

3. Customize a function syntax

Define function syntax:

def 函数标识名称(参数列表):
“函数_文档字符串,对函数进行说明"
函数体
return [表达式]

By default, parameter values ​​and parameter names are defined in the function declaration The order matches.

4. Function call

Defining a function only gives the function a name, specifies the parameters contained in the function, and the code block structure.

After the basic structure of this function is completed, you can execute it through another function call or directly from the Python prompt.

The following example calls the printme ( ) function:

Introduction to the basics of Python functional programming

The output result after the call is:

Introduction to the basics of Python functional programming

4. Return keyword

  • return statement [expression] exits the function and selectively returns an expression to the caller.
  • The return statement without parameter value returns None.
  • The previous examples did not demonstrate how to return a value. The following example tells you how to do it:

Introduction to the basics of Python functional programming

5. Parameter passing

In python, the type belongs to object, and the variable has no type:

a=[1,2,3]
a="Runoob"

In the above code, [1,2,3] is List type, "Runoob" is String type, and variable a has no type Type, it is just a reference to an object (-a pointer), which can be a List type object or a String type object.

Parameter passing of Python functions

  • Immutable types: Value passing in program programming, such as integers, strings, and tuples. For example, fun(a) only transfers the value of a and does not affect the a object itself. For example, modifying the value of a inside fun(a) only modifies another copied object and does not affect a itself. We often call this passing by value.
  • Variable type: similar to reference passing (address passing) in programming, such as lists and dictionaries. For example, fun(la) will actually pass la. After modification, la outside fun will also be affected.

Everything in Python is an object. In a strict sense, we cannot say whether to pass by value or by reference. We should say passing immutable objects and passing mutable objects.

6. Parameters

The following are the formal parameter types that can be used when calling functions:

  • Required parameters.
  • Keyword parameters.
  • Default parameters.
  • Indefinite length parameters.

Required parameters

Required parameters must be passed into the function in the correct order. The quantity when called must be the same as when declared.

Example:

ch06-demo01-args-necessary.py

When calling the greeting() function, you must pass in a parameter, otherwise a syntax error will occur:

Introduction to the basics of Python functional programming

Key Word parameters

Keyword parameters are closely related to function calls. Function calls use keyword parameters to determine the incoming parameter values.

Using keyword parameters allows the order of parameters when the function is called to be inconsistent with the order of declaration, because the Python interpreter can match parameter values ​​with parameter names.

Example:

ch06-demo02-keyword.py

The following example uses the parameter name when the function printinfo() is called:

Introduction to the basics of Python functional programming

缺省参数

调用函数时,缺省参数的值如果没有传入,则被认为是默认值。

示例:

ch06-demo03-args-default.py

打印默认的age,如果age没有被传入:

Introduction to the basics of Python functional programming

Introduction to the basics of Python functional programming

注意:缺省值必须放在最后一个参数。

不定长参数*args

可能需要一个函数能处理比当初声明时更多的参数。这些参数叫做不定长参数。

适用于当参数个数不确定或根据调用情况其参数个数会动态变化的情况。

基本语法如下:

def函数名称(formal args, *args ):
“函数_文档字符串"
函数体
retum [表达式]

加了星号(* )的变量名会存放所有未命名的变量参数。选择不多传参数也可,可变长参数的类型为元组。

补充: **kw

**两个型号代表接受的是一个可变长度的 字典类型的参数。

因此,改参数必须以k-v值结构出现。

def函数名称(formal _args, **kw ):
“函数_文档字符串”
函数体
retum [表达式

加了星号(** )的变量名会存放所有未命名的变量参数。选择不多传参数也可,可变长参数的类型为字典。

总结: *argv和**kw的区别

两个参数必须为函数定义中参数列表中的排名最后的参数。

*argv代表该参数位置可以放任意个数的数据,最终都会转换成元组数据类型在函数体内处理。

**kw代表该参数位置可以放k=v格式的数据,最终都会转换成字典类型数据安函数体内处理。

The above is the detailed content of Introduction to the basics of Python functional programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete