Menu
Try in Playground

Python Numbers and Booleans: int, float, bool, and Arithmetic

How Python handles integers, floating-point numbers, and booleans — arithmetic, conversion, and the edge cases that trip people up.

Two Kinds of Numbers, One Kind of Truth

The numeric types you'll use day-to-day are int and float, plus bool for true/false values. They're enough for nearly all the arithmetic and logic a normal Python program needs.

main.py
Output
Click Run to see the output here.

Python sometimes calls booleans a subtype of integers — True is literally 1 and False is literally 0 — which opens a few useful tricks later.

Integer Arithmetic

Integers in Python never overflow. That sounds unimportant until you write your first factorial function and realize most other languages would have silently rolled over to a negative number by now.

main.py
Output
Click Run to see the output here.

Both of those produce exact answers, no matter how large. Memory is the only ceiling.

The arithmetic operators are what you'd expect, plus a couple:

main.py
Output
Click Run to see the output here.

Three things worth memorising from that list:

  • / always returns a float in Python 3, even when both sides are integers. 10 / 2 gives you 5.0, not 5. If you want an integer, use //.
  • // rounds toward negative infinity, so -7 // 2 is -4, not -3. Unusual, but consistent.
  • % gives the remainder, and it's handy for "is this number even?" (n % 2 == 0) and for wrapping around a fixed range.

Floats and Their Famous Pitfall

Floats are imperfect by design. They're stored in binary, which can't represent most decimal fractions exactly. The textbook example:

main.py
Output
Click Run to see the output here.

The first line prints 0.30000000000000004. The second prints False. Every language that follows the IEEE 754 standard — Python, JavaScript, Java, C — has this behavior. It's not a bug in Python.

In practice, this means two things:

  1. Don't compare floats with ==. If you need "close enough," use math.isclose(a, b) or check whether abs(a - b) < some_tolerance.
  2. For money, use decimal.Decimal. The standard library's decimal module gives exact decimal arithmetic — no surprises like 0.1 + 0.2. It's slower than float, which is why it's not the default, but it's the right call for currency.
main.py
Output
Click Run to see the output here.

Converting Between Number Types

Python won't silently convert an integer to a string or a string to a number. You ask for it explicitly:

main.py
Output
Click Run to see the output here.

Invalid conversions raise ValueError:

>>> int("hello")
ValueError: invalid literal for int() with base 10: 'hello'

The error message is blunt and easy to act on. Clean the input or catch the exception.

Booleans, in a Little Depth

True and False are the two boolean values — spelled exactly like that, capital letter first. Most comparisons produce one of them:

main.py
Output
Click Run to see the output here.

The boolean operators are and, or, and not, written as English words rather than symbols:

main.py
Output
Click Run to see the output here.

One subtlety that's useful to know: and and or don't necessarily return True or False — they return the value that decided the expression. 0 or "fallback" returns "fallback"; 5 and 10 returns 10. This lets you write short defaults like name = user_input or "anonymous", but it's also worth understanding if a boolean check ever gives you an unexpected type.

Truthy and Falsy

Python treats many values as "true-ish" or "false-ish" when you use them in a boolean context like if or while. The falsy values are:

  • False
  • 0, 0.0
  • None
  • Empty containers: "", [], {}, set(), ()

Everything else is truthy. This lets you write more natural-looking checks:

main.py
Output
Click Run to see the output here.

Read if name: as "if name has anything in it." Much cleaner than if name != "":. Just be careful with numbers — if count: will treat 0 as "skip," which is usually but not always what you want. If zero is a legitimate value, use if count is not None: instead.

Boolean Arithmetic

Because booleans are integers, you can do arithmetic on them:

main.py
Output
Click Run to see the output here.

That trick comes up often enough to stash away. If you want to count how many items in a list pass some test, sum a generator that returns booleans:

main.py
Output
Click Run to see the output here.

The Practical Takeaways

  • Integers never overflow. Use them freely.
  • / returns a float; use // when you want integer division.
  • Don't compare floats with ==; compare with a tolerance, or use Decimal for money.
  • Truthiness makes conditions read better — as long as you remember that 0 and empty strings are falsy.

Next: input and print, the two tools that turn your scripts from silent programs into conversations.

Frequently Asked Questions

What's the difference between int and float in Python?

An int is a whole number with no decimal point; a float is a number with a decimal point. Integers in Python can be any size — there's no overflow. Floats follow the standard IEEE 754 rules, which means they can lose tiny amounts of precision on decimal arithmetic.

Why does 0.1 + 0.2 not equal 0.3 in Python?

Because binary floating-point numbers can't represent most decimals exactly. 0.1 + 0.2 evaluates to 0.30000000000000004. This isn't a Python bug — every language with IEEE 754 floats has the same behavior. For exact decimal math, use the decimal module.

What are truthy and falsy values in Python?

Python treats many values as true or false in boolean contexts, even if they aren't literally True or False. Zero, empty strings, empty lists, empty dicts, and None are falsy; most other values are truthy. if my_list: reads as "if the list has anything in it."

Learn to code with Coddy

GET STARTED