<=
"At most" — ordering with the boundary included. Also subset testing for sets.
Common call
if x <= limit:
Returns
bool
Replaces
on sets, <= means "is subset of"
Watch out
range idiom: lo <= x < hi (inclusive lo, exclusive hi)
aa — Left operand.type: comparable · required <= bb — Right operand.type: comparable · required
→ bool
Demo
Live evaluation
Try:
Inputs
afloatleft
bfloatright
Output
3 <= 5
True
Like < but True on equality too — the half-open range idiom lo <= x < hi is built from exactly this pair.
Operands
| Name | Type | Required | Description |
|---|---|---|---|
| a | comparable | yes | Left operand. |
| b | comparable | yes | Right operand. |
Return value
bool — True when a orders before b or equals it.
Common patterns
The half-open range idiom
Inclusive start, exclusive end — matches slicing and range().
if 0 <= i < len(items): ...
Subset testing
On sets, <= is the subset relation.
required <= provided # all required items present?
Examples
1. Less
3 <= 5
Returns
True2. Equal
5 <= 5
Returns
True3. Subset (sets)
{1, 2} <= {1, 2, 3}
Returns
TruePitfalls
1. Both-inclusive ranges drift from Python idiom
lo <= x <= hi is valid but slicing/range are half-open — mixing conventions breeds off-by-ones.
Convention clash
if 0 <= i <= len(items) - 1:
works, but noisy
Idiomatic
if 0 <= i < len(items):
matches range()/slices
When to use
Use it
- Inclusive bounds ("at most")
- Half-open range checks with <
- Subset tests on sets
Reach for something else
- Strict bound wanted → <
- Clamping → min()/max()
Notes
Complexity
O(1) numbers; O(n) sequences/sets
Return
bool
CPython impl
Objects/object.c :: PyObject_RichCompare (Py_LE) → __le__
Memory
No allocation
Thread-safe
Yes for immutable operands
FAQ
a <= b is "a is a subset of b" (allowing equality); a < b is a PROPER subset. Same symbols, set semantics.
History
1.0
Core operator from the beginning.