Skip to content

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.

Beginner 12 min read·Lesson 18 of 306 in Python Programming

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

Text
result = operand operator operand
print(function(argument) + 1)
  • 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

Python
print(8)
print(8 + 2)
print(len("python") * 2)
Output
8
10
12

Comparisons are expressions too

Python
age = 17
print(age >= 18)
Output
False

More examples

Building a result from nested expressions

Python
length, width = 12, 5
rate_per_sq_m = 450
cost = length * width * rate_per_sq_m
print("Area:", length * width, "sq m")
print("Cost:", cost)
print("Over budget:", cost > 25000)
Output
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:

TextOutput
Area: 60 sq m
Cost: 27000
Over budget: True

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

print(total = 5)

Prefer

total = 5
print(total)

= 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

price * 2

Prefer

print(price * 2)

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

  1. 1
    Write an expression that converts 37 degrees Celsius to Fahrenheit (C * 9 / 5 + 32) and print it.
  2. 2
    Print 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?
Practise this topic