- interactive;
- batch.
Interactive mode can be accessed by typing
> python
or
> python3
This will start Python in interactive mode and wait for the user to enter commands. If you have a Python source code file and you want to run it, you must call the Python interpreter on the command line and pass your file as an argument. For example, for a file named test.py the launch procedure would look like this:
> python test.py
Table of Contents
Interactive mode
Open Python in interactive mode and type in the following:
print("Hello, World!")
And press ENTER. In response, the interpreter will execute the given string and display the result of its work one line below. Python can be used as a calculator for various calculations, and if you add the necessary math libraries, it becomes almost equal to such packages as Matlab, Octave, etc. Various examples of calculations are listed below. The arithmetic operations will be explained in more detail in the following lessons.
To exit interactive mode, type the command exit() and press ENTER. Included with the Python interpreter is IDLE (integrated development environment). It is essentially like an interpreter running in interactive mode with an extended set of features (syntax highlighting, object browsing, debugging, etc.). To run IDLE in Windows, go to the Python folder in the Start menu and find there a shortcut named “IDLE (Python 3.5 XX-bit)”.
In Linux, there is no IDLE shell by default, so you need to install it beforehand. To do this, if you have Ubuntu, type at the command line (for Python 3.4):
> sudo apt-get install idle-python3.4
This will install IDLE on your computer. To start the shell, type:
> idle-python3.4
Below is the appearance of IDLE on Linux.
Batch mode
Now let’s run Python in source file interpretation mode (batch mode). Create a file named test.py, open it with any text editor, and type the following code:
a = int(input()) print(a**2)
This program takes an integer as input and prints the square of it. To run it, at the command line, type
> python test.py
An example of how the program works is shown in the window below.
Summary
To run Python in interactive mode, you must type the name of the interpreter executable (python or python3) at the command line or run IDLE (integrated development environment). To run Python in batch mode, type the name of the interpreter and the file name of the Python program on the command line, separated by a space:
> python test.py