Python (1)-install Python, program execution, Python module, and IDLE debugging

Source: Internet
Author: User
Tags windows x86

In recent years, with the extensive use of Python by Google, YouTube, IRobot, and other large companies, python has become an excellent programming language in terms of software quality, development efficiency, program portability, and support for standard libraries, it can complete various tasks for websites, game development, aerospace control, and other fields. This topic hopes to learn Python from scratch through the "Python learning manual" and related guidance on the website, and hope to help you.
Development Environment of this topic: Windows 7 + Python 2.7.2 + VS2008 (later integration)

Python (1)-install Python, program execution, and Python modules

1. Install Python
1. Download the Active Python installation package
Download Python based on your machine model, 32b for windows X86, 64b for window 64b
2. Run, install, successfully

 


2. Program Execution
3. Open IDLE (python GUI) and enter a simple script.


4. Traditional Python running mode:
Python Interpreter: a program that runs a Python program;
Python bytecode: The underlying format obtained by Python after compilation. Python automatically saves the bytecode as a file named. pyc;

The entered source code is converted to bytecode-> bytecode is run in PVM (Python Virtual Machine)-> the code is automatically compiled and then explained
Unlike C ++,
Python does not have the "build" or "make" steps. It runs immediately after the code is written.
Python bytecode is not a machine's binary code (so cannot run as fast as C ++, and its speed is between the traditional compilation language and the traditional interpretation language)

5. Alternative implementation of the Python Compiler:
CPython (Python C code writing and implementation is the standard implementation of Python)
JPython (integrated with Java programming environment, that is, Java Implementation of Python)
IronPython (integrated with the. NET Framework on windows, that is, the. NET implementation in Python)

6. Compile the. py file:
Open the editor pythonwin:

Create a new file, which exists on the desktop

Run the following command in cmd:

You can also run it directly in pythonwin. For the running result, see the "interactive window" window:


7. Other startup methods:
Method 1: Ctrl + R (run): python
Method 2: Another running method. We can see that double-click script1.py to flash the window. Similar to Ctrl + F5 during VC running, right? What should we do? (Skip this step unless in windows)
Here we add a sentence raw_input () to check the effect:

Save and double-click script1.py:

So what is raw_input?
In general, raw_input reads the next row of the standard input and waits until it is obtained. This is equivalent to a cin. Of course, its syntax is quite different from that of cin. raw_input () there are three methods:

The result of the first method is as follows:

The following two chapters will be discussed later.
However, if you double-click to open the SDK in this way, errors in the python file are not displayed. The generated error information is displayed through the system command line huozheIDLE. So try not to double-click to run the python program.

 


Iii. Python Module
8. What is a module?
What is a module?
Every. py file is a module. Other files can be imported into a module to read the content of this module, which is equivalent to include ...... A large program is usually in the form of multiple modules. One module File is designed as the main file (or top-level file ).

 

9. Module import:
Module location
Here we will make a simple example, which will be explained in more detail later. Here we put the script1 we just wrote in the position where we installed Python:


Then start-run-python:

 


10. import, from and reload
The module can be imported only once.
Import is a very costly operation, so that each program can only be imported once. To run it again, unless you call the built-in reload function:


The difference between import and reload is that import does not require parentheses and is a statement, while reload is a function, and its return value is a python module object.
A module plays the role of a tool Library: a module is often the encapsulation of a variable name and can be considered as a namespace. The variable name in a package is an attribute, or, attribute is the variable name bound to a specific object. A module can usually have two Python statements: import and from, and the use of the reload function.
The import Statement references a module as a whole, which is equivalent to introducing an object of a class. You can see the following results. Myfile is referenced as a module, and script1 can use the attributes of myfile.

Another reference method:

As shown in the figure, from and import are very similar, except that the latter adds an additional value to the variable name, that is, copying the attributes of the module. Therefore, the object can be imported with title instead of myfile.

Import & from summary:

 

Dir (myfile) is used to obtain all attributes in myfile. The prefix is the predefined Python built-in variable name.


11. Other concepts of the module:
The module is the largest program structure of the Python program.
Each module File is an independent and complete variable packaging, that is, a namespace
Execfile ('myfile. py') is another program execution method, but in essence it does not import modules.


12. Use IDLE to edit text in GUI:
Steps:
File-> New Window
Edit script
Run-> Run module (F5)
In addition, there are some precautions:
IDLE does not have the clear screen Option
If a connection error occurs, try to start IDLE in the mode of a single process. Switch to the idle directory in command line cmd and start it with idle. py-n (C: \ Python27 \ Lib \ idlelib> idle. py-n)


§ 4. IDLE debugging
13. debugging in IDLE:
Set a breakpoint in a line in the text: Right-click the line and choose Set breakpoint.
Debug-> Debugger in the Main Window
Run the command line by Step in Debug Control. For the variables, see the following.

 

========================================================== =====
Okay, I opened a header to give you a preliminary understanding of Python, in this chapter, we have reviewed the concepts and references of Python installation, program execution, and Python modules through 13 knowledge points, and briefly introduced the debugging of Python code in IDLE, hope to help beginners.

Author: abcjennifer

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.