Flat_Eric Contemplating Value Expressions vs Statements

Value Expressions vs Statements

Code That Returns a Value vs Code That Performs an Action

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.

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.

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:

  1. 10 + 5 is the expression (it evaluates to 15).
  2. 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:

Exercise 1: The Print Test

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.

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!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course