Introduction to Expressions
Most statements (logical lines) that you write will contain expressions . A simple example of an expression is 2 + 3 . An expression can be broken down into operators and operands.
What Is an Expression?
An expression is a piece of code that combines values, variables, and operators to produce a result. Most statements (logical lines) that you write will contain at least one expression. The simplest expression might be a single value like 5 or a variable name like score. More interesting expressions combine multiple pieces using operators.
Expression: A combination of values, variables, and operators that evaluates to produce a single result.
In everyday life, when you calculate the total cost of three items priced at 2 dollars, 3 dollars, and 5 dollars, you are performing an expression: 2 + 3 + 5. The expression combines the values (2, 3, 5) with an operator (addition) to produce a result (10). In programming, expressions work the same way—they combine data with operations to compute new values.
Anatomy of an Expression
Every expression can be broken down into two fundamental parts: operands and operators. Understanding this structure helps you read and write expressions correctly.
Operand: A value or variable that an operator acts upon. In the expression 2 + 3, the operands are 2 and 3.
Operator: A symbol that specifies an operation to perform on one or more operands. In the expression 2 + 3, the operator is +, which represents addition.
In the expression 2 + 3, the number 2 is the left operand, the plus sign (+) is the operator, and the number 3 is the right operand. When this expression is evaluated, the operator tells the program to add the two operands together, producing the result 5.
How Expressions Are Evaluated
When you write an expression, the program does not simply store it as text. Instead, the program reads the expression, performs the operation specified by the operator, and produces a result. This process is called evaluation.
An expression always evaluates to a single value. That value can then be used in other operations, assigned to a variable, or displayed to the user.
You can test expressions interactively in a Python interpreter. For example, to evaluate the expression 2 + 3, you type it at the prompt and the interpreter immediately shows you the result.
What do you think happens?
What do you think happens when you type 2 + 3 into a Python interpreter and press Enter?
Reveal answer
Answer: The interpreter evaluates the expression and displays the result 5
The Python interpreter is designed to evaluate expressions. When you type 2 + 3, it recognizes the + operator, applies addition to the operands 2 and 3, and returns the result 5 to you immediately.
Expressions Inside Statements
An expression by itself is useful for calculation, but most of the time you will use expressions as part of larger statements. A statement is a complete logical line of code that performs an action. Expressions are the building blocks that statements use to compute values, make decisions, or control program flow.
For example, in an assignment statement like total = 2 + 3, the expression 2 + 3 is evaluated first, and then its result (5) is assigned to the variable total. The expression does the computation, and the statement decides what to do with that result.
Expressions compute values; statements use those values to perform actions like storing data, making decisions, or repeating code.
Compound Assignment Operators
A common pattern in programming is to run a math operation on a variable and then assign the result back to the same variable. For example, you might write score = score + 10 to increase a score by 10. Python provides a shortcut for this pattern called compound assignment operators.
Compound assignment operator: A shorthand notation that combines an operator and assignment. The pattern var = var operation expression becomes var operation= expression.
Instead of writing score = score + 10, you can write score += 10. Both statements do exactly the same thing: they add 10 to the current value of score and store the result back in score. The compound operator += is shorthand for the longer form.
Compound assignment operators like +=, -=, *=, and /= make code more concise and are commonly used when updating a variable with the result of an operation.
Common Mistakes with Expressions
Confusing an expression with a statement
An expression computes a value but does not store it anywhere. To actually use the result, you must assign it to a variable or use it in a statement.
Fix:
Write x = x + 5 or x += 5 to both compute the result and store it back in x.Forgetting that expressions must have valid operands
An operator needs operands on both sides (for binary operators). The expression is incomplete and the program cannot evaluate it.
Fix:
Always provide both operands: 2 + 3.Misunderstanding operator precedence
Multiplication has higher precedence than addition, so 3 * 4 is evaluated first (12), then 2 + 12 = 14.
Fix:
Remember the order of operations (PEMDAS/BODMAS) or use parentheses to make precedence explicit: (2 + 3) * 4 for 20.Using the wrong operator
The subtraction operator (-) decreases the value instead of increasing it.
Fix:
Use the correct operator for your intent: score = score + 10 or score += 10.
Practical Notes on Expressions
Practice
Identify the operands and operator in each of the following expressions, then predict what result each expression will produce: (1) 10 - 3, (2) 4 * 5, (3) 20 / 4.
Hints
- Remember that an operator is a symbol like +, -, *, or /.
- Operands are the values or variables on either side of the operator.
- Think about what each operator does: subtraction, multiplication, and division.
Write a compound assignment statement that increases the variable count by 1. Then write the equivalent long-form statement without using a compound operator.
Hints
- Compound assignment operators combine an operator and assignment into one symbol.
- The pattern is: variable operator= value.
- Think about what += does: it adds to the variable and stores the result back.
Explain why the statement x = x + 5 is not the same as just writing the expression x + 5 on a line by itself.
Hints
- An expression computes a value but does not store it.
- A statement performs an action, such as storing a value in a variable.
- What happens to the result of x + 5 if you do not assign it to anything?
Key Takeaways
- An expression is a combination of values, variables, and operators that evaluates to produce a single result.
- Every expression consists of operands (the values being operated on) and an operator (the symbol specifying the operation).
- Expressions are evaluated by the program, which means the operation is performed and a result is computed.
- Expressions are building blocks used inside statements; a statement decides what to do with the result an expression produces.
- Compound assignment operators like += provide a shorthand way to update a variable with the result of an operation.