Learning Python version 2 Notes-Chapter 3 how to run your program

Source: Internet
Author: User
This article describes how to run a python program. There is not much valuable information, as shown below:

1. The first method is interactive coding, which is to input a statement in the python shell. The cost of doing so is that the Code will be executed once it is input, and can be used for fast unit testing (for example, if you want to know how to write a code to meet your needs, this mode can be used ). Note the following points: a) in this mode, you do not need to enter the print statement. You can print the expression and variable value B by directly inputting the expression or variable) in this mode, the entered Code cannot contain leading spaces, that is, it cannot start with spaces. c) How to input a composite statement, that is, this is a piece of code, not a sentence. When we enter a composite statement, the python shell prompt will change from >>> ..., then we can continue to input the statement. Remember that when the statement is input, enter a blank carriage return and tell the python shell that the statement is complete!

2. The second method is to write the code in a file. First, we can use python <FILENAME> to run this python program. Remember that the python program should set the extension. PY; second, Unix/Linux, which is unique in the same way as Bash. In the first line of the Code, enter #! /Usr/bin/python. In this way, you can directly execute the file by attaching the executable permission to the file, which is the same as Bash programming, you can write the first line of the Code as follows :#! /Usr/bin/ENV python, which is different from the previous one. In this way, the Env command searches for python executable programs in the current environment, we do not need to hard code the path of the python executable file to the code, which is also unique in Unix/Linux.

3. The third method is to directly double-click the. py file in windows. This is not enough. We basically do python in Linux.

4. Imports & reloads. This is very important. The import command can directly import a py file. The specific search path is described in Chapter 15th. For example, import script4. Note that script4 should not be written as script4.py. Import will automatically search for python files with the py extension. In python, every py file is called a module. Variables and functions in these files are called attributes in this module. through import, we can load this module, then access attributes. It should be noted that the Import action will only be performed once. That is to say, after we import script4, even if this script4 is modified, we will not respond if we execute import script4 again, because Python regards import as a very expensive action, it will only do it once! If you want python to re-load a module that has already been imported, use reload. The syntax is as follows: Reload (script4). Note that reload should reference script4 in parentheses, because reload is a function, and import is a statement! Reload will re-load script4, but during reload, script4 must be imported!

5. access module and attributes. After a module is imported, you can access the attribute in the module in two ways: (title is a variable in myfile. py ):

% Python # Start python.
>>> Import myfile # Run file; load module as a whole.
>>> Print myfile. Title # Use its attribute names: '.' to qualify.

% Python # Start python.
>>> From myfile import title # Run file; copy its names.
>>> Print Title # Use name directly: No need to qualify.

You can see that the first method is to use the full qualify method to reference the title. The second method uses the from keyword to directly reference the title.

6. After importing the module, you can use the Dir function to view all the attributes of the module, such:

>>> Dir (threenames)
['_ Builtins _', '_ Doc _', '_ file _', '_ name _', 'A ', 'B', 'C']

Threenames is threenames. PY, which has been imported. You can see the attribute in Dir and use two underscores to mark the built-in attribute. Each module will be preconfigured with these attributes by python, which is similar to C ++.

7. Embedded CILS. python can be embedded in many places, such as Python code being embedded in databases, webpages, and other program code. Here is an example of C calling Python:

# Include <python. h>
...
Py_initialize ();
Pyrun_simplestring ("x = brave + sir + Robin ");

If python. H is included, you can use the above functions to execute Python code. In short, python is compatible with many other languages.

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.