Expressions and Operators
Combine values with arithmetic, comparisons, and logical operators.
python, arithmetic, comparison, boolean, indentation, commentsWork out a result
When you calculate the cost of three pencils at two coins each, you combine
numbers with a rule. In code, 3 * 2 is an expression: Python evaluates
it to a value. An operator such as * describes the operation.
| Operator | Example | Result |
|---|---|---|
+, -, * | 3 * 2 | 6 |
/ | 7 / 2 | 3.5 |
// | 7 // 2 | 3 |
% | 7 % 2 | 1, the remainder |
** | 2 ** 3 | 8 |
Floor division rounds toward negative infinity, so -7 // 2 is -4, not
-3. Dividing by zero raises an error. In (2 + 3) * 4, the addition
happens first, giving 5 * 4 = 20. Without the parentheses, 2 + 3 * 4
multiplies first and gives 2 + 12 = 14.
Ask a yes-or-no question
==, !=, <, <=, > and >= compare values and produce True or
False. and, or, and not combine or reverse conditions. and and
or can stop early once their result is determined; do not rely on the
second expression being evaluated every time.
Python illustration
price = 7
people = 2
print(price // people)
print(price % people)
print(price >= 7 and people > 0)
print((2 + 3) * 4)
Prepared output — not live execution:
3
1
True
20
See the reasoning
Follow the example one step at a time
See why parentheses change the order of a calculation. This is a prepared teaching demonstration, not live code execution.
Start
(2 + 3) * 4
The parentheses group the addition.
Comments and spaces
A comment beginning with # helps a human reader; it is not an instruction
for Python to run. Use comments to explain a non-obvious choice, not to
replace clear names. Python uses indentation at the beginning of a line to
group instructions inside a block. Use consistent spaces; the next lesson
shows why the placement matters.
Reflect on the concept
Four fixed questions to reinforce the reasoning. Answers are not saved or scored.
