Running Your First Python Script
Python is a powerful and popular language for data science and big data. In this article, we show you how to get started using the language.
Join the DZone community and get the full member experience.Join For Free
Since you are here, I'll 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 a Python interpreter to run our code.
- executing Python script contained in a file from the command line.
The Python Interpreter
The 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 to open up a terminal and use the interpreter from the command line. You can open the terminal:
- 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 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 Ubuntu terminal:
Python Interpreter in Ubuntu
>>> shows that we are interacting with the Python interpreter. If you don't see that, you may want to revisit the installation steps given in the above links.
Now that we are all set, let's continue following the long-standing 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 that we see the
- 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!") 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("Hello, World!") File "<stdin>", line 1 print("Hello, World!") ^ 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 more 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:
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:
Running a Python Script
An interesting fact is that our script file does not require an extension. The Python interpreter will run the script no matter what it is called. However, giving Python script files an 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 the Python interpreter and Python scripts. In the next article, we will see how helpful the Python interpreter is and how we can effectively use it.
Opinions expressed by DZone contributors are their own.