Integers
Integers, data type int, are interpreted as numbers, enabling you to perform standard
mathematical operations efficiently. Integers represent whole numbers, crucial for various
mathematical and computational tasks. No decimal nonsense here โ just clean, whole numbers. ๐ข
Declaring integers in Python is a breeze, following the same straightforward syntax used for all
variables. You assign a value to a variable name using the assignment operator (=). For instance,
you can declare integers like this:
| Declaring Integer Variables | |
|---|---|
Readable Large Numbers
Python lets you use underscores in numbers for readability. 1_000_000 is the same as
1000000, but much easier to read. Your eyes will thank you. ๐
Combining Integers with Strings
Exercise caution when combining an int with a string. To ensure correct output, you must
cast the int as a string within the context of the print() statement. For example,
when working with the variable my_age = 12, a standard concatenated print statement would
need to cast the variable like this: print("My age is " + str(my_age)). This casting
operation ensures that the int value of my_age is correctly interpreted as part of the
string. Python can also handle this automatically through the use of "F-strings" (the
preferred approach).
Arithmetic Operations
The primary use of an int in Python is mathematical operations. Whether you're calculating the
area of geometric shapes, managing quantities, or working on numerical algorithms, integers are
essential.
Basic Operators
| Basic Arithmetic | |
|---|---|
Division: The Three Flavors ๐ฆ
This is where integers get interesting. Python has three different division operators:
| Division Types | |
|---|---|
Let's break these down:
| Operator | Name | What It Does | Result Type |
|---|---|---|---|
/ |
True division | Divides and keeps decimals | Always float |
// |
Floor division | Divides and rounds down | int if both operands are int |
% |
Modulo | Returns the remainder | int if both operands are int |
True Division Always Returns a Float
Even if the division is "clean," / returns a float:
If you need an integer result, use // or wrap in int().
Floor Division with Negative Numbers
Here's a gotcha that trips people up โ floor division rounds toward negative infinity, not toward zero:
| Floor Division Gotcha | |
|---|---|
This is mathematically consistent but can be surprising if you expect truncation toward zero.
The Modulo Operator in Action
The modulo operator (%) is more useful than it might seem:
Operator Precedence
Python follows standard mathematical order of operations (PEMDAS/BODMAS):
| Order of Operations | |
|---|---|
| Priority | Operators | Description |
|---|---|---|
| 1 (highest) | ** |
Exponentiation |
| 2 | +x, -x |
Unary plus/minus |
| 3 | *, /, //, % |
Multiplication, division, modulo |
| 4 (lowest) | +, - |
Addition, subtraction |
When in Doubt, Use Parentheses
Even if you know the precedence rules, parentheses make your intent clear:
Useful Integer Functions
Python provides several built-in functions for working with integers:
Number Systems
Python can work with numbers in different bases. This is handy when dealing with low-level programming, networking, or just impressing your friends. ๐ค
Binary, Octal, and Hexadecimal Literals
Converting Between Bases
When Would You Use This?
- Binary: Bit manipulation, flags, permissions
- Hexadecimal: Colors (
#FF5733), memory addresses, MAC addresses - Octal: Unix file permissions (
chmod 755)
# RGB color as hex
red = 0xFF
green = 0x57
blue = 0x33
print(f"#{red:02X}{green:02X}{blue:02X}") # #FF5733
# Unix permissions
rwx_owner = 0o700 # Read, write, execute for owner
rx_group = 0o050 # Read, execute for group
rx_other = 0o005 # Read, execute for others
permissions = rwx_owner | rx_group | rx_other
print(oct(permissions)) # 0o755
Python's Unlimited Integer Size
Unlike many programming languages, Python integers have no maximum size. They can be as large as your memory allows:
| Big Numbers | |
|---|---|
No Integer Overflow
In languages like C or Java, integers have fixed sizes (32-bit, 64-bit) and can overflow. Python handles this automatically by switching to arbitrary-precision arithmetic. You'll never see an integer overflow in Python โ just potentially slow calculations with very large numbers.
Converting To and From Integers
int() Truncates, Not Rounds
int() always truncates toward zero, which is different from round() or floor division:
Key Takeaways
| Concept | What to Remember |
|---|---|
| Division types | / (float), // (floor), % (modulo) |
| True division | Always returns a float, even for 10 / 2 |
| Floor division | Rounds toward negative infinity, not zero |
| Modulo | Great for even/odd, cycling, divisibility |
| Precedence | ** โ * / // % โ + - (use parentheses!) |
| Number bases | 0b (binary), 0o (octal), 0x (hex) |
| No overflow | Python integers can be arbitrarily large |
| int() truncates | Toward zero, not rounding |