In Python, understanding the difference between expressions and statements is like understanding the difference between a phrase and a complete sentence in English.
Expressions (The "Phrases")
An expression is a piece of code that produces a value. Whenever Python "looks" at an expression, it calculates it down to a single result.
- Think of it as: A question that Python answers.
-
Examples:
- 5 + 5 (evaluates to 10)
- print(round(30/4)) (evaluates to 8)
- user_score > 10 (evaluates to True or False)
Statements (The "Sentences")
A statement is a complete action. It tells Python to do something. While an expression produces a value, a statement executes a command.
- Think of it as: An instruction or a move the program makes.
-
Examples:
- Assignment Statement: x = 10 (Tells Python to store 10 in x).
- Print Statement: print("Hi") (Tells Python to show text on the screen).
- If Statement: if x > 5: (Tells Python to make a decision).
The Key Difference: The "Print Test"
A simple trick to tell them apart is the Print Test: If you can put the code inside print() and it outputs a value without crashing, it is likely an expression.
print(5 + 5) ✅ Works. (Expression)
print(x = 10) ❌ Fails. (Statement—you can't "print" an assignment).
Important Note: Nested Logic
Expressions can be part of a statement. In the code score = 10 + 5:
- 10 + 5 is the expression (it evaluates to 15).
- The entire line score = 15 is the statement (the act of assignment).
Test Your Knowledge
Select the correct answer below to see if you've mastered Expressions and Statements.
Loading quiz...
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named expressions_vs_statements.py in your part8 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: The Print Test
- In your editor, try to run print(10 + 5).
- On the next line, try to run print(y = 20).
The Task: One of these will crash.
In a comment, identify which one failed and explain why based on the "Print Test" rule.
Exercise 2: Building Statements
Write a script that calculates the area of a rectangle (length multiplied by width).
The Task: Create three statements.
- Statement 1 sets the length
- Statement 2 sets the width
- Statement 3 calculates the area
Label the expression part of your third statement with a comment.
Exercise 3: Expression Hunting
Copy these lines:
result = (10 + 2) * 3
print(result)
The Task: Below this code, add a comment listing every expression you see. (Hint: even result by itself is an expression when used inside print())
Exercise 4: Constants and Conventions
Create a variable for Gravity (9.8) using the Constant naming convention.
The Task: Write a statement that calculates weight by multiplying a mass variable by your gravity constant. Ensure you use snake_case for mass and ALL_CAPS for gravity.
Exercise 5: The Dunder Mystery
Type: print(__name__)
The Task: Run the code. In a comment, explain if __name__ is acting as an expression or a statement here. Also, note the "Dunder" naming convention used.
Don't Forget to commit and Push!