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
Expressions are the core of decision-making in code. Almost every condition, calculation, and comparison in a data science script is built from simple expressions like these.

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:

  1. Parentheses ( )
  2. Exponent **
  3. Multiplication, division, floor division and remainder: * / // %
  4. Addition and subtraction: + -
  5. Comparisons: == != > < >= <=
  6. not, then and, then or
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

ExpressionResultMeaning
7 / 23.5True division, always returns a float
7 // 23Floor division, rounds down
-7 // 2-4Rounds towards minus infinity, not towards zero
7 % 21Remainder, handy for odd or even checks
2 ** 101024Power

Handy Expression Features

  • Chained comparisons: 18 <= age < 60 works like 18 <= age and age < 60.
  • Short-circuiting: in count > 0 and total / count > 50, the division is skipped when count is zero, so no error occurs.
  • Membership: city in ["Delhi", "Noida"] returns True or False.
  • Identity: value is None is the correct way to test for a missing value.
  • Augmented assignment: total += price is a shorter form of total = total + price and reads naturally inside loops.

Mistakes Beginners Make

  • Writing = (assignment) when == (comparison) is meant.
  • Comparing floats exactly: 0.1 + 0.2 == 0.3 is False. Use math.isclose(0.1 + 0.2, 0.3) instead.
  • Mixing text and numbers: "5" + 3 raises a TypeError; convert with int("5") first.
  • Using and and or on pandas columns; for Series, use & and | with each condition in parentheses.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course