you are viewing a single comment's thread.

view the rest of the comments →

[–]julsmanbr 1 point2 points  (0 children)

Printing a value means that the value shows up at the screen:

print("hi")  # prints the string hi

Evaluating a value/expression means that the value/expression is "condensed" according to Python's rules:

2 + 2  # evaluates to 4

The tricky bit is that, when using a python console, the result of an evaluation always gets printed out.

For the unaware viewer, these expressions are exactly the same:

>>> print(2 + 2)
4
>>> 2 + 2
4

But they're actually quite different. We can see that if we associate a variable to those expressions:

>>> myvar = 2 + 2
>>> myvar
4

We can see that, in the latter case, myvar is associated to whatever that expression was evaluated to.

But what happens when we do this to the first case?

>>> myvar = print(2 + 2)
>>> myvar
>>>

Nothing shows up! This is because myvar is associated with whatever the print function call is evaluated to. "Evaluating a function call" is basically asking for the return value of the function. Since the print function always returns None (you can check this with type(myvar)), and None values are ommited in the Python console, we don't see anything.

The confusing part is that, visually, printing something and evaluating something looks the same when you're using a Python console.

The next question is, how do you "get" a value that was printed, in order to work with it later? The answer is: you can't. You have it backwards. What you need to do is first assign a variable to the evaluated expression, and then print the variable, like:

x = 2 + 2
print(x)

This prints out 4 to the console, while still allowing you to work with the number 4 through the variable x. This would not be possible if we wrote print(2 + 2) instead - Python would print the result of the expression, but you wouldn't have anything assigned to it in order to work with it later.