At the core of computation lies the ability to work with numbers. Whether you're counting items, calculating distances, or measuring physical properties, numbers are indispensable. Python provides built-in support for several numeric types, but the two most fundamental ones you'll encounter constantly are integers and floating-point numbers.
Integers represent whole numbers. These can be positive, negative, or zero, but they cannot have a fractional or decimal component. Think of them as numbers you use for counting discrete items: the number of users, the steps in a loop, or the score in a game.
In Python, you create an integer simply by writing the number without a decimal point:
# Examples of integers
user_count = 150
temperature_celsius = -5
year = 2024
zero_value = 0
print(user_count)
print(temperature_celsius)
Python integers can be arbitrarily large, limited only by the available memory of your system. You don't need to worry about different integer sizes like short
, int
, or long
as you might in other programming languages.
You can confirm the type of a number using the built-in type()
function:
age = 30
print(type(age)) # Output: <class 'int'>
Floating-point numbers, or floats, represent real numbers, including those with a fractional part. They are essential when dealing with measurements, percentages, or any value where precision beyond whole numbers is needed.
You create a float by including a decimal point or by using scientific notation (e notation).
# Examples of floating-point numbers
pi_approx = 3.14159
price = 49.99
temperature_fahrenheit = -20.5
account_balance = 0.0 # Using a decimal makes it a float
distance_meters = 1.5e3 # Scientific notation for 1.5 * 10^3, which is 1500.0
print(pi_approx)
print(distance_meters)
Even if the number represents a whole value, including a decimal point makes Python treat it as a float:
whole_number_float = 10.0
print(whole_number_float) # Output: 10.0
print(type(whole_number_float)) # Output: <class 'float'>
int
when you need exact whole numbers: counts, indices, identifiers.float
when dealing with measurements, fractions, probabilities, or when calculations might result in non-whole numbers.It's important to know that floating-point arithmetic on computers isn't always perfectly precise. Due to the way these numbers are stored internally (typically using a binary format like IEEE 754), calculations involving floats can sometimes yield results that are very slightly off from what you'd expect mathematically. For example, 0.1+0.2 might result in a value like 0.30000000000000004 instead of exactly 0.3. For most applications, this tiny difference is negligible, but it's something to be aware of, especially in financial calculations or scientific simulations where high precision is required (Python offers other tools like the Decimal
type for such cases, which are beyond our current scope).
Understanding integers and floats is fundamental to performing almost any kind of calculation or quantitative analysis in Python. As we proceed, you'll see how these numeric types interact with operators to perform arithmetic.
© 2025 ApX Machine Learning