59

I haven't been able to figure out what the >>> does, even though I often see it often in source code.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Dumle29
  • 809
  • 1
  • 6
  • 13
  • 4
    They mean you are running your code in a Python shell eg. on terminal or IDLE etc. I'm not sure that this qualifies as a question. They are a decoration... – jamylak May 07 '13 at 13:17
  • Besides the python command line prompt will print >>> where you type stuff, you shouldn't see this in valid source code. Please provide a links to an example. – nos May 07 '13 at 13:18
  • 1
    If you see them in source code they are just showing a shell session to demonstrate the code with examples inside of a comment – jamylak May 07 '13 at 13:18
  • 3
    Might also be a VCS conflict relict. – Tadeusz A. Kadłubowski May 07 '13 at 13:19

6 Answers6

44

You won't see it in source code, it's probably documentation. It indicates an interactive session, and things typed into the 'interpreter' are marked with this. Output is shown without the arrows.

In fact, the python documentation often has a button >>>at the top right of example code to be able to hide the arrows (and output) so that you can copy and paste the code.

Shown:
shown
Hidden:
hidden

askewchan
  • 45,161
  • 17
  • 118
  • 134
  • 11
    If you do see it in a docstring in source code, it might be part of a [doctest](http://docs.python.org/2/library/doctest.html). – rakslice Sep 13 '13 at 03:54
  • In case someone is looking for the sphinx package that allows that: https://sphinx-toggleprompt.readthedocs.io/en/latest/ – Heberto Mayorquin May 05 '22 at 13:07
20

'>>>' is the prompt of the interactive Python interpreter, meaning that the interpreter is ready to get Python statements typed in. It's occuring quite often in examples within the documentation of a Python program, in order to show which commands can be used and what will be the result of giving these commands to the interactive interpreter. For example, in a documentation of the print statement, one could give this example:

>>> print "Hello world."
Hello world.

This would be an actual snippet of a session with the interactive Python interpreter.

An interesting feature in IPython is that it ignores leading >>>, meaning that you can copy and paste code from such documentation without needing to remove the leading >>>:

In [1]: >>> print "Hello world."
Hello world.

(The prompt in IPython is In [n]:, where n is counting the interactive commands issued.)

silvado
  • 17,202
  • 2
  • 30
  • 46
  • 3
    Worth pointing out that in IPython, there is also the `Out[n]` which displays *return* values: `In [4]: 1+1`; `Out[4]: 2` (as opposed to printed values as in your example). The standard python interpreter does not make this distinction by default. – askewchan May 07 '13 at 18:10
  • Also, coming from a duplicate, if you want to run something like `python myscript.py` that's not something you would type at Python's `>>>` prompt. You probably want to exit Python (ctrl-Z on Windows, ctrl-D on Unix-like systems, or type `exit()` on either) and run the command from your system prompt (CMD or PowerShell on Windows, Bash or whatever on Unix-like systems). If you _really_ wanted to, there are ways to run a script in a file from the Python prompt, too, but let's not go there yet. – tripleee Jul 31 '20 at 12:34
7

Here are some of my findings on >>> and consequently ... complementing the previous answers.

You only see >>> when you are running Python in interactive mode prompting/asking the user for the "next command". Technical details here.


>>> and ... are not written in stone. These are stored in sys.ps1 and sys.ps2, and therefore can be changed. Further elaborated here.

>>> import sys
>>> sys.ps1 = "$ "
$

Every standard Python has this prompt unless you compile your own Python after changing >>> and ... to what you (sanely) wish to. Apart from that there seems to be a way to change it for all future interactive sessions by changing /usr/lib/python2.7/code.py but I couldn't find any success with it.

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
  • Since you brought up `...`, you should probably also mention that it is the symbol for `Ellipsis` in Python3. – kojiro Sep 21 '13 at 21:57
  • @kojiro I don't quite get your point(don't know much about Python3). But I intentionally tried to remain focused on `2.7` seeing the question tag. – Bleeding Fingers Sep 22 '13 at 11:52
3

The >>> prompt is the Python interpreter’s way of asking you, “What do you want me to do next?”, and it is called "chevron" prompt

0

In case you are trying to figure out how to exit a session, run this:

quit()
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • I don't know why anyone would want to type `quit()` then press Enter for this particular purpose, instead of just left clicking that obvious cross sign... – Ξένη Γήινος May 01 '23 at 18:01
-2

I found it to be called ' REPL'

  • 1
    No, the Read Eval Print Loop that Python enters when you run it interactively is called REPL, and the prompt in that REPL is `>>> ` by default. Perhaps one of the existing answers should be updated to clarify this. – tripleee Dec 15 '20 at 13:21
  • nice! i learnt something new, thanks for clearing that! – Rohit Athithya Dec 16 '20 at 08:52