Expressions and Statements
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 Are Expressions and Statements?
When you write code, you are constantly combining values and operations together. Most of the logical lines you write will contain expressions. An expression is a piece of code that can be evaluated to produce a value. The simplest expression might be just a single number like 5, or it might be something more complex like 2 + 3. A statement, on the other hand, is a complete instruction that tells the computer to do something. Statements often contain expressions, but they are the full command that gets executed.
An expression is a combination of values, variables, and operators that can be evaluated to produce a single value. A statement is a complete line of code that performs an action; most statements contain one or more expressions.
Breaking Down an Expression
Every expression is built from two key parts: operands and operators. An operand is a value or variable that the operation acts upon. An operator is a symbol that tells the computer what operation to perform. In the expression 2 + 3, the numbers 2 and 3 are operands, and the plus sign is the operator. Understanding this structure helps you read and write expressions correctly.
Operands can be literal values (like 2 or 3), variables (like x or y), or even the result of another expression. Operators include arithmetic operators like +, -, *, and /, as well as comparison operators like ==, <, and >, and logical operators like and, or, and not. When an expression is evaluated, the operator is applied to the operands, and the result is a single value.
How Expressions Fit Inside Statements
A statement is the complete instruction you give to the computer. Most statements contain expressions. For example, if you write length * breadth, that is an expression. But if you write area = length * breadth, that is a statement—specifically, an assignment statement. The expression length * breadth is evaluated first, and then the result is stored in the variable area. Similarly, if you write print(2 * (length + breadth)), the entire line is a statement, and inside it are multiple expressions: the addition length + breadth, the multiplication 2 * (...), and the function call itself.
The key difference: an expression produces a value, but a statement performs an action. You cannot use a statement where an expression is expected, but you can use an expression as part of a statement.
Worked Example: Calculating Rectangle Properties
Using Expressions in a Real Program
You have a rectangle with length 5 and breadth 3. You need to calculate and display both the area and the perimeter. The area is length times breadth. The perimeter is 2 times the sum of length and breadth.
Identify the expressions: The area calculation is the expression length * breadth. The perimeter calculation is the expression 2 * (length + breadth). Notice that the perimeter expression contains a nested expression: length + breadth is evaluated first, then multiplied by 2.
Recognize the statements: The statement area = length * breadth stores the result of the expression in a variable. The statement print(area) is a statement that contains the expression area. The statement print(2 * (length + breadth)) is a statement that contains a more complex expression.
Understand the evaluation order: When the computer encounters the expression 2 * (length + breadth), it first evaluates the expression inside the parentheses (length + breadth), then multiplies that result by 2. This order matters because it determines what value the expression produces.
The area expression evaluates to 15 (5 * 3). The perimeter expression evaluates to 16 (2 * (5 + 3) = 2 * 8 = 16). These values are then used in the statements that print or store them.
Common Mistakes with Expressions
Confusing an expression with a statement
An expression produces a value, but that value is not automatically displayed or stored. The expression is evaluated, but the result is discarded unless it is part of a statement like print(2 + 3) or x = 2 + 3.
Fix:
Always use the expression as part of a complete statement. If you want to see the result, put it in a print statement. If you want to save it, assign it to a variable.Forgetting that operators have different precedence
Multiplication has higher precedence than addition, so 3 * 4 is evaluated first (giving 12), then 2 + 12 is evaluated (giving 14). Without understanding precedence, you may predict the wrong value.
Fix:
Use parentheses to make the order explicit: (2 + 3) * 4 if you want 20, or 2 + (3 * 4) if you want 14. Parentheses always override the default precedence.Using an operator incorrectly or mixing up similar operators
The = operator assigns a value to a variable; it is not an expression that produces a true/false result. The == operator compares two values and produces true or false. Using the wrong one changes what the code does entirely.
Fix:
Remember: = is for assignment (used in statements), and == is for comparison (used in expressions). Use == when you want to check if two values are equal.
Types of Operators in Expressions
Expressions can contain different kinds of operators, each serving a different purpose. Arithmetic operators like +, -, *, and / perform mathematical calculations. Comparison operators like ==, !=, <, >, <=, and >= compare two values and produce a true or false result. Logical operators like and, or, and not combine or negate boolean values. Understanding which type of operator you are using helps you predict what value an expression will produce.
| Operator Type | Examples | What It Does | Result Type |
|---|---|---|---|
| Arithmetic | +, -, *, /, // | Performs mathematical calculations | A number (int or float) |
| Comparison | ==, !=, <, >, <=, >= | Compares two values | True or False |
| Logical | and, or, not | Combines or negates boolean values | True or False |
Evaluating Expressions Step by Step
When the computer evaluates an expression, it follows a specific order. It respects operator precedence (for example, multiplication before addition), and it processes operations from left to right when operators have the same precedence. Let's trace through a more complex expression to see how this works.
What do you think happens?
What value does the expression 10 - 2 * 3 + 1 produce?
Reveal answer
Answer: 5
The expression is evaluated in this order: first, 2 * 3 is evaluated (giving 6) because multiplication has higher precedence than addition and subtraction. Then, 10 - 6 is evaluated (giving 4) because subtraction and addition are evaluated left to right. Finally, 4 + 1 is evaluated (giving 5). So the result is 5.
This step-by-step evaluation is crucial to understand. If you misunderstand the order, you will predict the wrong value. Using parentheses makes the order explicit: (10 - 2) * 3 + 1 would give 25, and 10 - (2 * 3 + 1) would give 3. The parentheses force the computer to evaluate the expression inside them first.
Statements That Contain Expressions
In real programs, you will rarely write an expression by itself. Instead, you will use expressions inside statements. An assignment statement like x = 5 + 3 contains the expression 5 + 3. A print statement like print(length * breadth) contains the expression length * breadth. A function call like max(a, b) contains expressions as arguments. A conditional statement like if x > 10: contains the expression x > 10. In each case, the expression is evaluated first, and then the statement uses that value to perform an action.
Every statement that does something useful will contain at least one expression. Learning to recognize expressions inside statements is a key skill for reading and writing code.
Practice: Identifying Expressions and Statements
For each of the following, identify whether it is an expression or a statement, and explain what value it produces or what action it performs. If it contains an expression, identify the operators and operands.
Hints
- Remember: an expression produces a value; a statement performs an action.
- Look for operators and operands to identify expressions.
- Most statements will contain one or more expressions.
- 7 + 2
- result = 7 + 2
- print(7 + 2)
- x > 5 and y < 10
- if x > 5 and y < 10: print('yes')
Why This Matters
Understanding the difference between expressions and statements is fundamental to programming. Expressions are the building blocks that produce values. Statements are the instructions that use those values to do something. When you write code, you are constantly combining expressions into statements. If you misunderstand how expressions work—how they are evaluated, what operators do, what values they produce—your code will not behave as you expect. By mastering expressions and statements now, you build a solid foundation for all the more complex programming concepts you will learn later.
Key Takeaways
- An expression is a combination of values, variables, and operators that evaluates to produce a single value. A statement is a complete instruction that performs an action; most statements contain expressions.
- Every expression is built from operands (values or variables) and operators (symbols that specify what operation to perform). In 2 + 3, the operands are 2 and 3, and the operator is +.
- Expressions are evaluated following operator precedence and left-to-right order. Parentheses can override the default precedence to make the evaluation order explicit.
- Statements use expressions to perform actions. Assignment statements store expression results in variables. Print statements display expression results. Conditional statements use expressions to decide what to do next.
- Recognizing expressions inside statements is essential for reading and writing code correctly. Confusing expressions with statements, forgetting operator precedence, or using the wrong operator are common mistakes that lead to incorrect results.