Basic python tutorials-Introduction to user-defined functions, basic python tutorial Functions

Source: Internet
Author: User

Basic python tutorials-Introduction to user-defined functions, basic python tutorial Functions

The most important purpose of a function is to make it easier for us to reuse the same program.

Some operations are affiliated to a function. When you want to implement the same operation in the future, you only need to call the function name, without repeating all the statements.

Function Definition

First, we need to define a function to describe the function.
Copy codeThe Code is as follows:
Def square_sum (a, B ):
C = a ** 2 + B ** 2
Return c

This function is used to calculate the sum of squares of two numbers.

First, def, this keyword notifies python: I am defining a function. Square_sum is the function name.

A and B in parentheses are the parameters of the function and are the input of the function. There can be multiple or none of the parameters (but the brackets should be retained ).

We have seen the affiliation expressed by colons and indentation in the loop and selection.
Copy codeThe Code is as follows:
C = a ** 2 + B ** 2 # This is an internal operation of the function.

Return c # return the value of c, that is, the output function. Python functions allow no return value, that is, no return.

Return can return multiple values separated by commas. Returns a tuple (value table ).

Return a, B, c # is equivalent to return (a, B, c)

In Python, when the program executes return, the program stops executing the remaining statements in the function. Return is not required. If there is no return, or there is no return value after return, the function will automatically return None. None is a special data type in Python, used to indicate nothing, equivalent to NULL in C. None is mostly the default value used for passing keyword parameters.

Function call and parameter transfer

After defining a function, you can use this function in subsequent programs.
Copy codeThe Code is as follows:
Print square_sum (3, 4)

Python knows through location that 3 corresponds to the first parameter a and 4 in the function definition, corresponding to the second parameter B, and then passes the parameter to the function square_sum.

(Python has rich parameter passing methods, including keyword passing, table passing, and dictionary passing. The basic tutorial will only involve location passing)

After calculation, the function returns value 25, which is printed by print.

Let's look at the following two examples.

Copy codeThe Code is as follows:
A = 1

Def change_integer ():
A = a + 1
Return

Print change_integer ()
Print

#=== (In Python, "#" is followed by a comment without execution)

B = [1, 2, 3]

Def change_list (B ):
B [0] = B [0] + 1
Return B

Print change_list (B)
Print B

In the first example, we pass an integer variable to the function. The function operates on it, but the original integer variable a does not change.

In the second example, a table is passed to the function for operations. The original table B changes.

For a variable of the basic data type, after the variable is passed to the function, the function will copy a new variable in the memory, so as not to affect the original variable. (We call this value transfer)

However, for a table, the table is passed to the function as a pointer pointing to the position of the sequence in the memory. operations on the table in the function will be performed in the original memory, thus affecting the original variables. (We call this pointer transfer)

Summary

Copy codeThe Code is as follows:
Def function_name (a, B, c ):
Statement
Return something # return is not required

Function purpose: to improve the repeated availability of the program.
Copy codeThe Code is as follows:
Return None

PASS Parameters by location.

Parameter of the basic data type: Value Transfer

Table as a parameter: pointer Transmission

Exercise:

Write a function for determining the leap year. The parameter is year, month, or day. Returns True if it is a leap year.


Python user-defined functions,

The Rows starting with... are function/Type Definitions,
>>> The row at the beginning is the execution

Therefore, after defining a function, you need to enter an additional carriage return to end the definition.

How does a python UDF re-run itself? Is there such code?

I don't know what you mean by re-running yourself? Recursion? Or call?

Previously written, copy the folder, you can see, hope to help you!
Import OS
Import shutil
Def my_copytree (src, dst ):
Names = OS. listdir (src)
If not OS. path. exists (dst ):
OS. mkdir (dst)
For name in names:
Srcname = OS. path. join (src, name)
Dstname = OS. path. join (dst, name)

If OS. path. isdir (srcname ):
My_copytree (srcname, dstname) # recursively traverse folders
Else:
If (not OS. path. exists (dstname) or (OS. path. exists (dstname) and (OS. path. getsize (dstname )! = OS. path. getsize (srcname )))):
# Print dstname
Shutil. copy2 (srcname, dst)

If _ name _ = '_ main __':
Src = 'C: \ caselog'
Dst = 'C: \ bug'
My_copytree (src, dst) ---- called here

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.