Learn the powerful functions of Python forever and learn python

Source: Internet
Author: User

Learn the powerful functions of Python forever and learn python

For humans, the ability to develop to this level of mathematical thinking is a leap. It can be said that its proposal has directly accelerated the development of modern science and society. functions have been widely used no matter in any modern science and technology category, or even in economics, political science, and sociology.

The following section is from Wikipedia (in this tutorial, a large number of definitions come from Wikipedia, because it is really an encyclopedia): function entry

This mathematical term of the function was used by laveniz in 1694 to describe a correlation volume of the curve, such as the slope of the curve or a point on the curve. The functions referred to by laveniz are now called bootable functions. Ordinary people outside of mathematicians generally come into contact with such functions. For a function, we can discuss its limits and derivatives. The two describe the relationship between changes in function output values and changes in input values, which is the basis of calculus. The Chinese term "function" was translated by Qing Dynasty mathematician Li Shanlan. The book on behalf of mathematics explains: "Where the function of this change (including) is another change, this is another function ".

Functions, ranging from simple to complex. The function entry provided in Wikipedia can be used as an overview. However, any function can be summarized as follows:

There is a general understanding of junior high school mathematics. I will not go into details here.

This article focuses on how to use python for a function.

Deep understanding of functions

In middle school mathematics, you can define a function in this way: y = 4x + 3, which is a lift function. Of course, you can also write it as f (x) = 4x + 3. X is a variable, which can represent any number.

When x = 2, it is substituted into the above function expression: f (2) = 4*2 + 3 = 11 so: f (2) = 11

The above understanding of functions can be achieved by junior high school students. However, if you have graduated from junior high school or a junior high school student, you must expand the understanding of functions.

Variable is not just a number

Can variable x be any number? In fact, a function is a ing. The viewer tries to understand the expression x as a pie, 4x + 3, that is, adding 3 to four pies (what is the unit, it doesn't matter ), this result corresponds to another thing, such as the iphone. Or, it can be understood that 4 pies plus 3 correspond to an iphone. This is the so-called ing relationship.

So, x is not just a number, but anything you think.

Variable nature-placeholder

Why is x used in the function variable? This is an interesting question. google it to see if you can find the answer.

I don't know why. However, I know clearly that variables can use x or other symbols, such as y, z, k, I, j ..., you can even use letters like alpha, beta, qiwei, and qiwsir.

A variable is essentially a placeholder. This is a straightforward understanding. What is a placeholder? It is to first occupy the position with variables, indicating that there is something here. As for what to put in this position, we will discuss it later, and use a symbol to occupy this position (placeholder ).

In fact, in advanced language programming, variables are more complex than learning in junior high school mathematics. But now, let's leave the complicated things behind. Now, we will study the variables in python Based on junior high school mathematics.

Generally, lower-case letters are used to name the variables in python. You can also add underscores to the variables to indicate differences.

For example, alpha, x, j, p_beta can all be used as python variables.

Assign values to variables

Open IDLE and perform the following operations:

>>> A = 2 # Note 1 >>> a # note 22 >>> B = 3 # Note 3 >>> c = 3 >>> b3 >>> c3>>

Note:

NOTE 1: The meaning of a = 2 is to point a variable to the number of 2, just like a is a fisherman who connects to a fish called 2 through a fishing line, a can be directed to 2 through the fish line
NOTE 2: It is equivalent to the person who wants to fish a. Export the fish along the fishing line to see which one is connected and find the silly fish called 2.
NOTE 3: B = 3. Same as above. So what about c = 3? The fish named 3 was caught by two people at the same time.

Create a simple function

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

The functions created in this way are no different from those learned in junior high school mathematics. Of course, this function is not very useful in programming practice. It is generally used to understand the function in the learning stage.

Don't be impatient. You are entering a = 3 and then entering y to see what the result is?

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

Isn't it strange? Why is a equal to 3 followed by y or 8.

The previous phishing theory can be used to explain the problem. A and 2 are connected. After calculation, y and 8 are connected. The connection object of a is modified, but the connection object of y has not changed. Therefore, it is still 8. After calculation, the connection object of y changes:

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

Note that if the expression is not a = 2 first, an error is reported.

>>> y=3*a+2Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined

Note that the error message is displayed. a is a variable and the prompt tells us that this variable is not defined. Obviously, if a variable is to be used in a function, it must be defined in advance. The definition method is to copy the variable.

Create practical functions

The above command is not enough to create a function, so write a. py file.

In IDLE, File-> New window

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 name her 106-1.py and you will get a name based on your preferences.

Then I enter the folder and run the file. The following result is displayed,

What is the result of your running? If the above result is not obtained, check the code very seriously, whether it is exactly the same as what I wrote. Note that, including colons and spaces, all have the same. Colon and space are important.

Start Ding Jie Niu:

# Coding: UTF-8 declares that the character set type of the Code in this file is UTF-8. If you do not understand it yet, you can go to google and just copy it first. I will explain it later. Def add_function (a, B): Here is the start of the function. When declaring a function, you must use def (def is the first three letters of English define) to inform the computer. Here, you need to declare a function; add_function is the name of this function. The name is exquisite, just like your name. In python, it must be meaningful to get a name. We can see from the name what this function is. From the add_function name, does it show that she is used to calculate addition? (A, B) the parameter in the brackets is the function variable. This colon is very important. If it is missing, an error is returned. The colon (:) means starting the real function content below. C = a + B Note that this line is Indented by four spaces than the previous line. This is a python rule. Keep in mind that if it is lost, an error is returned. Then, the two parameters (variables) are added, and the result value is assigned to the other variable c. Print c reminds the reader to note that four spaces are indented. Print the value of Result c. If name = "main": do not explain this sentence. Note that it is not indented. Add_function (): This is the real call to the previously created function, and two parameters are input: a = 2, B = 3. The method of passing in parameters is to put 2 at the position of a and 3 at the position of B (so the variable is a placeholder ).

After the solution is completed, make a summary:

The declaration function format is:

Def function name (parameter 1, parameter 2,..., parameter n ):

Function body

Is style very simple?

Learning by name

Some masters predict his/her bad fortune by a person's name. It seems that the name is too important. If you have a good name, there will be a good sign. So Mr. Kong said, "the name is not correct, and the words are not smooth. This is a poor solution. I hope it will not affect the official understanding. I don't know if masters can predict the bad fortune of a foreign national congress by the name of foreigners?

In any case, a Chinese person cares about his name, and a country next to him does not seem to care about his name.

Python also cares about name issues. In fact, all advanced languages have requirements for names. Why? If the name is messy, the computer will be overwhelmed. See the general naming requirements of python.

File Name: All in lower case. The name can contain underscores (_).

Function Name: lower case. You can use underlined words to increase readability. For example, myfunction and my_example_function. Note: The mixed case is only allowed when this style is already dominant in order to maintain backward compatibility.

Function parameters: If the parameter name of a function and the reserved keyword (the so-called reserved keyword is the name occupied by the python language, it is usually used as the name of an existing function, if you still use it, it won't work .) Conflict. It is usually better to use a suffix underline than to use abbreviations or odd spelling.

Variable: all variable names are in lower case, and each word is connected by an underscore. For example, color = WHITE, this_is_a_variable = 1.

In fact, there are still a lot of arguments about naming? The most typical examples are Hungary naming and camping naming. If you like, you can google it. The following content is for reference:

Hungarian naming convention
Case sensitivity
Pascal naming convention
The official requirements for python naming are as follows. If the English is a little inferior, you can read Chinese. Can you do it without a ladder? It depends on your life.


Python has many functions similar to _ init _.

_ Init _ is the class constructor.
_ Le _ ne _ is an operator function.
_ Le _ Yes <=
_ Ne _ Yes! =
Overloading these two functions is equivalent to overloading operators.

Explain the execution process of this Python Function

It is indeed recursive. When executing the command, one yield is passed to another yield, which seems inefficient.
This is similar to the example given at the end of this article:
Www.ibm.com/..rm-20/

In other words, listing = [I for I in fun] seems to be a List = List (fun)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.