Working with the Python editor. General issues. Interactive and program modes

Working with the Python editor. General issues. Interactive and program modes


Contents


1. Python modes of operation. What modes of operation are supported by the Python integrated development environment?

Integrated Development Environment IDLE (Integrated Development Environment) Python supports 2 modes of operation:

  • interactive mode. In this mode, commands are executed immediately after their call. The result of the commands is immediately displayed on the screen (Figure 1). This mode of operation is well suited for writing simple programs and testing the operation of code fragments;
  • program mode. In this mode, the entire program is recorded first, and then this program is completely executed. Pre-program text must be saved. A Python program is named “script”.

Python window interactive mode

Figure 1. Python window interactively

Python window program mode

Figure 2. Python window in program mode

 

2. The features of interactive mode

In interactive mode, the interpreter executes the instructions and immediately displays the result. You cannot save these instructions to a file.

The interactive mode of the Python integrated environment is used to obtain instant calculations, conduct experiments, test programs “on the fly”.

In the interactive mode, it is convenient to perform experiments on small code fragments, which can then be transferred to scripts that are implemented in software mode.

 

3. How to input multi-instructions in interactive mode?

Instructions that have two or more lines in interactive mode must be completed with an additional blank line. This means that in order to complete a multi-line instruction, you must press the Enter key twice.

For example.

>>> x=3
>>> y=8
>>> if x>0: # multiline instruction
        y=5 # here you need to press Enter 2 times
>>>

The above code executes a multiline if statement. To exit the if statement, press the Enter key twice.

 

4. Features of the program mode

Program mode allows you to save programs for a long time. In program mode, the program text is first written to a file, then this file is executed in the integrated environment. Python files have the extension *.py and are called modules. Modules are simple text files. Code modules can be run as long as necessary. The Python interpreter executes all the program code in the module.

Module files that are launched for execution directly are also called scripts.



 

5. What are the disadvantages of interactive mode?

Interactive mode has the following interrelated disadvantages:

  • the program code that is entered is not saved anywhere. To re-run the same program code, it must be re-entered;
  • programs that are entered interactively disappear after execution by the Python interpreter. To perform them again, you need to type them again or use copy operations, which is inefficient.

 

6. How to call the program mode? How to run a program in program mode?

If the system is in interactive mode, the program mode can be invoked using the command

File->New File

as shown in Figure 3.

Python File New command

Figure 3. The “File->New” command

As a result, a new editor window will open in which you need to enter text in the Python language. Figure 4 shows the window of the file myprog1.py with the text of the program.

window program text Python

Figure 4. The window of the file myprog1.py with the text of the program in the Python language

To run the myprog1.py file, run the Run command from the Run menu or press the F5 key (Figure 5).

Python command Run window

Figure 5. The command Run

As a result of the file execution, the program will go to interactive mode with the following program execution result

============ RESTART: C:/Programs/Python/myprog1.py ============
10
>>>

 


Related topics