The forever powerful function of the old Ziko python

Source: Internet
Author: User
function, to the human speaking, can develop to this level of mathematical thinking, is a leap. It can be said that its proposed, directly accelerate the development of modern science and technology and society, regardless of any modern science and technology, and even economics, political science, sociology, etc., have been widely used function.

The following paragraph from Wikipedia (in this tutorial, a lot of definitions from Wikipedia, because it really is encyclopedia): function entry

The mathematical term for the function is Leibniz, which was used in 1694 to describe a relative amount of a curve, such as the slope of a curve or a point on a curve. The functions referred to by Leibniz are now referred to as the derivative functions, which are generally accessible to ordinary people outside the mathematician. It is possible to discuss its limits and derivatives for the derivative function. Both of these describe the relationship between the change of function output value and the change of input value, which is the basis of calculus. The word "function" in Chinese was translated by Qing mathematician analysis. The Algebra book explains: "Where the change is in the letter (including) the variable, this is the function."

Functions, from simple to complex, all kinds. The previous function entry in Wikipedia provides an overview. But no matter what the function, can be summed up:

Have a junior high school math level can understand a probably. Don't dwell on it here.

This lecture focuses on how to use Python to do a function with a use.

Deep understanding of functions

In high school mathematics, you can define a function in this way: Y=4x+3, this is a function once, of course, can also be written as: F (x) =4x+3. where x is a variable, it can represent any number.

When x=2, substituting the function expression above: f (2) = 4*2+3 = 11 so: F (2) = 11

The above understanding of the function is the average junior high school students can hit. However, if crossing has graduated from junior high School, or is a pursuit of junior middle school students, can not be limited to the above understanding, but also to expand the understanding of the function.

Variables are more than just numbers

Can the variable x be any number? In fact, a function is a correspondence relationship. Crossing tried to think of the expression x as a pie, 4x+3, that is, 4 pies in addition to 3 (what the unit is not important), the result corresponds to another thing, that thing, such as the iphone. Or it can be understood that 4 pies plus 3 corresponds to an iphone. This is the so-called mapping relationship.

So, X, not just the number, can be anything you think.

Variable Essence--placeholder

Why do variables use X in a function? This is an interesting question, Google a bit, see if you can find the answer.

I don't know why. However, I am well aware that variables can be used in x, or other symbols, such as y,z,k,i,j ..., even with a combination of letters such as Alpha,beta,qiwei,qiwsir.

A variable is essentially a placeholder. This is sharply's understanding. What is a placeholder? is to first put that position with the variable, indicating there is a thing, as for this position what to put, and later, anyway, first with a symbol occupies this position (placeholder).

In fact, in high-level language programming, variables are more complicated than what we learn in junior maths. But now we don't care about that, the complicated stuff is going to be in the future again. Now, we're going to study the variables in Python according to junior math.

Usually lowercase letters are used to name the variables in Python, or they can be underlined to indicate differences.

For example: Alpha,x,j,p_beta, these can be used as Python variables.

Assigning values to variables

Open idle and experiment with the following:

>>> A = 2  #注1 >>> a    #注22 >>> b = 3  

Description

Note that the meaning of 1:a=2 is to point a variable A to the number 2, just like a is a fishing person, through the fish line, and a fish called 2, a through the fish line can lead to 2
NOTE 2: The equivalent of a fishing person, along the line to export the fish, to see which is connected, found to be called 2 of that silly fish
Note 3:b=3, understand ibid. What about c=3? It was this fish called 3 that was caught at the same time by two people.

Building Simple functions

>>> a = 2>>> y=3*a+2>>> y8

The function established in this way is no different from the study in junior high school mathematics. Of course, the function of this way, in the programming practice of the use of small, generally in the learning stage to understand the function to use.

Don't be impatient, you enter A=3, and then enter Y to see what results?

>>> a=2>>> y=3*a+2>>> y8>>> a=3>>> Y8

Isn't it weird? Why has a equal to 3 in the back, and the result y is still 8.

It can be explained by the previous fishing theory. A and 2 are connected, and after calculation, Y and 8 are connected. The connection object of a is modified, but the connection object of Y has not changed, so it is still 8. Once again, the connection object for Y is changed:

>>> a=3>>> y8>>> y=3*a+2>>> Y11

Special note, if not first a=2, directly under the function expression, like this, will be an error.

>>> Y=3*a+2traceback (most recent): File "
 
  
   
  ", line 1, in 
  
   
    
    nameerror:name ' a '
  
   
 
   is not defined

Note that the error hint, a is a variable, the hint tells us that the variable is not defined. Obviously, if you want to use a variable in a function, you have to define it in advance. The definition method is to copy the variable.

Create a useful function

It is not enough to "normalize" the above to create a function with the command, so write a. py file.

In Idle, file->new window

Then enter the following code:

#coding: Utf-8def add_function (A, B):  c = a+b  print CIF __name__== "__main__":  add_function (2,3)

Then save the file, I named her 106-1.py, and you take a name according to your preference.

Then I went into that folder, ran the file, and the following results appeared,

What is the result of your operation? If you do not get the results above, you will be very careful to check the code, whether I write exactly the same, note, including colons and spaces, are the same. Colons and spaces are important.

Below starts discovering:

#coding: Utf-8 declares that the character set type of the code in this file is Utf-8 format. Beginners if not understand, on the one hand can go to Google, in addition can put a put, first so copied down, will explain later. Def add_function (A, B): Here is the beginning of the function. When declaring a function, be sure to use the DEF (Def is the first three letters of the English define), meaning to tell the computer, here to declare a function; Add_function is the name of the function, and the name is the same as your name. The emphasis of naming in Python is to have a sense of what this function is used for in a name. From the name of Add_function, did you see that she was used to calculate the addition? (A, B) the parentheses are the parameters of the function, which is the function variable. Colon, this colon is very very important, if less, it will be an error. The meaning of the colon is to begin the actual function content. C=A+B Special note that this line indents four spaces than the previous line. This is the Python rules, to keep in mind, do not lose, lost on the error. Then this is the addition of two parameters (variables), the result is assigned to another variable C. Print C is also a reminder to crossing that it indents four spaces. Prints the value of the resulting C. If name== "main": This sentence first copy, do not explain. Note that it is not indented. Add_function (2,3) This is the real call to the previously established function, and passed two parameters: A=2,b=3. The method of carefully observing the incoming parameter is to put 2 in the position of a, 3 in the B position (so the variable is the placeholder).

After the completion of the cow, make a summary:

The format of the declaring function is:

def function name (parameter 1, Parameter 2, ..., parameter n):

function body

Is it a simple style?

The learning of naming words

Some masters, through the name of a person to predict his/her inauspiciousness misfortune and so on. It seems that the name of this thing is too important. A good name is a good omen. So Mr. Kong Qiu said, "The name is not right, the words are not smooth", crooked Solution: The name is not regularized, it is not smooth. This is a crooked solution, hoping not to affect the correct understanding of crossing. I wonder if the masters can predict the inauspiciousness of foreigners by their names.

Anyway, some people are very concerned about the name, next to a country does not seem to care.

Python also cares about name issues, in fact, all high-level languages have a requirement for names. Why is it? Because if the name is messed up, the computer is a little overwhelmed. See Python's general requirements for naming.

File name: All lowercase, can use underline

Function name: lowercase, you can use underline style words to increase readability. such as: Myfunction,my_example_function. Note: Mixed case is only allowed to be used when this style has taken advantage, in order to remain backwards compatible.

Parameters of the function: if the parameter name of a function and the reserved keyword (the so-called reserved keyword is the name that the Python language already occupies, it is often used as a naming for existing functions, etc.). ) conflicts, usually using a suffix underline better than using abbreviations or strange spellings.

Variables: variable names are all lowercase, and each word is connected by an underscore. such as color = White,this_is_a_variable = 1.

In fact, there is still a lot of controversy about the naming problem. The most typical is the so-called Hungarian name Law, hump naming and so on. If you like, you can Google a bit. The following information is available for reference:

Hungarian Naming Act
Camel-Case
Pascal name Law
Python naming official requirements, if crossing English can be, be sure to read. If English is not enough, can you read Chinese without a ladder? Look at your life.

  • 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.