Python has two execution modes: interactive mode and script mode.
Interactive mode is a mode of operation in which the user can type Python statements and immediately see the results. To enter interactive mode, open a terminal and type python
. You will then be prompted with a prompt that looks like this:
Code snippet
>>>
You can type any valid Python statement at this prompt, and the interpreter will execute it and print the result. For example, the following statement will print the number 10:
Code snippet
>>> 10
10
Script mode is a mode of operation in which Python code is stored in a file and executed by the interpreter. To run a Python script, open a terminal and type the following command:
Code snippet
python filename.py
where filename.py
is the name of the Python file you want to run. The interpreter will then execute the code in the file and print any output.
Here is a table that summarizes the differences between interactive mode and script mode:
Feature | Interactive mode | Script mode |
---|---|---|
How to start | Open a terminal and type python | Open a terminal and type python filename.py |
How to enter code | Type statements at the prompt | Write code in a file |
How to execute code | Press Enter | Run the file with the python command |
Output | Printed at the prompt | Printed to the console |
Which mode you use depends on your needs. If you want to experiment with Python or quickly test a piece of code, interactive mode is a good choice. If you want to write a longer piece of code or save your code for later use, script mode is a better choice.