Print and Expressions: Display Python Results
Evaluate an expression and print its result
An expression is code that produces a value. For example, 6 + 4 produces the number 10. When you put an expression inside print(), Python evaluates it first, then displays the result. Use + for addition and * for multiplication. A whole number such as 6 is an integer; text in quotes is a string.
The first expression adds six and four. The second multiplies three by five. The results appear on separate lines: 10 and 15.
Group a calculation
Each bundle costs four plus two units. Parentheses group the addition, so Python calculates six first and multiplies by three, giving 18. Without the inner parentheses, 3 * 4 + 2 gives 14 because multiplication has higher precedence than addition.
Text is not a calculation
The quotes make the first argument literal text, so the first line displays 6 + 4. The second line displays 10. In a script, an arithmetic expression by itself does not normally display a result; use print() when you want visible output. The REPL can display the value of an expression without print().
Predict, then check
Imagine two packs, each containing five items plus one bonus item. Write an expression using parentheses and print the total. For the coding challenge, the supplied solution function is just a grading wrapper: its parameter is the provided number and return hands the computed value back to the checker. You will study functions later.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Expressions and printed results
Check your understanding of print and expressions.
What does this statement display?
print(4 * 5)What does this statement display?
print("4 + 5")What is the result?
print(2 * (3 + 4))Which expression computes the total for four packs containing 3 items and 2 bonus items each?
In a saved script, why use print(3 + 2) rather than just 3 + 2 when you want to see the result?