Furious Python-Article 4-functions and modules, Article 4 python

Source: Internet
Author: User

Furious Python-Article 4-functions and modules, Article 4 python

PythonFunction: Actually, it is very similar to C ++, but because it is a weak language (it feels like it is ......), remove all types of declarations, and pass the value instead of reference (at least 2.7 is ), A small number of cells are defined by def (like macro definition ......), the following is an example.

Def sum (a, B): # sum is the function name. Pay attention to the colon ...... c = a + B # This part is the return c # Of the function's statement block. The function ends here, and print sum (1, 2) is the same as C ++) # Call a function based on the function name and output 1 + 2 values

There are alsoLambdaFunctions are similar to the features of C ++ 11. The preceding addition function example is provided.

Sum = lambda a, B: a + B # This is the definition of the lambda function. It starts with lambda and then is the input parameter. No matter how many parameters are separated by ",", and then ": "The following is the expression print sum (1, 2)

PythonModule: This part of feeling has something to do with Java. However, the module gives me the feeling that the header file and implementation file in C ++ are all written together. The following is an example.

First, the code of the add function module is provided. If there is a code in fun. py of the current directory

#!/usr/bin/pythondef sum(a, b):    c = a + b    return c

The code of the main function is provided. If the name test. py exists in the current directoryImportTo import the module fun (isn't this Java ......),

#!/usr/bin/pythonimport funprint fun.sum(1, 2)

Sometimes it is very troublesome. That fun is the same as the C ++ namespace, so we can actually pass the sumFrom... importThe statement is imported to the current space and the code is given.

#!/usr/bin/pythonfrom fun import sumprint sum(1, 2)

Sometimes there may be a path strength problem. For now, make sure that the imported module is in the same directory as the main program.


After reading the fourth version of the python learning manual, what should I read next?

In addition to reading books, python can learn more quickly by creating projects and exercises!
There are many ways to do python. You can start with the web, pick a web framework, and recommend the web. py, flask, started from the demo, You can slowly translate the learned knowledge into understanding.
Python source code parsing is a good book, but you just need to buy one if you have to have a foundation;
Python should be learned. It is more about manual projects. It is recommended that you start from the web project!

For beginners: built-in modules and functions of python32

Not afraid of repetition. In fact, it can be done without being underlined.
We mainly consider the code specification. We underline all anonymous functions and variables so that you can see the name.
Because there is no clear boundary between anonymous and public functions in python. (In my opinion, it is not guaranteed to be true)

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.