Expressions vs Statements
Python code can be divided into two categories: expressions and statements. In short: expressions product a value, statements perform an action.
Expressions
Expressions are code that produces a value.
5 + 3 # Expression → produces 8
"hello" + "world" # Expression → produces "helloworld"
You'll learn more about expressions when you study operators. For now, just know that expressions produce values.
Statements
Statements are code that does something but doesn't produce a value you can use directly:
print("Hello") # Statement → prints something (doesn't give you a value back)
Statements:
- Perform actions (like printing, assigning values to variables)
- Don't produce values you can use
- Are the "commands" that make your program do things
You'll see more examples of statements when you learn about variables and conditionals. For now, just know that statements perform actions.
Why this matters
Understanding expressions vs statements helps you understand:
-
Why some code produces values and some don't:
5 + 3is an expression—it produces the value8print("Hello")is a statement—it does something (prints) but doesn't give you a value
-
The difference between doing something and getting a value:
- Expressions give you something you can use
- Statements just do work
This distinction will become clearer as you learn more about operators and other Python features. For now, just remember: expressions produce values, statements perform actions.
Summary
Understanding expressions vs statements helps you understand how Python works:
- Expressions produce values—they're like "questions" that Python answers with a value (e.g.,
5 + 3produces8) - Statements perform actions—they're "commands" that do work but don't give you a value back (e.g.,
print("Hello")prints but doesn't return a value)