Lesson 4 of 30

In progress

Course progress

Python

Numbers, operators and rounding that makes sense

Use Python's arithmetic operators, understand integer division and remainders, and avoid the floating-point surprises that break totals.

A calculator that gives the wrong change is worse than no calculator. Numbers look simple until division, rounding or money is involved. This lesson makes those cases predictable.

What you will learn

  • Use +, -, *, /, //, % and ** correctly.
  • Predict whether a result is an int or a float.
  • Round for display without corrupting the stored value.

The seven operators you need

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

The first three are familiar. The interesting ones are:

  • / always returns a float, so 7 / 2 is 3.5 and 8 / 2 is 4.0.
  • // is floor division: it divides and rounds down, so 7 // 2 is 3.
  • % is the remainder, so 7 % 2 is 1.
  • ** is a power, so 7 ** 2 is 49.

Floor division rounds towards negative infinity, not towards zero. -7 // 2 is -4, not -3. This matters when you split items into pages or groups and one value is negative.

Remainders are more useful than they look

% answers "what is left over", which is how you detect even numbers, convert minutes to hours, and cycle through a list.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

Minutes to hours is the clearest real example: divide for whole hours, remainder for the minutes left over. No rounding rules to remember.

Precedence and brackets

Python multiplies before it adds, just like school arithmetic. 2 + 3 * 4 is 14. Brackets change that: (2 + 3) * 4 is 20.

Three built-in helpers save writing a loop: abs(-4) is 4, min(3, 9, 2) is 2, and max returns the largest of its arguments. divmod(17, 5) returns both the quotient and the remainder as a pair, which is exactly what you need when converting a total into groups.

Do not memorise a precedence table. Add brackets when the order is not obvious to a reader. (a + b) / 2 is clearer than relying on the reader to know that division happens after the addition inside the brackets anyway.

Floats are approximate

Try this:

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

The first line prints 0.30000000000000004, and the second prints False. This is not a Python bug. Binary floating point cannot represent 0.1 exactly, in the same way that decimal notation cannot represent one third exactly. The same issue appears in JavaScript, Java and C#, so treat it as a habit rather than a Python quirk.

To compare two computed floats, subtract one from the other and check that the difference is smaller than a small tolerance, rather than comparing them with ==.

For learning exercises, round(value, 2) is enough:

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

For money in a real application, store whole paise or cents as integers, or use the decimal module. Never store rupees as a float and hope the rounding works out.

A common error is writing round(2.675, 2) and expecting 2.68. It returns 2.67 because the stored value is slightly below 2.675. When a total must be exact, work in the smallest unit (paise), round once at the end, and display with :.2f.

A shop sells pens at 12 rupees each. Ask for a quantity, then print the total, the amount after a 10 percent discount, and the change from 500 rupees. Round money to two decimals only when displaying.

Show solution
Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

For an exact answer in a real shop, multiply in paise: total_paise = quantity * 1200, apply the discount on integers, then divide by 100 at the end.

Recap

  • / gives a float, // gives a whole number rounded down, % gives the remainder.
  • Brackets beat precedence arguments and make intent visible.
  • Floats are approximate; round for display and use integers for exact money.

Next, you will learn to read the error messages Python prints, which is the fastest way to stop guessing when something breaks.

Why is 8 / 2 a float instead of an integer?
Python’s `/` always returns a float so the result type does not change based on the numbers. Use `//` when you want a whole number.
What does 0.1 + 0.2 show in Python?
It shows `0.30000000000000004`. The value is extremely close to 0.3, and comparing it with `==` to 0.3 returns False. Compare with a tolerance or round before comparing.
Should I use round() or :.2f in an f-string?
Use :.2f for display and round() when the rounded number itself is needed for later calculation. Neither fixes floating-point storage; for exact money, work in whole paise or cents.

Full syllabus

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.