LAUNCHING PYTHON ON YOUR MACHINE
There are three different ways to launch Python:
- Use the Python Interactive Interpreter.
- Launch Python scripts from the command line.
- Use an IDE.
The next section shows you how to launch the Python interpreter from the command line, and later in this chapter you will learn how to launch scripts from the command line and also about IDEs.
NOTE The emphasis in this book is to launch Python scripts from the command line or to enter code in the Python interpreter.
The Python Interactive Interpreter
Launch the interactive interpreter from the command line by opening a command shell and typing the following command:
python
You will see the following prompt (or something similar):
Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec 7 2020, 12:44:01) [Clang 12.0.0 (clang-1200.0.32.27)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Now type the expression 2 + 7 at the prompt:
>>> 2 + 7
Python displays the following result:
9 >>>
Press ctrl-d to exit the Python shell.
You can launch any Python script from the command line by preceding it with the word “python.” For example, if you have a script myscript.py that contains commands, launch the script as follows:
python myscript.py
As a simple illustration, suppose that the script myscript.py contains the following code:
print('Hello World from Python')
print('2 + 7 = ', 2+7)
When you launch the preceding script, you will see the following output:
Hello World from Python 2 + 7 = 9