002 using the Python Interpreter

Source: Internet
Author: User

2. Use the Python interpreter 2.1. Invocation of the Python interpreter

Python interpreter is usually installed in the machine /usr/local/bin/python3.6 directory, if the directory is available, use your Unix shell to search the /usr/local/bin directory and type the following command to start: [1]

python3.6

Since the choice of the directory where the Interpreter lives is an installation option, and other places is possible; Check with your local Python guru or system administrator. (e.g., is /usr/local/python a popular alternative location.)

On Windows machines, the Python installation are usually placed in, though you can change this if you C:\Python36 ' re running th E installer. To add this directory to your path, you can type the following command into the command prompt in a DOS box:

Path=%path%;  C: \python36      

Typing an end-of-file character (control-d on Unix, control-z on Windows) at the primary prompt Causes the interpreter to exit with a zero exit status. If that doesn ' t work, you can exit the interpreter by typing the following command: quit () .

The interpreter ' s line-editing features include interactive editing, history substitution and code completion on system s that support ReadLine. Perhaps the quickest check to see whether command line editing are supported is typing < Span class= "Pre" >control-p to the first Python prompt get. If It beeps, you are command line editing; See Appendix interactive Input Editing and history Substitution for a introduction to th e keys. If nothing appears to happen, or if ^p is echoed, command Line editing isn ' t available; You'll have only been able to use BACKSPACE to remove characters from the current line.

The interpreter operates somewhat like the Unix shell:when called with standard input connected to a TTY device, it reads and executes commands interactively; When called with a file name argument or with a file as standard input, it reads and executes a script from that File.

A second The starting the Interpreter python -c command [arg] ... is, which executes the statement (s) in command, analogous to the S Hell ' s -c option. Since Python statements often contain spaces or other characters that's special to the shell, it's usually advised to Q The Uote command is entirety with a single quotes.

Some Python modules is also useful as scripts. These can invoked using python -m module [arg] ... , which executes the source file for module as if you are had spelled out it full NA Me on the command line.

When a script file was used, it was sometimes useful to being able to run the script and enter interactive mode afterwards. This can is done by passing -i before the script.

All command line options is described in commandline and environment.

2.1.1. Argument passing

When known to the interpreter, the script name and additional arguments thereafter is turned into a list of strings and a Ssigned to theargvVariable in thesysModule. You can access the this list by executingimport sys. The length of the list is at least one; When no script and no arguments is given,sys.argv[0]is an empty string. When the script name is given as‘-‘(meaning standard input),sys.argv[0]is set to‘-‘. When-c Commandis used,sys.argv[0]is set to‘-c‘. When-m Moduleis used,sys.argv[0]is set to the full name of the located module. Options found after-c CommandOr-m Moduleis not consumed by the Python interpreter ' s option processingsys.argvFor the command or module to handle.

2.1.2. Interactive Mode

When commands was read from a TTY, the interpreter was said to being in Interactive mode. In this mode it prompts for the next command with the primary prompt, usually three Greater-than signs ( >>> ); f or continuation lines it prompts with the secondary prompt, by default three dots ( ... ). The Interpreter prints a welcome message stating its version number and a copyright notice before printing the first Promp T:

$ python3.6Python 3.6 (default, SEP, 09:25:04)[GCC 4.8.2] on LinuxType ' help ', ' copyright ', ' credits ' or "license" for more information. >>>   

Continuation lines is needed when entering a multi-line construct. As an example, take a look at this if statement:

>>>
Truethe_world_is_flat:print("Be careful not to fall off!" )... Be careful not to fall off!       

For more on interactive mode, see interactive mode.

2.2. The interpreter and its Environment2.2.1. Source Code Encoding

By default, Python source files is treated as encoded in UTF-8. In so encoding, characters of most languages in the world can be used simultaneously in string literals, identifiers and Comments-although The standard library is uses ASCII characters for identifiers, a convention and any portable code should follow. To display all these characters properly, your editor must recognize that the file was UTF-8, and it must use a font that s Upports all the characters in the file.

To declare an encoding and than the default one, a special comment line should is added as the first line of th E file. The syntax is as follows:

#-*-Coding:encoding-*-

where encoding is one of the valid codecs supported by Python.

For example, to declare that Windows-1252 encoding are to being used, the first line of your source code file should be:

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

One exception to the first line rule was when the source code starts with a UNIX "Shebang"line. In this case, the encoding declaration should is added as the second line of the file. For example:

#!/usr/bin/env Python3#-*-coding:cp-1252-*-

Footnotes

[1] On Unix, the Python 3.x interpreter are by default not installed with the executable named python , so this it does not CONFL ICT with a simultaneously installed Python 2.x executable.

002 using the Python Interpreter

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.