Executing Your First Python Script

Since you are reading this, I 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 set up 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
  • Executing 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 exit() or quit() command. 
Starting the Interpreter
The easiest way to start the Interpreter is to open a terminal and use the Interpreter from the command line. You can open the terminal via the following methods. 
  • 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 or python3 and hitting 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 an Ubuntu terminal.
 Python Interpreter on Ubuntu
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.
Hello, World!
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 "Hello, World!":
  • We need to ensure we do see the >>> prompt
  • Type the command print("Hello, World!")
  • Hit return/enter key
The output from the Interpreter is shown in the next line. Note that the output line does not begin with >>>.
  1. >>> print("Hello, World!")  
  2. 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,
  1. >>> print("Hello, World!")  
  2. File "<stdin>", line 1  
  3. print("Hello, World!")  
  4. ^  
  5. 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 favorite text editor, create a new file called hello.py and add the following code,
  1. print("Hello, World!")   
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:
python hello.py.
Running a Python Script 
An interesting fact is that our script file does not require us 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 the 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.

Similar Articles