โ 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).
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
๐ฆ The Business Scenario
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.
โ Arithmetic Operators
โก 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.
๐ 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.
โ ๏ธ 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.
โ Comparison & Logical Operators
All Operators in One Table
balance > 50000is_active and balance > 0interest = principal * rateround(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):
()โ Parentheses**โ Exponentiation* / // %โ Multiply, divide, floor, modulo+ -โ Add, subtract== != < > <= >=โ Comparisonsnotโ Logical NOTandโ Logical ANDorโ Logical OR
Example: 2 + 3 * 4 is 2 + 12 = 14, not 5 * 4 = 20. Multiply first.
๐ Also Used In
Operators Are Universal
๐ค 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.
๐ฏ Mini Challenge
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.
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.0.30000000000000004. For financial calculations, always use the Decimal module instead of float. Never rely on float equality for money.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
10 // 3 return?2 + 3 * 4. What is the result?10 % 3 return?Decimal('0.1') + Decimal('0.2') == Decimal('0.3') true?๐ Quick Cheatsheet
| Operator | Symbol | Example | Result |
|---|---|---|---|
| Addition | + | 5 + 3 | 8 |
| Subtraction | - | 5 - 3 | 2 |
| Multiplication | * | 5 * 3 | 15 |
| Float Division | / | 10 / 3 | 3.333... |
| Floor Division | // | 10 // 3 | 3 |
| Modulo (remainder) | % | 10 % 3 | 1 |
| Power | ** | 2 ** 3 | 8 |
| Equal to | == | 5 == 5 | True |
| Not equal | != | 5 != 3 | True |
| Greater than | > | 5 > 3 | True |
| Less than | < | 5 < 3 | False |
| Logical AND | and | True and True | True |
| Logical OR | or | True or False | True |
| Logical NOT | not | not True | False |
round() | โ | round(3.7) | 4 |
abs() | โ | abs(-5) | 5 |
divmod() | โ | divmod(10, 3) | (3, 1) |
โ ๏ธ Common Mistakes
/ when you want an integer10 / 2 = 5.0 (float). Use // for integer results: 10 // 2 = 5 (int). In banking, never assume / returns an integer.2 + 3 * 4 is 14, not 20. Multiply first. Use parentheses to be explicit: (2 + 3) * 4.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').and / or in conditionsand 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) โ useDecimalfor banking and finance round(),abs(), anddivmod()are built-in tools for common number tasks
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 โ