Module 2 · Python Syntax and Fundamentals — topic 4 of 14
Expressions
An expression is any piece of code that produces a value, from a single number to a calculation or function call. Learn how expressions differ from statements and how Python evaluates them.
Learning objectives
- Identify expressions in code
- Explain how an expression is evaluated to a value
- Combine expressions inside statements
Definition
An expression is any piece of code that produces a value, from a single number to a calculation or function call. Learn how expressions differ from statements and how Python evaluates them.
Introduction
An expression is code that Python can work out to a single value. 5 is an expression, so is 5 + 3, and so is len("hello"). Statements use expressions: in total = price * 2, the right-hand side is an expression.
Why it matters
Almost everything interesting in a program is calculated by expressions. Knowing that any expression can be used wherever a value is expected lets you write compact, clear code, such as printing a calculation directly or passing one function’s result to another.
Detailed explanation
Literals such as 42, 3.5 and "text" are the simplest expressions; they evaluate to themselves.
A variable name is an expression that evaluates to the value it currently refers to.
Operators combine expressions into bigger ones: price * quantity, age >= 18, "a" + "b".
Function calls are expressions too: len(name) evaluates to a number.
Python evaluates sub-expressions first, following operator precedence, then combines the results. The final value can be stored, printed, compared or passed on.
Statements are actions; expressions are values. x = 5 is a statement, while 5 and x + 1 are expressions.
Syntax
- Operands are smaller expressions such as numbers or names.
- Operators such as + and * combine them.
- An expression can sit anywhere a value is expected, including inside print().
Code examples
Expressions evaluate to values
8 10 12
Comparisons are expressions too
False
More examples
Building a result from nested expressions
Area: 60 sq m Cost: 27000 Over budget: True
Step-by-step explanation
- length * width * rate_per_sq_m is evaluated left to right: 12 * 5 = 60, then 60 * 450 = 27000.
- The assignment statement stores that value in cost.
- length * width is evaluated again directly inside print.
- cost > 25000 evaluates to the Boolean True, which print shows.
Expected output
Running “Building a result from nested expressions” prints exactly:
Practical applications
- Spreadsheet formulas are expressions; Python scripts that replace spreadsheets use the same idea.
- Validation rules such as age >= 18 and len(password) >= 8 are expressions that produce True or False.
- Report code builds totals and percentages with expressions before formatting them.
Best practices
- Store the result of a complex expression in a well-named variable.
- Use parentheses to make the intended order obvious.
- Avoid repeating the same expression; calculate once and reuse.
Common mistakes
Using = where an expression is needed
Avoid
Prefer
= is an assignment statement, not an expression. Inside print() Python treats total= as a keyword argument and raises a TypeError.
Writing a bare expression and expecting output
Avoid
Prefer
In a script an expression on its own is calculated and thrown away. Store it or print it.
Key takeaways
- An expression produces a value.
- Literals, names, operators and function calls are all expressions.
- Statements perform actions and often contain expressions.
Try it yourself
- 1Write an expression that converts 37 degrees Celsius to Fahrenheit (C * 9 / 5 + 32) and print it.
- 2Print whether the word "programming" has more than 10 letters, using a single expression.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhich of these is an expression that produces a value?