Welcome back, curious minds! Today, we’re going to delve deeper into the world of number sequences with Python. Specifically, we’ll focus on generating custom, finite sequences tailored to suit various scenarios. So grab your text editor, and let’s begin!
Introducing the range() Function
When discussing finite sequences in Python, the range()
function immediately comes to mind. Its primary purpose is to simplify iteration over integer intervals. Here's a basic usage example:
for i in range(10):
print(i)
# Output: 0, 1, ..., 8, 9
You may have noticed that the output doesn’t include ‘10’. That’s because range()
stops before reaching the specified endpoint. If you wish to include it, pass an extra argument as follows:
for i in range(10, 14):
print(i)
# Output: 10, 11, 12, 13
We can even create step sequences using range()
:
for i in range(0, 20, 3):
print(i)
# Output: 0, 3, 6, ..., 15, 18