I’m considering proposing an optional inclusive parameter for the built-in range() function and would welcome feedback before drafting a formal PEP. This would be a change to the existing range() signature only—no new syntax or function.
Summary: When inclusive=True, the stop value would be included in the sequence. The parameter would be keyword-only and default to False, preserving current behavior.
Examples:
-
range(5, inclusive=True)→ 0, 1, 2, 3, 4, 5 -
range(2, 6, inclusive=True)→ 2, 3, 4, 5, 6 -
range(10, 0, -1, inclusive=True)→ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
Rationale: Inclusive ranges are common (e.g., “from 1 to 10” meaning 1 through 10). When teaching my nephew to code, he struggled with exclusive stop; a simple irange helper helped. As the Zen of Python says, “Explicit is better than implicit.” That experience made me think this could be useful more broadly. People currently use range(start, stop + 1); a built-in parameter would make that intent clear and reduce off-by-one mistakes.
Prior discussions: Inclusive range was discussed on python-ideas in 2010. The main proposal then was to change the default to inclusive (rejected as a breaking change). Xavier Morel suggested an inclusive=True flag as an alternative, saying it “might have a chance.” This proposal is that flag—keeping the default exclusive and adding an opt-in. Nick Coghlan’s arguments for half-open ranges (len formula, slicing) still apply to the default; they don’t argue against an optional parameter.
Why revisit now: The flag was dismissed in one sentence and never seriously debated. Fifteen years have passed; Python’s use in education has grown, and I’ve observed the option helping in at least one teaching case. I’m asking whether the community is open to reconsidering.
I’d welcome feedback on:
-
Whether this is worth pursuing
-
The design (keyword-only, default False)
-
Use cases where this helps or falls short
I’m new to the PEP process and would appreciate constructive feedback.