Data Engineering Lab

Arithmetic Operators — Calculations, Named Stages, and Data Checks

Overview

Arithmetic operators combine numeric values in expressions. In Python 3 the set you use most often is +, -, *, / (always float), // (floor toward −∞), % (remainder), and ** (power). abs() and round() are built-in functions, not operators, but they show up constantly next to them in data work.

The Practice Lab Arithmetic Operators (segment Operators & Expressions) trains five deliberately sequenced patterns: add/subtract, multiply/divide, floor division/modulo, exponentiation with a named intermediate, and an applied accuracy report. In Q5, -, /, and * perform the arithmetic; abs() and round() are supporting built-in functions with clearly separated jobs.

Map to Practice Lab questions

Lab QSkill
Q1price + tax - discount → 23
Q2hours * rate, then earnings / people → 120, then 40.0
Q3items // batch_size and items % batch_size → 8, then 10
Q4growth_factor = 1.05 ** 2, then 1000 * growth_factor → 256, 27, then 1102.5
Q5Named -, /, and * stages; abs() for magnitude and final round(..., 1) → 5, then 5.0

Practice here: Practice Lab → Arithmetic Operators.

Enable JavaScript to explore the interactive diagram.

Operator quick reference

OpMeaningLab tie-in
+ -Add and subtractPrice adjustment (Q1), signed difference (Q5)
* /Multiply and true-divideEarnings/share (Q2), percentage stages (Q5)
//Floor divisionComplete batches (Q3)
%RemainderLeftover items (Q3)
**PowerPowers and a named growth factor (Q4)

Key takeaways

  • / never truncates to int in Python 3; use // when you need a floored integer quotient.
  • In Q4, a named growth_factor keeps exponentiation and principal scaling as two visible decisions. The next topic teaches implicit operator precedence in depth.
  • In Q5, name the arithmetic stages before using helpers: subtraction → magnitude → division → multiplication → final formatting.
  • abs() interprets a signed difference as a magnitude; round(x, 1) formats the completed percentage with one decimal place.

Examples

Example 1 — Q1: add tax, subtract discount

price = 25
tax = 3
discount = 5
total = price + tax - discount
print(total)

Output:

23

Example 2 — Q2: multiply earnings, divide the share

hours = 8
rate = 15
people = 3
earnings = hours * rate
share = earnings / people
print(earnings)
print(share)

Output:

120
40.0

Example 3 — Q3: full batches and leftovers

items = 250
batch_size = 30
full_batches = items // batch_size
leftover = items % batch_size
print(full_batches)
print(leftover)

Output:

8
10

Example 4 — Q4: powers and a named growth factor

print(2 ** 8)
print(3 ** 3)
growth_factor = 1.05 ** 2
balance = 1000 * growth_factor
print(balance)

Output:

256
27
1102.5

Example 5 — Q5: calculate the metric, then format it

actual = 95
target = 100
signed_diff = actual - target
diff = abs(signed_diff)
ratio = diff / target
percentage = ratio * 100
pct_error = round(percentage, 1)
print(diff)
print(pct_error)

Output:

5
5.0

Example 6 — All seven operators (cheat sheet)

a, b = 15, 4
print(a + b, a - b, a * b, a / b, a // b, a % b, a ** 2)

Mistakes

Mistake 1 — Reversing the price adjustments (Q1)

Tax increases the base price and the discount decreases it. Use price + tax - discount, not price - tax + discount.


Mistake 2 — Expecting / to return an integer (Q2)

In Python 3, 120 / 3 returns 40.0. Use / for the share and preserve the expected float result.


Mistake 3 — Confusing // and % (Q3)

// returns the eight complete batches; % returns the ten items left over. Check the reconstruction: 30 * 8 + 10 == 250.


Mistake 4 — Squaring the principal with the rate (Q4)

Only the growth factor repeats. Calculate growth_factor = 1.05 ** 2, then balance = 1000 * growth_factor. This keeps Q4 focused on exponentiation rather than implicit precedence.


Mistake 5 — Treating abs() and round() as operators (Q5)

The operators -, /, and * calculate the metric. abs() converts the signed difference to a magnitude, and round() formats the final percentage.


Mistake 6 — Rounding an intermediate Q5 value

Keep full precision through ratio and percentage. Apply round(percentage, 1) only after the arithmetic is complete.


Interview

Q1. How do you explain price + tax - discount?

Tax increases the base price, then the discount decreases the adjusted amount: 25 + 3 - 5 == 23.


Q2. Why does the equal share print 40.0 rather than 40?

Python 3 true division with / returns a float, even when the mathematical quotient is whole.


Q3. How do Q3's quotient and remainder account for every item?

Reconstruct the input: batch_size * full_batches + leftover is 30 * 8 + 10 == 250, and the remainder is smaller than one batch.


Q4. Why calculate growth_factor before multiplying the principal?

The name isolates the job of **: 1.05 ** 2 creates the two-year factor 1.1025. The next line applies that result to the principal, so the learner does not need an implicit-precedence rule.


Q5. Which Q5 steps are operators and which are helper functions?

-, /, and * calculate the signed difference, ratio, and percentage. abs() converts direction to magnitude; round() formats the completed percentage.


Python vs PySpark

Python runs scalar formulas on the driver. PySpark expresses the same algebra on Column objects so executors evaluate it per row in parallel; null propagates unless you coalesce or use when.

The comparison table is generated for this topic. Use Try Now for Python and PySpark practice.


Python vs SQL

Python uses //, %, and ** with fixed semantics. SQL dialects differ on integer vs float division and sometimes spell remainder as MOD(a,b) instead of %.

The comparison table is generated for this topic. Use Try Now for Python and SQL practice.