Operands and Expressions
Once you can store data in variables, the next step is learning how to work with that data. This is where operands and expressions come in. An operand is a value used in a computation, and an expression is a combination of operands and operators that produces a result.
Common Types of Operators
- Arithmetic operators:
+,-,*,/,%,** - Comparison operators:
==,!=,>,<,>=,<= - Logical operators:
and,or,not - Assignment operators:
=,+=,-=,*=
price = 250
quantity = 4
total = price * quantity # expression using arithmetic operator
is_bulk_order = quantity >= 3 # expression using comparison operator
Where This Leads
With operators and expressions in hand, you're ready to make your programs respond differently based on conditions, which is exactly what conditional statements are for.
Operator Precedence: Which Part Runs First
When an expression has several operators, Python follows a fixed priority order, similar to the BODMAS rule from school maths. From highest to lowest, the most common ones are:
- Parentheses
( ) - Exponent
** - Multiplication, division, floor division and remainder:
* / // % - Addition and subtraction:
+ - - Comparisons:
== != > < >= <= not, thenand, thenor
result = 10 + 4 * 3 ** 2 # 3**2 = 9, 4*9 = 36, 10+36 = 46
safer = (10 + 4) * 3 ** 2 # 14 * 9 = 126
print(result, safer)
Adding parentheses even where they are not strictly needed makes intent clear to anyone reading the code later.
A Worked Example: Discount Logic
A shop offers 10 percent off when a customer buys at least 3 items or spends more than 1,000 rupees, but never for clearance stock.
price = 250
quantity = 4
is_clearance = False
subtotal = price * quantity # 1000
eligible = (quantity >= 3 or subtotal > 1000) and not is_clearance
discount = subtotal * 0.10 if eligible else 0
final_amount = subtotal - discount
print(subtotal, eligible, discount, final_amount) # 1000 True 100.0 900.0
Each line combines operands with one kind of operator, which keeps the logic easy to check step by step. Try changing quantity to 2 and price to 600: the subtotal becomes 1200, the second condition is now true, and the customer still gets the discount.
Division and Remainder Behaviour
| Expression | Result | Meaning |
|---|---|---|
7 / 2 | 3.5 | True division, always returns a float |
7 // 2 | 3 | Floor division, rounds down |
-7 // 2 | -4 | Rounds towards minus infinity, not towards zero |
7 % 2 | 1 | Remainder, handy for odd or even checks |
2 ** 10 | 1024 | Power |
Handy Expression Features
- Chained comparisons:
18 <= age < 60works like18 <= age and age < 60. - Short-circuiting: in
count > 0 and total / count > 50, the division is skipped whencountis zero, so no error occurs. - Membership:
city in ["Delhi", "Noida"]returns True or False. - Identity:
value is Noneis the correct way to test for a missing value. - Augmented assignment:
total += priceis a shorter form oftotal = total + priceand reads naturally inside loops.
Mistakes Beginners Make
- Writing
=(assignment) when==(comparison) is meant. - Comparing floats exactly:
0.1 + 0.2 == 0.3is False. Usemath.isclose(0.1 + 0.2, 0.3)instead. - Mixing text and numbers:
"5" + 3raises aTypeError; convert withint("5")first. - Using
andandoron pandas columns; for Series, use&and|with each condition in parentheses.