search
HomeBackend DevelopmentPython TutorialBasic 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


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
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
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 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.

See all articles

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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!