Since you are here, I would assume that you have a working Python setup along with a working Python interpreter, and are ready to run your first Python script. If not, check out my previous articles, where I walk you through each step to setup your Python environment. Here are the links to those articles:
In this article we will be talking about:
- using Python interpreter to run our code
- execute a Python script contained in a file from the command line
The Python Interpreter
Python interpreter is the most commonly used way to get started with Python for beginners. The Python interpreter is a Read-Eval-Print-Loop (REPL) that simply takes commands, evaluates them and prints the output.
- Reads the command
- Evaluates and executes the command
- Prints the output
- Loops back and repeat the process
The interpreter continues until we instruct it to terminate using the
Starting the Interpreter
The easiest way to start the interpreter is open up a terminal and use the interpreter from command line. You can open the terminal as:
- on Windows, search for Command Prompt or Powershell.
- on Linux or Mac, search for Terminal.
Once the terminal is open, you can start the interpreter by typing
python and hit return. If the paths have been set by the Python install process, you should see a response from the Python interpreter. Given below is an example from Ubuntu terminal:
The >>> shows that we are interacting with the Python interpreter. If you don’t see that, you may want to revisit the installation steps in the above given links.
Now that we are all set, let’s continue following a long going tradition of writing a program that prints
Hello, World! on the console. In its simplest form, the following Python steps display
- we need to ensure we do see the >>> prompt
- type the command
- hit return/enter key
The output from the interpreter is shown in the next line. Note that the output line does not begin with >>>.
>>> print("Hello, World!")
It’s important to remember that Python does take indentation into account while interpreting a command. So if we have some extra space before the print command, we will get an error as shown below:
>>> print("Hello, World!")
File "<stdin>", line 1
IndentationError: unexpected indent
Running Python script from Command Line
Using the interpreter is great for quick exploration of features and functionality. However, when we work on a larger problem, we write much larger code and we do not want to type that code over and over again. This is where we want to create a script file.
A Python script is a reusable set of code which is essentially a Python program—a sequence of Python instructions—contained in a file. You can run the program by specifying the name of the script file to the interpreter. It’s time to create our first script file.
On your system, open up your favourite text editor, create a new file called
hello.py and add the following code:
Start the terminal and navigate to the directory where you had saved the script file. We can now execute the script by simply specifying the filename as a command line argument to the Python interpreter:
An interesting fact is that our script file does not require to have a
.py extension. The Python interpreter will run the script no matter what it is called. However, giving Python script files a
.py extension is a useful convention as it makes them more easily identifiable.
The Python interpreter is very useful, whether we want some help or we want to run some Python script. However, to increase code reusability and to solve a large problem we are required to write a Python script. This article was to just get you started with Python interpreter and the simplest Python script. In the next article, we will see how helpful the Python interpreter is and how we can effectively use it.