Python Tip #1- why add`>>>` in the Python example code?
2 min readDec 2, 2024
In many Python example code, we see sample Python code started with >>>
, like this:
why do people do this?
The >>>
symbols preceding the example code lines serve several purposes, largely rooted in conventions and readability enhancements:
Origins and Convention:
- Python Interpreter Prompt: In a Python interactive shell (e.g., IDLE, IPython, or simply running
python
in your terminal), the interpreter prompts the user with>>>
to input commands. This symbol has become synonymous with "execute this line in Python." - Documentation and Tutorial Conventions: Many Python documentation resources, tutorials, and books adopt this notation to indicate interactive shell examples. It visually distinguishes example code meant for execution from explanatory text.
Benefits in Example Code:
- Immediate Clarity: At a glance, readers understand that these lines are executable code snippets, rather than just illustrative text.
- Visual Separation: The
>>>
prefix provides a clear visual distinction between the example code and the surrounding explanatory text, enhancing overall readability. - Execution Hint: For newcomers to…