Here’s what you will learn in this Python REPL Basics tutorial:

  • Starting a Python interpreter session
  • Learn how to use Python REPL to execute Python instructions
  • Running our first Python script

So let’s get started.

The Rite of Passage (Hello, World)

It is common practice in the programming world to start with printing Hello, World on the console when starting with a new language.

And how can we do that in Python?

Well, it’s as simple as the below statement:

print("Hello, World")

In the next sections, we will explore different ways to execute this code.

Python REPL to use the Python Interpreter

The simplest way to get started with Python is to use the interactive REPL (Read, Evaluate, Print, Loop) environment. Basically, it means starting up the interpreter and typing commands directly in the console.

In this REPL environment, the interpreter:

  • Reads the commands entered by the user
  • Evaluates and executes the command
  • Prints the output to the console (if any)
  • Loops back and repeats the process

The session continues until we terminate the interpreter.

Starting the Interpreter

The easiest way to start the Python interpreter is to open a Terminal (in Linux or Mac) or Command Prompt (in Windows) and execute the command python.

In case your system also has Python2, the python command will start a Python2 environment. In order to start Python3 instead, we need to use the command python3. If multiple versions of Python are present, we may also need to specify the version in the command. For example, to open a Python3.6 environment, we need to use the command python3.6

Upon executing the python or python3 command, you should see the caret prompt (>>>). In case the prompt does not appear or there is an error message, it means that Python is not installed properly on your system.

python interpreter

See above example where we use python3 command to start a Python interpreter session. The triple-caret prompt is also ready for usage.

Executing First Python Command

If the prompt is visible, we are ready to use Python. The next step is to execute the Hello, World message.

To do so, we can simply type the command at the prompt and press enter.

>>> print("Hello, World")
Hello, World

If we see the Hello, World message printed, the first command is executed successfully.

Exiting the Interpreter

We can exit the interpreter REPL prompt by using the below command:

>>> exit()

This command will take us back to the normal terminal prompt.

On a Windows system, we can also use CTRL + Z and press Enter.

On a Mac or Linux system, we can achieve the same thing using CTRL + D.

If all else fails, you can also simply close the terminal window. However, that might seem rude!

3 – Some Common Mistakes while using Python REPL

While the above command seems trivial, there are still a few mistakes that can happen. Some of the common ones are as below:

  • Forgetting to enclose the Hello, World string in quotes.
>>> print(Hello, World)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Hello' is not defined
  • Not closing the quotation mark properly
>>> print("Hello, World)
  File "<stdin>", line 1
    print("Hello, World)
                        ^
SyntaxError: EOL while scanning string literal
  • Different opening and closing quotations
>>> print("Hello, World')
  File "<stdin>", line 1
    print("Hello, World')
                         ^
SyntaxError: EOL while scanning string literal
  • Forgetting the parantheses
>>> print "Hello, World"
  File "<stdin>", line 1
    print "Hello, World"
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello, World")?
  • Putting an extra whitespace before the command
>>>   print("Hello, World")
  File "<stdin>", line 1
    print("Hello, World")
IndentationError: unexpected indent

In case of any of the above errors or any other error, go back and verify the command.

Running your first Python script

While it is great to simply enter commands on the Python interpreter REPL view for quick testing and exploring any new features, it is not a practical choice for large scale systems.

As we create more complex programs, we will have many hundreds or even thousands of lines in a program. We would wish to edit and run them frequently.

Clearly, it is not feasible to re-type the code into the interpreter every time!

This is where we would want to save our code in a script file.

A Python script is a reusable piece of code. Essentially, it is simply a Python program kept in a file. We can run the file by specifying the name of the script file to the interpreter.

Python scripts are plain text files and we can edit them using any text editor of choice. Some popular ones are Visual Code or PyCharm or even plain old Notepad++.

Using the editor of choice, we can create a Python script named as hello.py. It will simply contain the Hello, World message we saw earlier.

print("Hello, World")

Once we save the file, we are ready to execute it. To do so, we can open a terminal window and go to the directory where we saved the file. Once there, we can simply execute the below command:

python3 hello.py

This will print the Hello, World message on the console.

IMPORTANT NOTE – It is not mandatory that a python script file should have the .py extension. The Python interpreter will be able to run the file. However, it is a good practice to use a proper naming convention for python file as it makes them easily identifiable.

Conclusion

With this, we have completed our Python REPL Basics tutorial and also execute commands using Python interpreter. We also looked at how to execute Python scripts using the interpreter.

In the next post, we will continue building our knowledge on Python by looking into Python data types and their usage.

If you have any comments or queries, please do not hesitate to mention in the comments section below.

Categories: BlogPython

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *