display()
display() shows an object in a notebook cell in its richest format, such as a rendered Markdown table instead of plain text.
Used in 20 lessons of the course, from 00.01.
display() comes from IPython (from IPython.display import display). In a notebook, print() always writes text. display() instead asks the object how it wants to look: if it has a method like _repr_markdown_, the notebook renders it as Markdown, with tables and bold text. If it has none, it shows the repr.
The course uses it like this: display(Markdown("| Term | ... " + rows)). You build a string with the table, wrap it in Markdown, and display draws it. Without display, a cell only shows the value of its last line.
display() is not in the standard library, so the examples below use a simple stand-in to show the idea.
Reach for it when: you want several outputs from one cell, or an object shown with rich formatting (Markdown, tables, HTML) instead of text.
Examples
Common mistake
display("hello")Why: Only IPython notebooks have display available without importing it. In a regular Python script the name doesn't exist.
How to write it: Write from IPython.display import display at the top, or use print() in regular scripts.
