Python Numbers: Integers & Floats Guide
Python numbers form the foundation of data manipulation in any program. Integers represent whole values like 10 or -5, while floats represent decimals like 3.14. This guide covers creating, operating on, and converting between these core numeric types with runnable examples.
What Are Python Numbers?
Python supports two primary numeric types: int for whole numbers and float for decimal values. Both can be positive or negative, and Python automatically handles arbitrarily large integers without overflow. Understanding these types is essential because they power calculations, loop counters, and decision logic throughout any Python project.
How Do You Work with Integers?
Integers are whole numbers, both positive and negative, without any decimal points. In Python, integers can be of any size, limited only by your computer's memory.
# Integer examples
my_integer = 10
negative_integer = -5
large_integer = 1_000_000 # You can use underscores for readability
zero_value = 0
# Check the type
print(type(my_integer)) # Output: <class 'int'>
Python's int type is flexible—you can use binary (0b), octal (0o), or hexadecimal (0x) notation for special cases:
binary_num = 0b1010 # 10 in binary
octal_num = 0o12 # 10 in octal
hex_num = 0xA # 10 in hexadecimal
How Do You Work with Floating-Point Numbers?
Floating-point numbers, or floats, are numbers that have a decimal point. They are used to represent real numbers and can also be expressed in scientific notation.
# Float examples
my_float = 3.14
negative_float = -0.5
scientific_notation = 2.5e2 # This is 2.5 * 10^2, or 250.0
# Check the type
print(type(my_float)) # Output: <class 'float'>
Important note on float precision: Due to how computers store floating-point numbers using binary representation, you might sometimes see small rounding errors. For example:
result = 0.1 + 0.2
print(result) # Output: 0.30000000000000004 (not exactly 0.3)
This is a well-known limitation in all programming languages. For financial applications requiring exact decimals, use Python's decimal module (https://docs.python.org/3/library/decimal.html) instead.
What Arithmetic Operations Can You Perform on Numbers?
Python supports all the standard arithmetic operations you'd expect:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | True Division | 10 / 5 | 2.0 (always returns float) |
// | Floor Division | 10 // 3 | 3 (discards remainder) |
% | Modulus | 10 % 3 | 1 (returns remainder) |
** | Exponentiation | 10 ** 2 | 100 (10 to the power of 2) |
# Practical examples
price = 19.99
quantity = 3
total = price * quantity
print(total) # Output: 59.97
# Order of operations (PEMDAS) applies
result = 2 + 3 * 4
print(result) # Output: 14 (multiplication first)
# Use parentheses to override
result = (2 + 3) * 4
print(result) # Output: 20
How Do You Convert Between Number Types?
You can easily convert between integers and floats using the int() and float() functions. This is useful when you receive data in one format but need another.
# Converting float to int (truncates decimal part)
my_float = 3.14
my_integer = int(my_float)
print(my_integer) # Output: 3
# Converting int to float
my_integer = 10
my_float = float(my_integer)
print(my_float) # Output: 10.0
# Converting strings to numbers
str_num = "42"
converted = int(str_num)
print(converted + 8) # Output: 50
# String to float
str_float = "3.14"
converted_float = float(str_float)
print(converted_float) # Output: 3.14
Watch out: Converting a non-numeric string raises an error:
int("hello") # ValueError: invalid literal for int() with base 10: 'hello'
Use error handling (try/except) when converting untrusted user input.
Key Takeaways
- Integers (
int) are whole numbers; Python supports arbitrary-precision integers that grow as needed - Floats (
float) represent decimal numbers using binary representation; small rounding errors are normal - Seven arithmetic operators (
+,-,*,/,//,%,**) work on both types - Type conversion between
intandfloatis straightforward usingint()andfloat()functions - Float precision issues are unavoidable—use the
decimalmodule for financial calculations
Frequently Asked Questions
What is the difference between / and // in Python?
The / operator performs true division and always returns a float (e.g., 10 / 3 = 3.3333...). The // operator performs floor division, discarding the decimal part and returning the integer result (e.g., 10 // 3 = 3). Floor division always rounds down toward negative infinity.
Why does 0.1 + 0.2 not equal 0.3 in Python?
Floats are stored in binary format, and decimal values like 0.1 cannot be represented exactly in binary. This causes tiny rounding errors. This occurs in all programming languages. For exact decimal arithmetic, import and use Python's decimal.Decimal class.
Can integers in Python be negative?
Yes, integers can be positive, negative, or zero. Python's int type handles negative numbers seamlessly. For example, my_int = -42 is valid and behaves exactly like a positive integer in arithmetic operations.
What's the largest integer Python can handle?
There is no fixed limit—Python integers can be arbitrarily large, limited only by available memory. This differs from languages like C or Java, where integer size is fixed. You can safely work with numbers with thousands of digits.
When should I use float instead of int?
Use float when you need to represent non-whole numbers (measurements, ratios, percentages) or perform division. Use int for counts, indices, and whole quantities. Always use float for scientific calculations where decimal precision is required.