# Literal values are expressions
print(5)
print(10.0)
print(True)
print("Hello")
# Math operations are expressions
print(1 + 2)
print(4 - 5)
print(8 / 4)
# Boolean operators are expressions
print(5 > 4)
print(False or not True)
print(1 < 2 and 4 > 5)
# Expressions can be inside of expressions
print(1 + 5 > 3 / 2 and 4 < 7 * 2)
print((1 + 2) * 3 < 10)
An expression is any kind of combination of literal values, mathematical operators, comparison operators, and Boolean operators. Any value alone is an expression. All operations are expressions. Expressions can even be made up of other expressions.
When Python encounters an expression, the result of the expression must be “evaluated.” We have previously described how we “run” a program. The word “evaluate” is similar but used for expressions. You “run” a program and “evaluate” an expression. The expression will always result in some value which Python can then use in some way.
199 + 1 + 300
When you look at the expression 199 + 1 + 300, one way to mentally solve the problem is to substitute the 199 + 1 term for 200.
Then, you can do the easier addition of 200 + 300.
Your brain imagines 200 even though the value is not literally in the expression.
When evaluating an expression, Python will do the same kind of substitution.
It does not show the intermediate results like the 200, but Python still did that calculation along the way.
Interestingly, by default, Python will not show the final result either.
Unless you use the print function, the result will not appear on the console when you run the code shown above.
The confusing part here, though, is that Python will still compute the value.
The result is just “thrown away!”
# This is not printed 1 + 2 # This is printed print(1 + 9) # This causes an error! 1 / 0 # This cannot be reached print(4 * 2)
Python will evaluate every expression, from the top of the program down to the bottom.
Even if an expression is not printed, the result of the expression will be calculated.
The first 1 + 2 expression is evaluated and results in 3.
But since that 3 is not printed or used in any way, the result is ignored and left behind.
However, the 1 + 9 expression evaluates to 10, which is then printed on the console.
Things get interesting when we reach the third statement 1 / 0.
That expression is invalid but still must be evaluated.
When Python tries to do division by zero, the program will stop with an error message.
This prevents Python from reaching the second print statement or even evaluating 4 * 2.
Run the playground above yourself to see the traceback appear, right after the 10.
We have previously described how the print and input commands will write text to the console area.
We said that the console was like an interactive chat window, that you can use to communicate with the computer.
This is actually even more true than we suggested before, thanks to some consoles’ ability to evaluate expressions directly.
Most programming environments have some kind of interactive console, also known as a shell or terminal, that lets you type an expression and see its value immediately, without writing a full program and without using print.
This is often called a REPL, which stands for read-evaluate-print-loop.
The computer repeatedly reads input from the programmer, evaluates the expression, prints the result, and then loops to do it again.
The code playgrounds on this page don’t work quite like a REPL: they run your code like a saved program, from top to bottom, rather than evaluating one line at a time and showing you each result as you go.
But you can still use them to explore the same printed-vs-not-printed distinction a REPL hides from you: try typing just an expression (no print) into a playground and run it, then wrap that same expression in print(...) and run it again. Compare what you see in each case.
1 + 2 print(3 + 4)
Run the playground above as shown first.
Only one line of output appears (7), even though both 1 + 2 and 3 + 4 are evaluated.
Now edit the code: delete print(...) from around 3 + 4, leaving just 3 + 4 on its own line, and run it again.
Nothing appears at all this time, even though the addition still happens!
Finally, add print(...) back around both expressions and run once more, so you can see both results together.
An interactive console automatically shows you the value of whatever you evaluate, which is why you don’t need print there.
But your regular Python programs, and the playgrounds on this page, run more like a saved program: only what you explicitly print appears as output.
print.