Python from getting started to giving up (i): Introduction __python

Source: Internet
Author: User
One: Python features 1. Software Quality

1.Python programs have good readability, consistency and software quality.
2.Python supports advanced reuse mechanisms for software development, such as object-oriented 2. Improve developer Productivity

1.python as a development tool, to pay less attention to accomplish more tasks and stand out. 3. Can be defined as an object-oriented scripting language. 4. Have shortcomings, is not fast enough

Pure Python code runs at speeds between the traditional compiled language and the traditional interpretation language. 5. Free Portability 6. Easy to mix programming with other languages second: How Python runs the program

1.python is a programming language, and Python is also a software package called an interpreter (in fact, the interpreter is the logic layer between the code and the hardware), that is, we often say that Python is a programming language, but at the same time, When we wrote the Python code, the interpreter's name was also called Python, and the language and an interpreter was called a name between the programs, but the meaning was not confused.
2.Python installed on the machine, it contains some minimized components: an interpreter (shell) and a supported library. Anyway, the Python code you write needs to be executed in the interpreter.
3. From a programmer's point of view, a Python program is just a text file containing a python statement. 4. In essence, the execution of a Python program has the following steps 1. Byte code compilation

When the program executes, Python's interior (hidden to the user) first compiles the source code into the so-called bytecode form.
If the Python process has write access on the machine. Then he saves the bytecode of the program as a file with a. pyc extension. After the program is run, you can see the files under the appropriate working file directory. The next time you run the program, if the source code hasn't been modified before, Python will automatically load the. pyc file without recompiling the source file again.
If the python process does not have write permissions. Then the program can still run. Bytecode is generated in memory and discarded after the program is finished.
Bytecode files have an accelerated effect. So it's best to be able to write in a large program; Similarly, bytecode is one way of distributing programs: If Python finds all the. pyc files, then he will be happy to run the program. 2.Python Virtual Machine

Once the program is compiled into bytecode (or the bytecode is loaded from an existing. pyc file), subsequent bytecode is sent to the commonly called Python virtual machine PVM to execute.
PVM is the running engine of Python. He usually behaves as part of the Python system. And he is the component that actually runs the script. (But in fact, these are all hidden from the user.) 5. How to run the code 1. Interactive mode

The interactive hint mode runs the code based on the user's input and responds to the results. But he doesn't save the code in a file. Enter Python under Windows or Linux console and enter the interactive mode (first to install Python)

When you have any questions about the operation of a Python code, open the interactive command line and experiment the code to see what happens. Note: In interactive mode, you can only enter Python commands without being able to enter commands for the system. The display of many results in interactive mode does not require printing statements. But the print statements in the script need to be aware of the transformations and compound statements in the prompt. In interactive mode, Ends a compound statement with a blank line. means that a simple statement can run directly under a carriage return. The best way to handle multiple lines of code under a compound statement is to copy it to a. py file. Then run the file. Do not directly copy the statements of many lines directly to the interactive mode. 2. Scripting Mode

1. Normal script (without #!)
Write a file suffix. py End

How to run: Python + filename

2. Executable script (with #!)
The first line is specific: #! followed by the interpreter's path (without knowing the path, you can see where the interpreter's road strength is by which Python commands) The file has executable permissions (if you do not have execute permissions, and you give execution permission with the chmod +x file name), Then the script can be executed directly.

3.Unix env Tips
We know that there is a env program under Linux. You can locate something by setting up a system's search path. (If you don't know where the env is, use the command which ENV)
Since #! is followed by the interpreter's path, we often add the interpreter's path directly behind it, but if you send the program to someone else, if the path to the Python interpreter on someone else's machine is not the path we write, then the file will not be able to execute correctly.
So the env trick will be able to search Python's location, even if the machine is loaded with Python in a different location.

You can do this directly after adding permissions. three. Module import and Overload preliminary 1. Concept

1. Every python source file that ends with the extension. PY is a module, and other files can read the contents of the module by importing a module. (The essence of an import is to load another file and read the contents of which file.)
2. The contents of a module can be used by the outside world through such attributes.
3. Large programs are often in the form of multiple modules. A tool for importing other module files. One of the module files is designed as a master file, which is the top-level file through which the entire program can be run. 2. Operation 1. Load a module and run this module (***import statement ) *

1. Add the module name on the line, but never add the extension. py
2. Downloading a module in an interactive interface will automatically run this module
3. Module load first time will run, but "can not" load the same module two times

As shown above, load the first time automatically run, and then load the second time will not run. Because importing is a costly operation in Python, the system will only import one time by default and will not import the same again after the first import. (Even if the file is changed.,...) 2. Reload a module (reload () function)

1.reload is a function, in the IMP Standard library module (python3.x)
The 2.Reload () function expects the parameter to be a name for a module object that has already been loaded.

Attention:
1. Avoid using import and reload to start the program 3. Module Salient features: Properties

From a macro perspective, the module plays a role as a tool library. In general terms, modules are often encapsulated as variable names and are considered namespaces. Variable names in a package are called attributes. In a typical application, the import obtains all the variable names that are defined at the top level in the module file.
Example
1. Establishment of a document a.py

2. You can get the Leo attribute in this module in two different ways
①import loads the module as a whole

To use: module. Property name
②from XXX Import xxx

Use words: direct use on the line

Note: The module files are executed whether import or from is used. And the imported component has the right to read the variable name in the top-level file. 4. Run module files using EXEC

1.Exec (Open ("Full module name"). Read ()) built-in function call
2. Running the module without loading the module is equivalent to pasting the module's code at that place, and because of this, there is the risk of overwriting a variable of the same name as defined previously. (So be careful on your own line)

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.