โž— What Are Operators?

An operator is a symbol or keyword that performs an action on one or more values. Python has three main families: arithmetic (do math), comparison (ask yes/no), and logical (combine true/false).

a + b = result

In banking, operators are everywhere. When SouthBank India checks if a deposit exceeds โ‚น50,000, they use comparison. When calculating 3 years of compound interest at 7%, they use arithmetic. When deciding if a loan is eligible and the account is active, they use logical.

๐Ÿ”ข Number Types: int vs float vs complex

int
42, -1000
Whole numbers, no decimal.
float
3.14, 7000.50
Decimal numbers. Money uses float.
complex
3+4j, 2j
Imaginary numbers. Rarely in banking.

๐Ÿฆ The Business Scenario

๐Ÿฆ
Banking ยท SouthBank India, Mumbai
Fixed Deposit Interest Calculation

A customer deposits โ‚น50,000 with SouthBank India at 7% annual interest for 3 years. The bank needs to calculate the final amount, monthly interest, remainders for fees, and validate eligibility rules. All operators come into play.

โ‚น50,000
Principal (int)
7%
Rate (float)
3 yrs
Time (int)

โž• Arithmetic Operators

fixed_deposit.py โ–ถ Run to see output below
# Output Principal : โ‚น50,000 Annual interest : โ‚น3,500.00 Total interest : โ‚น10,500.00 Final amount : โ‚น60,500.00 Monthly interest : โ‚น291.67

โšก Power, Integer Division & Modulo

The ** operator raises to a power โ€” critical for compound interest. The // operator divides and rounds down to an integer. The % operator returns the remainder.

compound_interest.py โ–ถ Real banks use this formula
# Output Simple interest : โ‚น10,500.00 Compound interest: โ‚น12,882.57 Final amount : โ‚น62,882.57 Monthly payment : โ‚น4166 (รท 12) Remainder : โ‚น8 (due in month 12)

๐Ÿ“Š Integer vs Float Division

The choice between / and // matters. Division with / always returns a float, even if both inputs are integers. Division with // always rounds down to an integer.

division_comparison.py โ–ถ Watch the types and values
# Output Float division : 83.33333333333333 (type: float) Integer division : 83 (type: int) Leftover rupees : 1 Verify: 83 ร— 3 + 1 = 250 Rounded to 2 decimals: โ‚น83.33

โš ๏ธ Floating-Point Quirks & Decimal

Computers store decimals in binary, which causes precision errors. 0.1 + 0.2 does not equal 0.3 โ€” it equals 0.30000000000000004. For financial calculations, use the Decimal module.

floating_point_demo.py โ–ถ Beware the precision trap
# Output Float: 0.1 + 0.2 = 0.30000000000000004 Equals 0.3? False Decimal: 0.1 + 0.2 = 0.3 Equals 0.3? True Banking scenario: Account A: 7000.10 Account B: 5000.20 Combined : 12000.30

โ“ Comparison & Logical Operators

All Operators in One Table

๐Ÿ”€
== / != / < / > / <= / >=
Comparison: return True or False. balance > 50000
๐Ÿ”—
and / or / not
Logical: combine conditions. is_active and balance > 0
๐Ÿ“
+ - * / // % **
Arithmetic: do math. interest = principal * rate
๐ŸŽฏ
round() / abs() / divmod()
Built-in functions for numbers. round(3.7) โ†’ 4

โš™๏ธ Operator Precedence (PEMDAS in Python)

Python evaluates operators in order: parentheses, exponents, multiply/divide, add/subtract. Comparison and logical come after all arithmetic.

Order (highest to lowest):

  1. () โ€” Parentheses
  2. ** โ€” Exponentiation
  3. * / // % โ€” Multiply, divide, floor, modulo
  4. + - โ€” Add, subtract
  5. == != < > <= >= โ€” Comparisons
  6. not โ€” Logical NOT
  7. and โ€” Logical AND
  8. or โ€” Logical OR

Example: 2 + 3 * 4 is 2 + 12 = 14, not 5 * 4 = 20. Multiply first.

๐ŸŒ Also Used In

Operators Are Universal

๐Ÿ“ฑ
if balance >= min_balance and is_active:
Telecom โ€” check subscriber credit and status before service
๐Ÿฅ
claim = premium * claim_rate if approved else 0
Insurance โ€” calculate payout using conditions
๐Ÿšš
shipping_cost = weight * rate + base_fee
Logistics โ€” calculate costs using arithmetic
๐Ÿค–
predictions = model_output * 100 // 10 * 10
AI โ€” normalize, round, and scale numeric predictions

๐Ÿค– AI & Machine Learning Connection

Every neural network is built on operators. When you train a model, it calculates loss using subtraction and division, then updates weights using multiplication. When you normalize features, you divide by the standard deviation. When you round predictions to integer classes, you use //. Operators are the heartbeat of AI.

numpy pandas scikit-learn TensorFlow

๐ŸŽฏ Mini Challenge

M-Pesa Transaction Fee Calculator

In Nairobi, M-Pesa charges a 1.5% fee on transfers over 100 KSh. Given a transfer amount of 5000 KSh, calculate: (1) the fee using multiplication, (2) the net amount after fee, (3) check if the fee is over 100 KSh using comparison, (4) print both values rounded to 2 decimals.

Start with amount = 5000, fee_rate = 0.015. Calculate fee: fee = amount * fee_rate. Net: net = amount - fee. Check: fee > 100. Use round(fee, 2) for formatting.

๐ŸŽค Interview Questions

/ always returns a float, even if both inputs are integers. 10 / 3 = 3.3333.... // returns an integer by rounding down (floor). 10 // 3 = 3. In banking, // is used for distributing money into equal instalments.
Floats are stored in binary, and many decimal fractions (like 0.1, 0.2) cannot be exactly represented. The result is 0.30000000000000004. For financial calculations, always use the Decimal module instead of float. Never rely on float equality for money.
Operator precedence determines the order Python evaluates expressions. 2 + 3 * 4 is 14, not 20, because * has higher precedence than +. Use parentheses to override: (2 + 3) * 4 = 20. In banking, wrong precedence can calculate interest incorrectly.
and returns True only if both sides are True. or returns True if at least one side is True. not inverts (True โ†’ False). Example: if balance > 0 and is_active: allows withdrawal only if both conditions are met. Short-circuit: or stops at first True; and stops at first False.
divmod(a, b) returns a tuple (quotient, remainder) in one operation. divmod(250, 3) returns (83, 1). Use it when you need both results at once (e.g., splitting a fee among customers). More efficient than calculating a // b and a % b separately, especially for large numbers.

๐Ÿง  Quick Quiz

Question 1 of 5
What does 10 // 3 return?
Question 2 of 5
Calculate: 2 + 3 * 4. What is the result?
Question 3 of 5
What does 10 % 3 return?
Question 4 of 5
Which condition allows a transaction if balance is positive AND account is active?
Question 5 of 5
Why is Decimal('0.1') + Decimal('0.2') == Decimal('0.3') true?
0
/5

๐Ÿ“‹ Quick Cheatsheet

OperatorSymbolExampleResult
Addition+5 + 38
Subtraction-5 - 32
Multiplication*5 * 315
Float Division/10 / 33.333...
Floor Division//10 // 33
Modulo (remainder)%10 % 31
Power**2 ** 38
Equal to==5 == 5True
Not equal!=5 != 3True
Greater than>5 > 3True
Less than<5 < 3False
Logical ANDandTrue and TrueTrue
Logical ORorTrue or FalseTrue
Logical NOTnotnot TrueFalse
round()โ€”round(3.7)4
abs()โ€”abs(-5)5
divmod()โ€”divmod(10, 3)(3, 1)

โš ๏ธ Common Mistakes

Using / when you want an integer
10 / 2 = 5.0 (float). Use // for integer results: 10 // 2 = 5 (int). In banking, never assume / returns an integer.
Forgetting precedence โ€” wrong calculation order
2 + 3 * 4 is 14, not 20. Multiply first. Use parentheses to be explicit: (2 + 3) * 4.
Using floats for money instead of Decimal
0.1 + 0.2 != 0.3 with floats. Always use from decimal import Decimal for financial calculations. Decimal('0.1') + Decimal('0.2') == Decimal('0.3').
Confusing and / or in conditions
and requires both. or requires one. if balance > 0 and is_active: allows action only if both are true. Not or.

โœฆ Key Takeaways

  • Python has three operator families: arithmetic (+ - * / // % **), comparison (== != < > <= >=), and logical (and or not)
  • Use / for float division and // for integer (floor) division โ€” they give different types
  • The % operator returns the remainder โ€” essential for distributing amounts and checking divisibility
  • Operator precedence follows PEMDAS: parentheses, exponents, multiply/divide, add/subtract โ€” use parentheses to be explicit
  • Floats have precision errors (0.1 + 0.2 โ‰  0.3) โ€” use Decimal for banking and finance
  • round(), abs(), and divmod() are built-in tools for common number tasks
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You've mastered operators.
Now build real banking applications.

Join the Best AI Program: The AI-Era Analyst โ€” 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Analyst โ†’