Python interpreter collection of detailed answers

Using Python interpreter

 

2.1 Calling Python interpreter

Python interpreter is usually installed in the target machine's  /usr/local/bin/python3.5 directory. The  /usr/local/bin directory contains the search path into the Unix shell's to make sure you can enter:

python3.5

Command to start him. [1]  As the installation path of Python interpreter is optional, it may also be other path, you can contact the Python guru or system administrator to confirm (for example, /usr/local/python is a common choice).

On a Windows machine, Python is usually installed in  C:Python35 position, of course, you can modify this value when you run the installation wizard. To add this directory to your PATH environment variable, you can enter the following command in a DOS window:

set path=%path%;C:python35

Usually you can enter an end of file (Unix systems in the main window  Control-D, Windows systems  Control-Z) allow the interpreter to exit 0 status code. If that does not work, you can enter the  quit() exit command interpreter.

Python interpreter has a simple line editing. On Unix systems, any Python interpreter may have added support for the GNU readline library, so you have a more elaborate interactive editing and history functions. Input Control-P may be the easiest way to check whether the command line editing support Python main window. If the beeps (computer speakers), then you can use the command-line editing; introduce more shortcuts, refer to the  interactive input line editing history back . If there is no sound or display  ^P characters, command line editing is not available; you can only delete and re-enter the characters from the current line typed by the backspace key.

Some operations similar Python interpreter Unix shell: when a terminal device (TTY) as standard input calls, it interprets and executes interactive command; when using a file name or a file as the parameter to call the standard input, it reads the file and file as a  script  execution.

The second way is to start the Python interpreter  python -c command [arg] ..., this method can  command line  execute Python statements, similar to the shell's  -c  option. Since Python statements often contain spaces or other special shell characters, generally recommended  command  in single quotes wrapped up.

Some Python modules can also be used as scripts. You can use the  python -m module [arg] ... command to call them, which is similar to type the full path name in the command line execution  module  source file the same.

When you use a script file, often run the script and enter interactive mode. This can also be before the script by adding  -i  to achieve parameters.

 

2.1.1. Parameter passing

When the interpreter, the script name and additional parameters passed in a named  sys.argv list of strings. You can be obtained by performing this list  import sys, the list length is greater than or equal to 1; and when no parameters given script, it also has at least one element: sys.argv[0] is an empty string. Script name is specified  '-' (indicated standard input),  sys.argv[0] it is set  '-'using the  -c  command  when sys.argv[0] is set  '-c'. Use  -m  module  parameter, sys.argv[0] is set to specify the full name of the module. -c  command  or  -m  module  after the parameter will not be intercepted by the Python interpreter's option processing but left in  sys.argv , the script for command operations.

 

2.1.2. Interactive mode

When reading from the tty command interpreter is said to work in  interactive mode . In this mode it is performed according to the main prompt, usually three main prompt greater than ( >>>); for continuation is called  secondary prompt by three dots ( ...). Before the first row, the interpreter prints a welcome message stating its version number and a copyright tips:

$ python3.5
Python 3.5.2 (default, Mar 16 2014, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

When the slave needs to prompt input multi-line structure, e.g., the following  if  statement:

>>> the_world_is_flat = 1
>>> if the_world_is_flat:
...     print("Be careful not to fall off!")
...
Be careful not to fall off!

For more content in interactive mode, see the  interactive mode .

 

2.2. The Interpreter and Its Environment

 

2.2.1 source coding

By default, Python source file is a UTF-8 encoding. In this code, the character most of the world's languages ​​can be used simultaneously in string identifiers and comments - in spite of the Python standard library use only ASCII characters as identifiers, which just about any portable code should comply with the agreement. If you want to display all the characters correctly, your editor must be able to recognize the file encoding is UTF-8, and it uses the font file to support all of the characters.

 

I point to learn more exciting content

You can also specify a different character encoding for the source file. To this end, the  #! row (first row) after the insertion of at least one row special comment line to define the source code files:

# -*- coding: encoding -*-

With this declaration, the source file will be treated as all things with  encoding  refer to the UTF-8 encoding treat. In the Python Library Reference  codecs  section you can find a list of available codes.

For example, if your editor does not support UTF-8 encoded file, but supports a number of other Windows-1252 encoding of images, you can define:

# -*- coding: cp-1252 -*-

I point to learn more exciting content

So you can use all the characters in Windows-1252 character set in the source file. This special code must be in the annotation file  of the first or second  line definition.

Footnotes

[1] On Unix systems, Python 3.X interpreter is not installed by default fame  python of command, so it will not work with Python 2.x installed in the system command conflict.
Published 29 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/HAOXUAN168/article/details/104087301