Numbers, Operators, and Conversions
Calculate with Python’s numeric operators, understand division and precedence, and convert external text only after validating what it represents.
Before this lesson
Predict numeric operator results
Use explicit conversions at boundaries
Avoid precedence and rounding mistakes
The short answer
Python provides arithmetic, comparison, and boolean operators with defined precedence. `/` returns a float, `//` performs floor division, and `%` gives a remainder. Convert input at the boundary and keep the rest of the program working with meaningful numeric values.
Build the mental model
Operators express a calculation, but their types matter. True division always produces a float. Floor division rounds toward negative infinity, which differs from truncating toward zero for negative values. Exponentiation binds more tightly than multiplication.
The central idea in this lesson is numeric expressions. Read the rule, predict a concrete outcome, and then run the smallest example that can confirm or reject the prediction. This separates knowledge from familiarity with syntax.
Make the design choice explicit
Use parentheses when they clarify intent, even when precedence would produce the same result. Convert strings with int or float close to input handling, catch invalid forms there, and pass already-validated values into calculation functions.
subtotal = 42.50
tax_rate = 0.08
tax = subtotal * tax_rate
total = subtotal + tax
print(f"Tax: USD {tax:.2f}")
print(f"Total: USD {total:.2f}")Expected output
Tax: USD 3.40 Total: USD 45.90
Trace the example before running it. Identify each input, transformation, returned value, and side effect. Then change one boundary value and explain why the new behavior follows from the rule rather than memorizing the output.
Recognize failure modes
float cannot exactly represent many decimal fractions. For display, format explicitly; for financial rules, use decimal.Decimal with a documented rounding policy. Never use eval to turn user text into a number.
Use a professional practice loop
Turn the concept into a repeatable workflow: write down the expected behavior, implement one coherent change, run a representative example, and retain a regression check. If the result surprises you, capture the exact input and error before changing anything.
Review the program for names, boundaries, and hidden side effects. A solution is complete when another developer can understand its contract, reproduce its setup, and verify both the successful path and one meaningful failure path.
Quick knowledge check
Answer before you reveal.
01What is the difference between / and //?
Slash performs true division; double slash performs floor division toward negative infinity.
02What four steps make the practice loop reliable?
Predict the behavior, make one coherent change, run it, and retain a check that detects regression.
Exercise
Practice challenge
Repair the starter program, then add a discount percentage and calculate the final total in an explicit, readable order.
Requirements
- The successful path produces the documented result
- At least one boundary or invalid case is handled deliberately
- Calculation or domain logic is separated from console interaction
Optional extension: Add one automated regression check for the most important rule.
Open in Python compilerLesson checkpoint
One small step locks it in
Mark this lesson complete, then keep the momentum going.
Clear up the details
Frequently asked questions
Is numeric expressions only important in large programs?
No. Small programs reveal the same rules with less noise, and learning the rule early prevents fragile habits from becoming architecture.
Should I memorize every API used here?
No. Memorize the mental model and how to verify behavior. Use documentation for exact names and parameters when needed.
How do I know the exercise is finished?
Meet every success criterion, test at least one boundary or failure case, and explain why the output follows from the code.