Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Analyze Categorical Data

Avatar image for pnmcdos

pnmcdos on April 8, 2022

Why is it that we can print the variables without having to write print? Line 24 cat_totals for example. Is this a Jupyter functionality?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on April 8, 2022

@pnmcdos It’s somewhat similar to a regular Python interpreter. When you run the python command, you’ll start an interactive Python interpreter session known as REPL (Read-Evaluate-Print Loop). It reads your commands, evaluates them, and prints the corresponding results immediately onto the screen.

It does so by calling repr() on the last evaluated expression, behind the scenes, in order to turn that expression into a textual representation. For example, a date object will have the following representation:

>>> from datetime import date
>>> date.today()
datetime.date(2022, 4, 8)

It’s almost the same as if you called repr() against that object yourself:

>>> repr(date.today())
'datetime.date(2022, 4, 8)'

The only difference is that the string will be enclosed in single quotes.

Now, when you call print() on something, in turn, it will call str() on that object for you, which might produce a slightly different textual representation:

>>> print(date.today())
2022-04-08

>>> str(date.today())
'2022-04-08'

Jupyter Notebooks take the same idea, but instead of producing textual output, they can leverage much more visually appealing representations thanks to running in a web browser, which often lets you interact with those representations. The specific representation will depend on the type of object at hand. In this case, the variable references a Pandas data frame object, which the notebook can display as tabular data.

Become a Member to join the conversation.