Home Backend Development Python Tutorial Basic exercises for function transfer in python language

Basic exercises for function transfer in python language

Apr 01, 2017 pm 01:32 PM

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


Basic exercises for function transfer in python language


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


Basic exercises for function transfer in python language


The following is the result of executing the program:

Basic exercises for function transfer in python language


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:

Basic exercises for function transfer in python language


#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


Basic exercises for function transfer in python language



Basic exercises for function transfer in python language


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:

Basic exercises for function transfer in python language


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

Basic exercises for function transfer in python language


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:

Basic exercises for function transfer in python language

##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:


Basic exercises for function transfer in python language

The results are as follows:


Basic exercises for function transfer in python language

5. Use functions to implement a factorial (recursive and automatic Definition)


Basic exercises for function transfer in python language
The following is the output result, the results of the two functions are consistent:

Basic exercises for function transfer in python language

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

Basic exercises for function transfer in python language

The following is the output result. By comparing the script, you can see the difference and function

Basic exercises for function transfer in python language


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

Basic exercises for function transfer in python language

The results are as follows:

Basic exercises for function transfer in python language


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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 12, 2025 am 12:01 AM

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: Games, GUIs, and More Python: Games, GUIs, and More Apr 13, 2025 am 12:14 AM

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 vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

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.

The 2-Hour Python Plan: A Realistic Approach The 2-Hour Python Plan: A Realistic Approach Apr 11, 2025 am 12:04 AM

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.

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

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: Exploring Its Primary Applications Python: Exploring Its Primary Applications Apr 10, 2025 am 09:41 AM

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 vs. C  : Exploring Performance and Efficiency Python vs. C : Exploring Performance and Efficiency Apr 18, 2025 am 12:20 AM

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: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

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.

See all articles