doctest [1] is a library that allows you to write python examples in comments or text documents. This is handy because you can write examples, test them, and have the peace of mind that they work.
Here are some examples example
>>> print("Hello World!")
Hello World!
>>> def one_plus_one():
... return 1 + 1
...
>>> one_plus_one()
2
As you can see, the key is to use the three arrows, >>>, to write the python that you would execute in the console. The result is written lined up after the >>> prompt. If you have a multi-line code sample, like when you are writing a function, you must enter the three dots, ... , and then line up the code as you would when writing python.
To test your code, you need to have a script that imports doctest. I looked it up on the internet. The solution I found was
% python3 << EOF
import doctest
doctest.testfile('pythons_doctest.md')
EOF
This is enough to get you going. Of course there is a lot more to it. Please consult [1] for more information.
One of the most awkward bits when writing about programming is writing examples in your articles and having to copy-and-past them into a script, run them, and then having to fix your article. It is not difficult. It is cumbersome. You leave a text editor to go into the console and you must add libraries and whatever you need to prepare to run the examples.
doctest makes it easier to write your examples in the docs. Or you could write the examples on the interactive console and copy-and-paste the expressions and results into the document you are writing. I first learned about these libraries from Elixir, which encourages writing documentation with examples that can be tested with their document testing library. As I started writing these small entries on Python, I decided that it would be handy to have examples that can be tested before I publish them.
References:
[1] doctest -- Test interactive Python examples docs.python.org https://docs.python.org/3/library/doctest.html (accessed, July 31, 2020)