


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:
- A function code block begins with the def keyword, followed by the function identifier name and parentheses ( ).
- Any incoming parameters and independent variables must be placed between parentheses. Parameters can be defined between parentheses.
- The first line statement of a function can optionally use a documentation string - used to store function descriptions.
- The function content starts with a colon and is indented.
- returm [expression] ends the function and optionally returns a value to the caller.
- 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:
The output result after the call is:
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:
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:
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:
缺省参数
调用函数时,缺省参数的值如果没有传入,则被认为是默认值。
示例:
ch06-demo03-args-default.py
打印默认的age,如果age没有被传入:
注意:缺省值必须放在最后一个参数。
不定长参数*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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
