Familiar with Python interpreter Interpreter

Source: Internet
Author: User

1. Start the python Interpreter

2. Two modes of the python Interpreter

3. handle errors

4. Set the python interpreter to startCode

5. Execute the python Module

5.1 Python file Annotation

5.2 how to write Chinese comments

5.3 how to execute the. py file

<1>. Start the python interpreter;

In the previous article, we installed python, installed the pydev plug-in under eclipse, and were familiar with the development environment of this IDE, here we will look at how to play Python (in Windows) under the command line ). The first step is to start the interpreter. If an environment variable is set in Windows, you can directly type python in cmd, or run cd in the python installation directory. /python.exe.

<2>. Two modes of the python interpreter;

After you start the python interpreter in the previous step, the interactive mode is enabled by default, and the interpreter is waiting for user input. The Python interpreter allows a "command" to be written in multiple lines of books. The Python interpreter will use the prompt... as follows:

D: \ pythonwork> pythonpython 2.7.1 (r271: 86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] onwin32type "help", "Copyright ", "Credits" or "License" for more information. >>> the_word_is_flat = 1; >>> If the_word_is_flat: ... Print ("be careful not to fail off "); ... Be careful not to fail off

>>>

<3>. handle errors;

If the interpreter encounters an error while parsing the command, interpreter prints the error message and a stack trace. Similar to other languages, error handling also exists in Python, which will be described later.

<4>. Python interpreter startup code the interactive Startup File

;

The Python interpreter can set the command to run at startup by setting the environment variable pythonstartup, as shown below:

 

Starup. py file:

Print ("Python interpreter is going to start! ");

At this time, the result of restarting the python interpreter is as follows:

D: \ pythonwork> Python

Python 2.7.1 (r271: 86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] onwin32type "help", "Copyright ", "Credits" or "License" for more information. Python interpreter is going to start! >>> <5>. Execute a python module;

5.1 Python file Annotation

Two types of annotations can be used in a python file. For a single line annotation, you can use:

# This is a comment

A = 10;

For multi-line comments, you can use ''':

''' Python tourial: An informal introduction to Python

''' 

5.2 how to support Chinese annotations

You can set the encoding method of the. py file to support Chinese annotations:

#-*-Coding: UTF-8-*-# The above statement must be added to indicate the encoding method # Of the file. Otherwise, the command line will return the import sys error;

Print (SYS. argv [0]);

5.3 execute a. py file

If a. py file has been compiled, run the following command: Python +. py file name:

D: \ pythonwork> Python startup. py

You can also pass parameters to the file:

D: \ pythonwork> Python startup. py"Arg1"

Python interpreter is going to start!

Parameters passed to the file can be obtained through SYS. argv:

#-*-Coding: UTF-8 -*-

''' Print the command line parameter '''import sys; print (SYS. argv [0]);

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.