Beginner·5 min·basics · math
Numbers & Math
You’ll be able to
- Perform integer, float, and modulo arithmetic accurately
- Choose between
/(float division) and//(floor division) - Use
mathmodule functions likesqrt,log,pow - Round and format numbers with
round()and f-string precision
Why this matters
Float arithmetic quirks like 0.1 + 0.2 != 0.3 cause real production bugs in billing, trading, and analytics code. Libraries like decimal and numpy exist precisely because devs kept losing rupees to floating-point drift.
Common pitfalls
- Using
/when you meant//. Integer division matters for indexing;len(x)/2gives a float and breaks slicing. - Forgetting operator precedence:
2 ** 3 ** 2is512, not64, because**is right-associative. - Comparing floats with
==. Usemath.isclose(a, b)for anything derived from arithmetic.
Python handles integers and floats natively. The math module adds the rest.
Operators worth remembering
+ - * /— the usual//— floor division (drops the remainder)%— modulo (the remainder)**— exponent
Built-ins
abs(), round(), min(), max(), sum([...]).
Try it
- Compute compound interest:
1000 * (1.07 ** 10). - Use
math.log(1024, 2)— guess the answer first.
Practice
3 graded exercises
Write the code, click Check. We’ll tell you exactly what to fix.
Progress0 / 3
- Exercise 1
Print the integer (floor) division of
100by7. Output should be exactly14. - Exercise 2
Print
2raised to the power of8. Output should be exactly256. - Exercise 3
Import
mathand print π (pi) rounded to 3 decimal places. Output should be exactly3.142.