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!

This tutorial demonstrates how to use Python to process the statistical concept of Zipf's law and demonstrates the efficiency of Python's reading and sorting large text files when processing the law. You may be wondering what the term Zipf distribution means. To understand this term, we first need to define Zipf's law. Don't worry, I'll try to simplify the instructions. Zipf's Law Zipf's law simply means: in a large natural language corpus, the most frequently occurring words appear about twice as frequently as the second frequent words, three times as the third frequent words, four times as the fourth frequent words, and so on. Let's look at an example. If you look at the Brown corpus in American English, you will notice that the most frequent word is "th

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

Dealing with noisy images is a common problem, especially with mobile phone or low-resolution camera photos. This tutorial explores image filtering techniques in Python using OpenCV to tackle this issue. Image Filtering: A Powerful Tool Image filter

PDF files are popular for their cross-platform compatibility, with content and layout consistent across operating systems, reading devices and software. However, unlike Python processing plain text files, PDF files are binary files with more complex structures and contain elements such as fonts, colors, and images. Fortunately, it is not difficult to process PDF files with Python's external modules. This article will use the PyPDF2 module to demonstrate how to open a PDF file, print a page, and extract text. For the creation and editing of PDF files, please refer to another tutorial from me. Preparation The core lies in using external module PyPDF2. First, install it using pip: pip is P

This tutorial demonstrates how to leverage Redis caching to boost the performance of Python applications, specifically within a Django framework. We'll cover Redis installation, Django configuration, and performance comparisons to highlight the bene

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap

Python, a favorite for data science and processing, offers a rich ecosystem for high-performance computing. However, parallel programming in Python presents unique challenges. This tutorial explores these challenges, focusing on the Global Interprete

This tutorial demonstrates creating a custom pipeline data structure in Python 3, leveraging classes and operator overloading for enhanced functionality. The pipeline's flexibility lies in its ability to apply a series of functions to a data set, ge


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools
