TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 4 of 22 · By Ethan Cole · Updated November 21, 2026

Python Numbers: int, float and Arithmetic Operators

Python has two number types you will use constantly: int for whole numbers and float for numbers with a fractional part. Python integers have no fixed size limit, so 2 ** 100 is computed exactly. Floats follow the standard double-precision format used by almost every computer.

The arithmetic operators

  • +, -, * add, subtract and multiply.
  • / is true division and always returns a float, even for 8 / 2 (which gives 4.0).
  • // is floor division: it divides and rounds down to the nearest whole number.
  • % gives the remainder after floor division. n % 2 == 0 is the classic test for an even number.
  • ** raises to a power.

Python follows the usual precedence: ** first, then *, /, // and %, then + and -. Use parentheses whenever the order is not obvious to a reader.

Python 3 · editable

Expected output

3.5
3
1
1024
-4
0.30000000000000004
0.3
43.5

Note the fifth line. Floor division rounds toward negative infinity, not toward zero, so -7 // 2 is -4. That surprises people coming from languages that truncate.

Why 0.1 + 0.2 is not 0.3

Floats are stored in binary, and most decimal fractions, including 0.1, have no exact binary form. The tiny representation errors show up as 0.30000000000000004. This is not a Python bug; every language using IEEE 754 doubles behaves the same way. Round results for display with round() or format specifiers, and for money use the decimal module or count whole cents as integers.

Converting between types

int("42") turns text into an integer and float("1.5") into a float. Mixing an int and a float in one calculation gives a float. int(3.9) truncates to 3; use round() when you want the nearest whole number.

Common mistakes

  • Expecting / to return an integer. Use // when you need whole-number division.
  • Comparing floats with ==. Check that the difference is tiny, for example with math.isclose().
  • Adding a number to text such as "5" + 3, which raises TypeError. Convert first.

Summary

  • int is unlimited in size; float is an approximate binary fraction.
  • / always gives a float; // floors; % is the remainder; ** is power.
  • Floor division rounds toward negative infinity.
  • Round floats for display, and avoid floats for exact money.

Quiz: Numbers and math

1. What is the result of 9 // 4?

Floor division divides and rounds down: 9 / 4 is 2.25, floored to 2.

2. What does 10 / 5 return in Python 3?

The / operator always produces a float.

3. Which expression tests whether n is even?

% gives the remainder; an even number leaves remainder 0 when divided by 2.

4. Why does 0.1 + 0.2 print 0.30000000000000004?

Binary floating point represents most decimal fractions approximately; the error becomes visible here.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.