With old Ziko Python's Forever powerful function _python

Source: Internet
Author: User
Tags lowercase reserved in python

function, for human beings, can develop to this mathematical thinking level, is a leap. It can be said that the proposed, directly accelerate the development of modern science and technology and society, whether it is any modern science and technology, and even economics, political science, sociology, etc., have been widely used in functions.

The following section comes from Wikipedia (in this tutorial, a lot of definitions come from Wikipedia because it's really Wikipedia): function entry

The mathematical term of a function is used by Leibniz in 1694 to describe a relative amount of a curve, such as the slope of a curve or a point on a curve. The function that Leibniz refers to is now called a homing function, which is the kind of function that ordinary people in general have come into contact with outside mathematicians. The limit and derivative of the Guide function can be discussed. The two 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 is translated by the Qing mathematician Li Shanlan. In its algebra book, it explains: "Where the variable is a letter (containing) the variable, this is the function."

Functions, from simple to complex, various. The previous Wikipedia function entry, which gives an overview. But no matter what kind of function, you can use the following diagram to summarize:

Have a junior high school math level can understand a probably. Here is not to repeat.

This highlights how to use Python to do a function with a.

Deep understanding of functions

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

When x=2, a function expression that is entered into the above:
f (2) = 4*2+3 = one
so: f (2) = 11

The above understanding of the function, is the average junior high school students can hit. However, if reader 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 only be any number? In fact, a function, is a corresponding relationship. Reader tried to interpret the expression x as a pie, 4x+3, 4 pies plus 3 (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, it's not just a number, it can be anything you think.

Variable Nature--placeholder

Why does the variable use x in the function? This is an interesting question, Google yourself and see if you can find the answer.

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

A variable is essentially a placeholder. This is a nail on the sharply understanding. What is a placeholder? is to put that position in the variable occupy, that there is a thing here, as for this position put what things, later, anyway, first of all, with a symbol occupy this position (placeholder).

In fact, in advanced language programming, variables are more complicated than those we learn in junior maths. But, now, let's not forget about the complicated stuff and put it in the future. Now, study the variables in Python according to junior math.

You can usually name a variable in python with a lowercase letter or underline something in it to indicate the difference.

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

Assigning values to variables

Open the idle, the experimental operation is as follows:

>>> A = 2  #注1
>>> a    #注2
2
>>> b = 3  #注3
>>> c = 3
& Gt;>> b
3
>>> C
3
>>> 

Description

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

Create simple functions

>>> A = 2
>>> y=3*a+2
>>> y
8

The functions established in this way are no different from those in junior high school mathematics. Of course, the function of this way, in the programming practice is not very useful, generally in the learning phase 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
>>> y
8
>>> a=3
>>> y
8

Is it strange? Why is that a equals 3, and the result is Y or 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 for the following a has been modified, but the connection object for Y has not changed, so it is still 8. Once again, the connection object for Y is changed:

>>> a=3
>>> y
8
>>> y=3*a+2
>>> y
11

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

>>> y=3*a+2
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 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 way to define this is to copy the variable.

Build a useful function

It's not "formalized" to create a function in a command way, so write a. py file.

In Idle, file->new window

Then enter the following code:

#coding: Utf-8

def add_function (a,b):
  c = a+b
  print c

if __name__== "__main__":
  add_function (2,3)

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

Then I go into that folder, run this file, and the following results appear, as shown in figure:

What is the result of your operation? If you don't get the results above, you're very serious about checking the code to see if it's exactly the same as I wrote, noting that both the colon and the space are the same. Colons and spaces are important.

The following starts Sunding:

#coding: Utf-8 declares that the character set type of code in this file is Utf-8 format. If beginners do not understand, on the one hand can go to Google, but also put a place, the first so copied down, will explain later.

def add_function (a,b): Here is the beginning of the function. When declaring that you want to create a function, be sure to use DEF (the first three letters of the English define), meaning to tell the computer to declare a function; Add_function is the name of the function, and the name is fastidious, just like your name. The importance of naming in Python is to have a sense of what the function is to be seen from the name. From the name of Add_function, is it to be seen that she is used to calculate addition? (A,B) This bracket contains the parameter of the function, which is the function variable. Colon, this colon is very important, if less, the error. The meaning of the colon is to start the real function content below.

C=a+b Special Note that this line is indented four spaces than the previous line. This is the rules of Python, to remember, do not throw away, lose the error. Then this sentence adds two arguments (variables), and the result is assigned to another variable, C.

print C or remind reader to indent four spaces. Print out the value of the resulting C.

if name== "main": This sentence first copy, do not explain. The note is not indented.

add_function (2,3) This is the real call to the previously established function, and passed in two parameters: A=2,b=3. The way to carefully observe incoming arguments is to put 2 in the position of a and 3 in the B position (so the variable is the placeholder).

Finish the cow, make a summary:

The format of the declaration function is:

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

function body

Is it a simple style?

The knowledge of naming words

Some masters, through the name of a person to predict his/her fate and so on. It seems that the name of this thing is too important. A good name is a good omen. Therefore, Mr Qiu said, "The name is not right, the speech is not smooth", crooked Solution: The name is not regularized, it is not smooth. This is the crooked solution, hope does not affect reader correct understanding. I wonder if the masters can predict the great fortunes of foreigners through the names of foreigners.

In any case, some people are very concerned about the name of the next to a country does not seem to care.

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

File name: Full lowercase, underline allowed

Function name: lowercase, you can use underlined style words to increase readability. such as: Myfunction,my_example_function. Note: Mixed capitalization is only allowed when this style has taken advantage of the time to maintain backward compatibility.

parameter of the function: if the parameter name of a function and the reserved keyword (the so-called reserved keyword, that is, the Python language has been occupied by the name, is often used as a function, etc.) named, if you still use, you will not. Conflicts, usually using a suffix underline better than using abbreviations or odd spellings.

Variable: The variable name is all lowercase, and the underline connects each word. such as color = White,this_is_a_variable = 1.

In fact, there is still much debate on the question of nomenclature. The most typical is the so-called Hungarian nomenclature, hump naming and so on. If you like, you can Google it. The following information is available for reference:

Hungarian Naming Act
Hump-Case
Pascal Naming Act
The official name of the Python request, if reader English can, must read. If English is inferior, can read Chinese, do not need a ladder to be able to do? It's your life.

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.