Reading preferences
Concept

Expressions and Operators

Combine values with arithmetic, comparisons, and logical operators.

python, arithmetic, comparison, boolean, indentation, comments

Work 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.

OperatorExampleResult
+, -, *3 * 26
/7 / 23.5
//7 // 23
%7 % 21, the remainder
**2 ** 38

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.

1. What is 7 // 2?
2. What does 4 >= 4 produce?
3. Which symbol tests equality?
4. What is the purpose of a comment?

References and further reading