Basic exercises for function transfer in python language
First of all, when writing the Python script, how to use Chinese
at the beginning of the script, you can add the following code segment:
# _*___ 8 _*_ its effect and# coding=utf-8 is the same
The following is a summary of how to pass parameters when learning Python to define functions:
1. First of all It is the positional parameter and the default value:
Define a function func_name, where the formal parameters are arg, arg1, arg2, arg3, arg4
Among these parameters, arg3 and arg4 have default values. If only three values are given when calling the function, then the default values of arg3 and arg4 will be used. When the parameters given are four or five, the corresponding formal parameters will be assigned to the corresponding parameters at a time
The following is the result of executing the program:
PS: The position parameter is the most The main thing is that the number of parameters must be consistent, and the order is very important. The corresponding value is assigned to the formal parameter at the corresponding position.
Passing parameters with keywords:
You can also use keywords to pass parameters here, that is, in When calling a function, you specify which actual parameter corresponds to the formal parameter. At this time, the position is not so important. However, if there are both positional parameters and keyword parameters, you still need to define the positional parameters first. This is in the definition. Be careful when calling functions, otherwise it will cause unnecessary trouble when calling functions.
2. Collect remaining redundant parameters and collect redundant dictionary parameters
Define a function that can be called by the normal method of passing in only value or key-value. But if the first parameter passed in is the key-value method, then the subsequent ones must all be key-value methods. If the first one is not, then the subsequent values can be passed in according to the situation.
The following is a specific example program:
#In the above function func_name, the parameter it specifies is uncertain, and may be 0, or maybe n, so use *arg to collect all remaining parameters
When calling a function, if the parameter is a list, using *Li is actually to solve the list and then add each element in the list in sequence To pass parameters, *arg is generally used for parameter collection; if *Li is not used, Li is called directly, which is to pass the entire list as a whole
**arg is used to collect key-value class parameters Yes, this type of parameter is specified and defined at the end of the function parameter. When actually calling the function, you can use the form 'key'='value' or {'key1'='value1', 'key2'='value2'} Calling by value
The definition of the function cat is to put all the parameters together so that everyone can see the effect and how the parameters are passed when calling the cat function
The following is the output result of the function. Based on the comparison of the results, you can feel the difference between different parameter calls:
3. Obtain variables from standard input, replace variables in statements, and use dictionary parameters in the function body:
raw_input is used Get the value of standard input and assign it to a variable, and you can also get a prompt message
In Python, add a variable to the statement and use % to replace it
In When defining and calling functions, you must pay attention to the two preceding * signs when passing parameters in the key-value dictionary. After the function is collected, it is saved in a dictionary. After passing it in, the corresponding dictionary method will be used when calling, here I won’t go into details;
The following is the result of running the program. You can compare it based on the results to see the specific effects of passing parameters:
##4 , The return value of the function
The most important thing in a function is its return value. When calling a function, the result of its return value is used to perform the next step of operation. The following is a simple example:
The results are as follows:
5. Use functions to implement a factorial (recursive and automatic Definition)
The following is the output result, the results of the two functions are consistent:
6. The following is a function call, and a comparison of the similarities and differences between the function name and function result assignment. Calling the return value of another function in one function
The following is the output result. By comparing the script, you can see the difference and function
7. The following is the basic function of some built-in functions. , are some simple exercises for everyone’s reference and study. Please give us your valuable opinions
The results are as follows:
The above are some of my summaries and reference exercises for everyone to learn from. Please give me your valuable opinions! ! !
This article comes from the "ptallrights" blog, please be sure to keep this source http://ptallrights.blog.51cto.com/11151122/1788896
The above is the detailed content of Basic exercises for function transfer in python language. 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











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
