🧠 What is a Sequence in Programming?

Full-Stack Developer & Tech Writer specializing in Python (Django, FastAPI, Flask) and JavaScript (React, Next.js, Node.js). I build fast, scalable web apps and share practical insights on backend architecture, frontend performance, APIs, and Web3 integration. Available for freelance and remote roles.
Ever wondered why arrays, lists, and strings all start counting at 0 instead of 1?
Or why Python’s [::-1] magically reverses a list?
Today, we’ll deep dive into sequences one of the most fundamental programming concepts and make it fun with real-life examples, code snippets, and even a custom sequence class you can show off in your next team meeting. 🚀
🎯 What is a Sequence?
At its core, a sequence is just an ordered collection of items that:
Stores elements in a fixed order
Lets you access them using an index
Supports iteration (looping through it)
Think of it like a train 🚆 each compartment (element) is in order, and you can visit them one by one.
🛠 Built-in Sequences in Python
List →
[1, 2, 3](mutable can be changed)Tuple →
(1, 2, 3)(immutable can’t be changed)String →
"hello"(sequence of characters)Range →
range(5)(sequence of numbers)
🌍 In Other Languages
JavaScript: Array, String
Java: Array, ArrayList
C/C++: Arrays (closest to the machine)
🔢 Why Do We Start at Index 0?
This is one of the first things that confuses new developers but there’s a really good reason for it.
🧠 The Low-Level Explanation
In C (and other lower-level languages), an array name is basically a pointer it stores the memory address of the first element.
When you do array[0], you’re really saying:
*(array + 0) // Value at array + offset 0
This is super efficient because there’s no extra math needed.
If indexing started at 1, every access would need to subtract 1 internally (i - 1).
Computers hate doing extra work (and so do developers 😉).
⚡ Why 0-Based Indexing is Awesome
✅ Fast no extra math
✅ Pointer-Friendly matches how memory works
✅ Makes Slicing Easier last element is len(seq) - 1
✅ Consistent Across Data Structures stacks, queues, heaps all benefit
🎯 Real-Life Analogy
Imagine standing at the entrance of a movie theater.
Seat numbering starting from 0 means the first seat is right where you are no need to walk one step before you start counting.
Computers like things simple and this is as simple as it gets.
✂️ Python Slicing: Your Superpower
Slicing is one of Python’s coolest tricks.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # [20, 30, 40] (start=1, stop=4)
print(numbers[:3]) # [10, 20, 30] (start defaults to 0)
print(numbers[::2]) # [10, 30, 50] (step=2)
print(numbers[::-1]) # [50, 40, 30, 20, 10] # Reversed!
🧩 How It Works
Syntax:
sequence[start:stop:step]
start→ where to beginstop→ where to end (but not include)step→ how much to skip each time
This means slicing is not just copying it’s a filtering machine.
📋 Copying Sequences the Right Way
Sometimes you want to make a copy of a list but avoid messing with the original.
original = [1, 2, 3]
copy1 = original[:] # using slice
copy2 = list(original) # using constructor
copy3 = original.copy() # list method
⚠️ Beware: These are shallow copies → changes to nested lists still affect both copies.
For deep copies:
import copy
nested = [[1, 2], [3, 4]]
deep_copy = copy.deepcopy(nested)
nested[0][0] = 999
print(deep_copy[0][0]) # Still 1 ✅
🎨 Build Your Own Custom Sequence
Why stop at lists? You can build your own sequence class that behaves like a Python list.
class RepeatedSequence:
def __init__(self, value, count):
self.value = value
self.count = count
def __len__(self):
return self.count
def __getitem__(self, index):
if isinstance(index, slice):
start, stop, step = index.indices(self.count)
return [self.value for _ in range(start, stop, step)]
if index < 0:
index += self.count
if 0 <= index < self.count:
return self.value
raise IndexError("Index out of range")
def __repr__(self):
return f"{[self.value] * self.count}"
🎮 Demo Time
seq = RepeatedSequence("🍕", 5)
print(seq[2]) # 🍕
print(seq[:3]) # ['🍕', '🍕', '🍕']
print(len(seq)) # 5
🎁 Bonus: Use collections.abc.Sequence
If you want all the fancy stuff like __contains__, __iter__, .index(), and .count() for free, just inherit from collections.abc.Sequence:
from collections.abc import Sequence
class MySeq(Sequence):
def __init__(self, data):
self.data = data
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return len(self.data)
Boom. Instant professional sequence. 💥
🏁 Key Takeaways
✅ Sequences are ordered collections that you can index and iterate
✅ Indexing starts at 0 because memory and math like it simple
✅ Slicing is powerful it’s like a tiny DSL for extracting data
✅ Copy with care know the difference between shallow and deep copy
✅ Build your own sequences because you can 😎
✨ Final Thoughts
Understanding sequences is a superpower for every developer.
Once you get why indexing starts at 0 and how slicing works, you start to write more elegant, bug-free code.
Plus, building your own sequence class is just… fun.
Go ahead try it out and flex on your team in your next stand-up. 💪



