Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Python Land
  • Python Tutorial
  • Courses
  • Blog
  • Contact
  • Log in
Home » Tips & Tricks » Use The Underscore in The REPL
A REPL shows 3 * 3 giving 9, then _ + 3 giving 12, while the 9 rests on an underscore-shaped shelf

Use The Underscore in The REPL

By Erik van Baaren · Published January 7, 2021 · Updated November 10, 2026

You can obtain the result of the last expression in a Python REPL with the underscore, _. It’s not an operator but a variable that the REPL fills in for you. In the Python REPL, this looks like:

>>> 3 * 3
9
>>> _ + 3
12Code language: Python (python)

This is handy when you use the REPL as a calculator, or when you call a function and forget to store the result in a variable. Instead of running it again, you grab it with _:

>>> sorted([3, 1, 2])
[1, 2, 3]
>>> numbers = _
>>> numbers
[1, 2, 3]Code language: Python (python)

Table of Contents

  • 1. When the underscore doesn’t change
  • 2. The underscore in IPython
  • 3. The underscore as a throwaway variable

When the underscore doesn’t change

The REPL only updates _ when an expression produces a value that it prints. A function like print() returns None, so _ keeps its old value:

>>> 3 * 3
9
>>> _ + 3
12
>>> print("hi")
hi
>>> _
12Code language: Python (python)

There’s one trap. If you assign something to _ yourself, you create a normal variable called _, and the REPL’s automatic version stops working:

>>> _ = 100
>>> 2 * 2
4
>>> _
100Code language: Python (python)

To get the old behavior back, delete your variable with del _.

The underscore in IPython

This works in the IPython shell too. In addition, the IPython shell allows you to use Out[n] to get the value of the expression In[n]. E.g., Out[1] would give us the number 9 in the example below:

In [1]: 3 * 3
Out[1]: 9

In [2]: Out[1] + 3
Out[2]: 12

In [3]: _ + 3
Out[3]: 15Code language: Python (python)

IPython goes one step further: __ (two underscores) gives you the second-to-last result, and ___ (three underscores) the one before that:

In [1]: 1 + 1
Out[1]: 2

In [2]: 2 + 2
Out[2]: 4

In [3]: 3 + 3
Out[3]: 6

In [4]: print(_, __, ___)
6 4 2Code language: Python (python)

The underscore as a throwaway variable

Outside the REPL, in your own programs, _ has no special meaning. It’s an ordinary variable name. By convention, Python programmers use it for a value they don’t need. For example, when you want to repeat something a few times, but don’t care about the loop counter, or when you unpack a tuple and want to skip one of the values:

for _ in range(3):
    print("Hello!")

name, _, age = ("Monty", "Python", 45)
print(name, age)Code language: Python (python)

Output:

Hello!
Hello!
Hello!
Monty 45Code language: Python (python)

That’s also why you shouldn’t use _ as a throwaway variable while working in the REPL: as you saw above, assigning to it switches off the “last result” feature. You can read more about the REPL, including how to use its history, in the Python REPL tutorial.

Erik van Baaren

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python! Writing good articles takes time and effort. Did you like this tutorial? You can buy him a coffee to show your appreciation.

More about Erik

Try this yourself, right now

Get more out of the Python REPL

The same topic as an interactive lesson: short explanation, then code you run and change in your browser, with instant feedback.

Open the interactive lesson

Free to start. Certificates and AI help with Guided.

Monty, your Python learning guide, smiling and waving

Leave a Comment Cancel reply

You must be logged in to post a comment.

Subscribe to my newsletter for Python news, tips, and tricks!

Footer subscribe

  • Home
  • Python Courses
  • Privacy Policy
  • About Us
  • Contact us
©2026 Python Land - All rights reserved
Python Land is not affiliated with Python.org or the Python Software Foundation
  • Free Tutorial
    • Install Python
      • Online Python Interpreter
      • How To Open Python on Windows, Mac, Linux
    • Introduction to Python
      • The Python REPL
      • Python Variable
      • Python String
      • Python Print function
      • Python Booleans
      • For-loop and While-loop
      • Python Functions
      • Your First Python Program
      • Python Comments
    • Creating Python Programs
      • The Best Python IDE
      • Installing VSCode
      • VSCode Python Extensions
      • VSCode GUI Tour
      • Python in VSCode: Running and Debugging
    • Classes and Objects in Python
      • Python Constructor
      • Python Inheritance
    • Structure Your Project
      • Python Modules And Importing
      • Python Packages
    • Python Data Types
      • Python Integer
      • Python Float
      • Python Tuple
      • Python List
      • Python Set
      • Python Dictionary
    • Language Deep Dives
      • Python Try Except (Exceptions)
      • Python Functions: Advanced Concepts
      • List Comprehension
      • Python Iterator
      • Python Range
      • Python Docstrings
      • Python pass (Do Nothing)
    • Interaction with the OS
      • Python Read And Write File
      • Python Subprocess: Run External Commands
    • Venvs / Package Management
      • Working With Python’s venv
      • Pip install: the Python package manager
      • Python Poetry: Package and venv Management Made Easy
      • Python Pipenv: Another Package Manager
      • Pipx: Safely Install Packages Globally
    • Python Concurrency
      • The Python GIL (Global Interpreter Lock)
      • Setting the Baseline
      • Python Threading
      • Python Multiprocessing
    • Data Processing with Python
      • Working With JSON
        • JMESPath Python: JSON Query Language
      • Python YAML: How to Load, Read, and Write YAML
      • Python CSV: Read And Write CSV Files
    • Migrating From Python 2 to 3
      • Check Python Version On The Command-Line
      • How to Migrate To Python 3
      • Python 3 Advantages
    • Using The Unix Shell
      • Basic Linux Commands
      • Bash Scripts
      • Using the Find Command in Linux
      • Unix Pipes
      • Process and Highlight JSON with jq
      • Using the Bash History
      • Bash Multiprocessing
    • Deployment
      • PyInstaller: Create An Executable From Python Code
      • How To Use Docker To Containerize Your Python Project
      • Automatically Build and Deploy Your Python Application with CI/CD
      • Guerrilla Scale Your Web Application
    • Data Science
      • Jupyter Notebook
      • NumPy: Getting Started Tutorial
    • Python Learning Resources
  • Python Courses
  • Blog
  • Login / Register