Seven-Segment Display¶
By the end of this lab you'll be able to:
- Represent each digit as a bitmask of which of 7 segments are "on"
- Draw horizontal and vertical line segments at computed positions
- Display a multi-digit number using a dictionary of segment patterns
A seven-segment display like those on digital clocks and calculators. Each digit is encoded as a pattern of 7 binary flags — one per segment. A drawing function reads the flags and draws the lit segments.
Welcome to the Seven-Segment Display!
Digital clocks use seven-segment displays — each digit is just 7 on/off switches!
This lab builds a dictionary-driven display renderer in Python.
Let's code it together!
How It Works¶
Each digit is stored as a tuple of 7 booleans: (top, top-left, top-right, middle, bot-left, bot-right, bottom). A draw_digit(n, x, y) function checks each flag and draws the corresponding line segment.
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
What Do You Think Will Happen?
Each digit is encoded as 7 flags — the code looks up the pattern for each character.
Will it look like a real digital display, or just scattered lines?
Make your guess — then click Run to find out!
Try It Now¶
A real-looking digital display — the segment patterns produce recognizable digits. Were you right?
How It Works¶
The segments dictionary maps each digit character to a 7-tuple. The positions:
- s[0]: top horizontal, s[1]: upper-left vertical, s[2]: upper-right vertical
- s[3]: middle horizontal, s[4]: lower-left vertical, s[5]: lower-right vertical, s[6]: bottom horizontal
Explanation Table¶
| Line | What it does |
|---|---|
segments = {'0': (1,1,1,0,1,1,1), ...} |
Dictionary: digit → 7 segment flags |
s = segments.get(ch, (0,)*7) |
Look up the pattern, default to all off |
if s[0]: seg_line(...) |
Draw only segments that are on |
start_x = -(len(number)*(w+10))/2 |
Center the display |
Learning Check¶
Your Turn — Display the Year You Were Born
Change number = "2026" to the year you were born.
Predict which digit shapes will appear — then run it to check!
Green digits 1-5 displayed in sequence — each digit's unique segment pattern is clearly visible.
Experiments¶
-
Draw a clock. Display
"12:30"by adding a colon character (two dots). You'll know it worked when a time display appears. -
Animate a counter. Use a loop to display 0 through 9 one at a time. You'll know it worked when the display cycles through digits.
-
Make the display larger. Change
w, h = 30, 50tow, h = 50, 80. You'll know it worked when much bigger digits appear. -
Add dim background segments. Draw all 7 segments in dark gray before drawing the active ones. You'll know it worked when the inactive segments appear as faint shadows.
Digital Numbers!
You built a working seven-segment display renderer!
This is exactly how ATMs, gas pumps, and alarm clocks display numbers.
Up next: Circular Name Badge — text arranged in a circle.