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.
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.
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:
Three things worth memorising from that list:
/always returns a float in Python 3, even when both sides are integers.10 / 2gives you5.0, not5. If you want an integer, use//.//rounds toward negative infinity, so-7 // 2is-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:
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:
- Don't compare floats with
==. If you need "close enough," usemath.isclose(a, b)or check whetherabs(a - b) < some_tolerance. - For money, use
decimal.Decimal. The standard library'sdecimalmodule gives exact decimal arithmetic — no surprises like0.1 + 0.2. It's slower than float, which is why it's not the default, but it's the right call for currency.
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:
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:
The boolean operators are and, or, and not, written as English words rather than symbols:
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:
False0,0.0None- Empty containers:
"",[],{},set(),()
Everything else is truthy. This lets you write more natural-looking checks:
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:
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:
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 useDecimalfor money. - Truthiness makes conditions read better — as long as you remember that
0and 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."