Numeric data types
Numeric data types
Numeric Data Types
Every value in Python has a type. In this lesson we will cover the three numeric types you will use most: int, float, and bool.
int — Integers
int represents whole numbers — positive, negative, or zero:
No Overflow
In C++, integers have a fixed size in memory, which means they can overflow — exceed their maximum value and wrap around to a wrong number. Python's int has no such limit. It grows as large as needed, limited only by your computer's memory:
This is a significant advantage in competitive programming where intermediate calculations can get very large.
float — Floating-Point Numbers
float represents real numbers with a decimal point:
Unlike C++ which has both float and double, Python only has one floating-point type — float — and it corresponds to a 64-bit double under the hood, so it always gives you double precision.
Precision Limitations
float cannot represent all real numbers exactly. This is a fundamental limitation of how computers store decimal numbers:
For competitive programming, this means you should avoid comparing floats with ==. Instead, check if the difference is small enough:
You can control how many decimal places are printed using round() or f-string formatting:
bool — Boolean Values
bool represents logical truth: either True or False. We already met booleans when working with comparison operators — every comparison returns a bool:
In Python, bool is actually a subtype of int. True equals 1 and False equals 0, which means you can use booleans in arithmetic — something that occasionally comes in handy in CP:
Dynamic Typing
In C++, you must declare the type of every variable explicitly. Python figures out the type automatically from the value you assign — this is called dynamic typing:
The same variable can hold different types at different points in the program. You can always check the current type with type():
type() works on any value — not just numbers. We will cover strings in detail in a later module, but it is good to know from the start that they are a distinct type.