DEV Community

Cover image for Python REPL: Your Interactive Python Playground
Javi Palacios
Javi Palacios

Posted on • Originally published at fjp.es

Python REPL: Your Interactive Python Playground

Now that you have Python installed on your system, it's time to discover one of the most useful tools for learning and experimenting: the Python REPL.

REPL stands for Read-Eval-Print Loop, and it's an interactive environment where you can write Python code and see the results immediately. Think of it as a sandbox where you can play with Python without creating files.

What is the REPL?

The REPL is an interactive Python shell that:

  1. Reads your Python code
  2. Evaluates (executes) it
  3. Prints the result
  4. Loops back to wait for more code

It's perfect for:

  • Testing small code snippets
  • Learning Python syntax
  • Exploring built-in functions
  • Debugging quick ideas
  • Performing calculations

Starting the Python REPL

Open your terminal and type:

python
Enter fullscreen mode Exit fullscreen mode

You should see something like this:

$ python
Python 3.12.0 (main, Oct  2 2023, 12:00:00)
[GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Enter fullscreen mode Exit fullscreen mode

The >>> prompt means Python is ready to receive commands. You're now inside the REPL!

Your First REPL Commands

Let's try some basic examples:

Simple Calculations

>>> 2 + 2
4
>>> 10 * 5
50
>>> 100 / 4
25.0
Enter fullscreen mode Exit fullscreen mode

Notice how Python immediately shows you the result. No need to compile or run files.

Printing Text

>>> print("Hello from REPL!")
Hello from REPL!
>>> print("Python is awesome")
Python is awesome
Enter fullscreen mode Exit fullscreen mode

Variables

>>> name = "Alice"
>>> name
'Alice'
>>> age = 25
>>> age
25
Enter fullscreen mode Exit fullscreen mode

When you type a variable name, Python shows its value automatically.

Built-in Helper Functions

The REPL comes with powerful helper functions:

help() - Get Help on Anything

>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    ...
Enter fullscreen mode Exit fullscreen mode

Press q to exit the help screen.

dir() - Explore Available Methods

>>> dir(str)
['__add__', '__class__', '__contains__', ..., 'upper', 'zfill']
Enter fullscreen mode Exit fullscreen mode

This shows all the methods available for strings. Very useful when exploring!

type() - Check Data Types

>>> type(42)
<class 'int'>
>>> type("Hello")
<class 'str'>
>>> type(3.14)
<class 'float'>
Enter fullscreen mode Exit fullscreen mode

Multi-line Code in REPL

You can write multi-line code too. The prompt changes to ... for continuation lines:

>>> for i in range(3):
...     print(f"Number: {i}")
...
Number: 0
Number: 1
Number: 2
Enter fullscreen mode Exit fullscreen mode

Important: After entering the code block, press Enter on an empty line (just ... with nothing after) to execute it.

Practical REPL Tips

Tip 1: Use as a Calculator

>>> # Calculate 15% tip on a $50 bill
>>> bill = 50
>>> tip = bill * 0.15
>>> total = bill + tip
>>> total
57.5
Enter fullscreen mode Exit fullscreen mode

Tip 2: Test String Methods

>>> message = "hello world"
>>> message.upper()
'HELLO WORLD'
>>> message.title()
'Hello World'
>>> message.replace("world", "Python")
'hello Python'
Enter fullscreen mode Exit fullscreen mode

Tip 3: Quick Experiments

>>> # Check if a number is even
>>> number = 42
>>> number % 2 == 0
True
Enter fullscreen mode Exit fullscreen mode

Special REPL Variables

The REPL has some special variables:

>>> 10 + 5
15
>>> _
15
Enter fullscreen mode Exit fullscreen mode

The underscore _ holds the result of the last expression. Handy for quick follow-ups!

>>> 100 / 3
33.333333333333336
>>> round(_, 2)
33.33
Enter fullscreen mode Exit fullscreen mode

Common REPL Gotchas

1. Indentation Matters

>>> for i in range(3):
... print(i)  # ❌ IndentationError
  File "<stdin>", line 2
    print(i)
    ^
IndentationError: expected an indented block
Enter fullscreen mode Exit fullscreen mode

Fix: Always indent continuation lines with 4 spaces:

>>> for i in range(3):
...     print(i)  # ✅ Correct
...
0
1
2
Enter fullscreen mode Exit fullscreen mode

2. Empty Line to Execute Blocks

After writing loops or functions, press Enter on an empty line to execute:

>>> for i in range(2):
...     print(i)
... [Press Enter here]
0
1
Enter fullscreen mode Exit fullscreen mode

Exiting the REPL

There are several ways to exit:

>>> exit()
Enter fullscreen mode Exit fullscreen mode

Or:

>>> quit()
Enter fullscreen mode Exit fullscreen mode

Or press Ctrl+D (Linux/macOS) or Ctrl+Z then Enter (Windows).

REPL vs. Python Files: When to Use Each

Use REPL When... Use Files When...
Testing quick ideas Writing complete programs
Learning new syntax Creating reusable code
Debugging small pieces Working with multiple functions
Performing calculations Saving your work for later
Exploring libraries Collaborating with others

Practical Exercise

Open your REPL and try this step-by-step exercise:

# 1. Store your name
>>> name = "Your Name"

# 2. Store your age
>>> age = 25

# 3. Calculate birth year (approximate)
>>> birth_year = 2026 - age

# 4. Create a greeting
>>> greeting = f"Hello, {name}! You were born around {birth_year}."

# 5. Print it
>>> print(greeting)
Hello, Your Name! You were born around 2001.

# 6. Explore the string methods
>>> dir(greeting)

# 7. Try some string methods
>>> greeting.upper()
>>> greeting.lower()
>>> greeting.replace("Hello", "Hi")
Enter fullscreen mode Exit fullscreen mode

Pro Tips for REPL Productivity

Use Arrow Keys

  • Up/Down arrows: Navigate through command history
  • Left/Right arrows: Move cursor in current line

Import Modules

>>> import math
>>> math.pi
3.141592653589793
>>> math.sqrt(16)
4.0
Enter fullscreen mode Exit fullscreen mode

Clear Variables

To start fresh, just restart the REPL or:

>>> del variable_name
Enter fullscreen mode Exit fullscreen mode

What You Learned

In this lesson, you discovered:

  • ✅ What the REPL is and why it's useful
  • ✅ How to start and exit the Python REPL
  • ✅ How to use help(), dir(), and type()
  • ✅ How to write multi-line code in the REPL
  • ✅ When to use REPL vs. Python files
  • ✅ Practical REPL productivity tips

Next Steps

The REPL is your new best friend for learning Python. Use it often to:

  • Test examples from tutorials
  • Verify syntax before writing files
  • Explore new libraries
  • Debug small issues

In the next lesson, we'll dive into Variables and Basic Data Types, and you'll use the REPL to experiment with them interactively!


💡 Quick Challenge: Open your REPL and use help(len) to discover what the len() function does. Then try it with len("Python") and len([1, 2, 3, 4]). What do you notice?

Top comments (0)