Page 58 - Python Basics: A Practical Introduction to Python 3
P. 58
3.4. Inspect Values in the Interactive Window
Here, you assign the number 2 to x. Both using print(x) and inspecting
x display output without quotation marks because 2 is a number and
not text. In most cases, though, variable inspection gives you more
useful information than print().
Suppose you have two variables: x, which is assigned the number 2,
and y, which is assigned the string literal "2". In this case, print(x)
and print(y) both display the same thing:
>>> x = 2
>>> y = "2"
>>> print(x)
2
>>> print(y)
2
However, inspecting x and y shows the difference between each vari-
able’s value:
>>> x
2
>>> y
'2'
The key takeaway here is that print() displays a readable representa-
tion of a variable’s value, while variable inspection displays the value
as it appears in the code.
Keep in mind that variable inspection works only in the interactive
window. For example, try running the following program from the
editor window:
greeting = "Hello, World"
greeting
The program executes without any errors, but it doesn’t display any
output!
57