Python with AI Tutorial · Chapter 7 of 48
Python numbers come in three built-in types: int for whole numbers of any size, float for decimals and complex for numbers with an imaginary part. Python supports the usual arithmetic operators plus integer division, modulus and exponent, and the math and decimal modules extend them. This chapter covers each type and the operations you will use in business calculations.
int: whole numbers
An int has no decimal point and, unlike Excel or most other languages, no upper limit. Python happily multiplies numbers with hundreds of digits. You can write underscores inside large literals to make them readable; Python ignores them.
units_sold = 1_250_000
unit_price_paise = 4999 # price stored in paise to avoid decimals
revenue_paise = units_sold * unit_price_paise
print(revenue_paise)
print(2 ** 100)
Output: 6248750000 1267650600228229401496703205376
Storing money as an integer number of paise or cents, as in the example above, is a common trick in finance systems. Whole numbers never suffer rounding drift, so totals reconcile perfectly, and you only divide by 100 at the very end when formatting the amount for a report. Keep it in mind when you design your own data structures later in the course.
float: decimal numbers
A float is any number with a decimal point or written in scientific notation. Floats are stored in 64-bit binary, exactly as Excel stores them, so a few decimal fractions cannot be represented precisely. For display, round them; for accounting, see the decimal section below.
price = 1299.50
growth = 3.5e-2 # 0.035 in scientific notation
print(round(price * (1 + growth), 2))
print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))
Output: 1344.98 0.30000000000000004 0.3
complex: numbers with an imaginary part
A complex number is written with a j suffix, for example 3+4j. You will rarely need it in business work, but it exists so that engineering and signal-processing code can use standard operators.
z = 3 + 4j
print(z.real, z.imag)
print(abs(z))
print(type(z))
Output: 3.0 4.0 5.0 <class 'complex'>
Arithmetic operators
Python has seven arithmetic operators. Three of them trip up newcomers: / always returns a float, // returns the floor (rounded down) and % returns the remainder. Exponent is **, not ^.
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 1200 + 350 |
1550 |
- |
Subtraction | 1200 - 350 |
850 |
* |
Multiplication | 12 * 99.5 |
1194.0 |
/ |
Division (always float) | 1000 / 8 |
125.0 |
// |
Floor division | 1000 // 300 |
3 |
% |
Modulus (remainder) | 1000 % 300 |
100 |
** |
Exponent | 1.05 ** 3 |
1.157625 |
budget = 10000
seat_cost = 1450
seats = budget // seat_cost
left_over = budget % seat_cost
print("Seats affordable:", seats)
print("Budget remaining:", left_over)
print("Value after 3 years at 5%:", round(budget * 1.05 ** 3, 2))
Output: Seats affordable: 6 Budget remaining: 1300 Value after 3 years at 5%: 11576.25
Floor division and modulus appear constantly in real work even though they look academic. // answers "how many whole units fit?" (cartons per pallet, full weeks in a project, seats within a budget) and % answers "what is left over?". Together they also drive alternating row colours in reports (row % 2) and convert minutes into hours and minutes (divmod(minutes, 60)).
2 ^ 3 expecting 8. In Python ^ is bitwise XOR and returns 1. Always use ** for powers.Order of operations
Python follows the same precedence as school maths and Excel: brackets, then exponent, then multiplication and division, then addition and subtraction. When in doubt, add brackets; they cost nothing and make the intent explicit.
gross = 50000
deductions = 8000
tax_rate = 0.20
print(gross - deductions * tax_rate)
print((gross - deductions) * tax_rate)
Output: 48400.0 8400.0
Built-in number functions
Several helpers are always available without an import. round(), abs(), min(), max() and sum() cover most reporting needs, and divmod() returns quotient and remainder together.
variances = [-1250.5, 340.0, -87.25, 910.75]
print("Largest shortfall:", min(variances))
print("Total variance:", round(sum(variances), 2))
print("Absolute of worst:", abs(min(variances)))
print(divmod(365, 7))
Output: Largest shortfall: -1250.5 Total variance: -87.0 Absolute of worst: 1250.5 (52, 1)
The math module
For anything beyond basic arithmetic, import math. It provides square roots, ceilings and floors, logarithms, trigonometry and constants such as pi. The math module chapter covers it fully; here are the three functions you will reach for first.
import math
boxes_needed = math.ceil(1375 / 100) # items per box = 100
print("Boxes:", boxes_needed)
print("Full pallets:", math.floor(boxes_needed / 6))
print("Hypotenuse of 3-4-5 triangle:", math.sqrt(9 + 16))
Output: Boxes: 14 Full pallets: 2 Hypotenuse of 3-4-5 triangle: 5.0
Notice that math.ceil is the right tool for "how many boxes do I need?" because a partial box still has to be shipped, while math.floor suits "how many complete pallets can I load?". Choosing between rounding up, rounding down and rounding to nearest is a business decision, not a maths one, so make it explicit in the code rather than relying on round() by default.
Exact money maths with decimal
Because floats accumulate tiny errors, accountants and payment systems use the decimal module, which stores numbers in base 10 exactly as you write them. Create decimals from strings, not floats, or you import the float error along with the value.
from decimal import Decimal, ROUND_HALF_UP
price = Decimal("19.99")
qty = 3
subtotal = price * qty
gst = (subtotal * Decimal("0.18")).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print(subtotal)
print(gst)
print(subtotal + gst)
Output: 59.97 10.79 70.76
Use float for analysis, statistics and charts where a billionth of a rupee does not matter, and Decimal for invoices, payroll and anything that must reconcile to the paisa.
Ask the assistant to explain the float rounding surprise in plain language and to show you exactly when it matters for money.
In Python 3.12, 0.1 + 0.2 prints 0.30000000000000004. Explain why in under 100 words without using the phrase "floating point" more than once. Then show me a 12-line example of an invoice with 20 line items where using float gives a total that is off by one paisa, and rewrite the same example with the decimal module so it is exact.
Turn a familiar Excel formula into Python and have the assistant walk through the operator precedence.
Convert this Excel formula into Python and explain the order in which Python evaluates each operator: =ROUND((B2*C2)*(1-D2)*(1+E2), 2) where B2 is quantity 12, C2 is unit price 249.99, D2 is discount 0.15 and E2 is tax 0.18. Show the result and then show what happens to the result if I remove the inner brackets.
Common mistakes
- Using
^for powers. It is XOR; use**. - Expecting
7 / 2to give 3. Division always produces a float (3.5); use//for 3. - Comparing floats with
==. Usemath.isclose(a, b)or round both sides first. - Creating
Decimal(0.1)from a float; writeDecimal("0.1")instead. - Forgetting that
round(2.5)gives 2, not 3. Python rounds half to even; useDecimalwithROUND_HALF_UPfor accounting rules.
Exercise
A warehouse receives 2,347 items and packs them 24 to a carton. Calculate how many full cartons are packed, how many loose items remain, and the total shipping cost if each full carton costs 85.50 and loose items are charged 4.25 each. Print all three values, rounding the cost to 2 decimals.
Show answer
items = 2347
per_carton = 24
full_cartons = items // per_carton
loose = items % per_carton
cost = full_cartons * 85.50 + loose * 4.25
print("Full cartons:", full_cartons)
print("Loose items:", loose)
print("Shipping cost:", round(cost, 2))
Output: Full cartons: 97 Loose items: 19 Shipping cost: 8374.25
Related chapters
FAQ
What is the difference between int and float in Python?
An int is a whole number with unlimited size, such as 250. A float has a decimal point, such as 250.0 or 19.99, and is stored in 64-bit binary, so it can carry tiny rounding errors.
Why does 0.1 + 0.2 not equal 0.3 in Python?
Binary floating point cannot store 0.1 or 0.2 exactly, so their sum is 0.30000000000000004. Use round() for display or the decimal module when the result must be exact, such as in invoices.
How do I do integer division in Python?
Use the double slash operator: 17 // 5 gives 3. The single slash 17 / 5 gives 3.4, and 17 % 5 gives the remainder 2. divmod(17, 5) returns both as (3, 2).
Working with spreadsheets too? Ready-made Excel, Google Sheets and Power BI templates are at NextGenTemplates.com.
Chapter 7 of 48 · Python with AI: all 48 chapters



