Skip to Content
Course content

3: Using the Interactive Interpreter (REPL)

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've found that whenever I'm stuck on a logic problem or I can't remember exactly how a specific Python method behaves, the last thing I want to do is create a new file, save it, and run it through the terminal. It's too much friction. Instead, I just jump into the interactive interpreter—the REPL.

Just wanting a quick answer

Let's say I'm working on a project and I need to know what happens if I try to multiply a string by a number. I don't need a whole program for this. I'll just type python3 (or python depending on your setup) into my terminal. I see those three arrows—>>>—and I know Python is officially listening.

>>> "Python" * 3
'PythonPythonPython'

Notice how I didn't have to wrap that in a print() function? In a standard script, that line would do absolutely nothing visible. But here, the interpreter "evaluates" the expression and immediately "prints" the result. This is the core of the Read-Eval-Print Loop. It's a conversation.

Keeping a bit of state

Now, let's say I'm trying to figure out a pricing formula. I can treat the REPL like a scratchpad, defining variables on the fly to see how they interact. I'll set a base price and a tax rate, then try to combine them.

>>> price = 19.99
>>> tax = 0.07
>>> price * tax
1.3993

That's not quite a clean currency format. I can refine my approach immediately without restarting. I'll try the round() function right here to see if it gives me what I want.

>>> round(price * tax, 2)
1.4

Wait, 1.4? I was expecting 1.40. This is where the REPL shines. I've just discovered that Python's round() doesn't force trailing zeros for floats. Now I know that if I want "1.40" in my actual app, I'll need to use string formatting instead of round(). I saved myself ten minutes of writing a script only to be confused by the output.

The "Wait, why didn't that run?" moment

Here is where people usually get tripped up. I want to test a quick loop to see if I can filter a list. I'll type this in:

>>> numbers = [1, 2, 3, 4, 5]
>>> for n in numbers:
...     if n > 2:
...         print(n)
...

I hit Enter after the print(n) line and... nothing happened. I'm staring at the ... prompt. I might think the computer froze, but it hasn't. Python is just telling me, "You started a block (the loop and the if-statement), and I'm waiting for you to finish it."

Because the REPL relies on indentation to know when a block ends, I have to hit Enter one more time on a completely blank line to tell Python I'm done with the loop. Once I do that, the results pop out immediately.

3
4
5
>>> 

Getting out of here

Once I've got my answer or my logic is verified, I need to get back to my actual code. You can use exit() or quit(), but I've always preferred the keyboard shortcut Ctrl+D (on macOS/Linux) or Ctrl+Z followed by Enter (on Windows). It's faster and feels more like a "hard stop" to the session.




📋 Practical Task

Testing the Case-Insensitive User Handle

Imagine you are building a system where usernames should be treated as case-insensitive (so "DevUser123" is the same as "devuser123"). Before you write the actual registration logic in your script, use your interactive interpreter (REPL) to experiment and find the best way to handle this.

Your goal: Open your REPL and perform the following exploration:

  • Create a variable called stored_user and assign it the value "Admin_Python".
  • Create a second variable called input_user and assign it the value "admin_PYTHON".
  • Try comparing them directly using == and observe the result.
  • Experiment with the .lower() or .casefold() string methods on both variables within a comparison to see if you can make the result True.
  • Once you've successfully made the comparison return True, exit the REPL.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.