Powerball in Python
December 26, 2025 at 10:48 AM by Dr. Drang
Yesterday morning, before Christmas got going, I read about the big Powerball win Wednesday night. I figured that was as good an excuse as any to practice my new lotto math skills and wrote a short Python script to work out the game’s odds.
Powerball’s rules and odds differ from the UK Lotto’s, but they’re similar. In Powerball, there are 69 numbered white balls and 26 numbered red balls. Players select five numbers from 1 through 69 and one number from 1 through 26. In the drawing, five white balls and one red ball (the Powerball) are drawn. Your winnings depend on how many white balls you match and whether you matched the Powerball.
The script I wrote, powerball.py, prints the odds for every type of win. Here’s the output:
⚪⚪⚪⚪⚪ 🔴 1 in 292,201,338.00
⚪⚪⚪⚪⚪ 1 in 11,688,053.52
⚪⚪⚪⚪ 🔴 1 in 913,129.18
⚪⚪⚪⚪ 1 in 36,525.17
⚪⚪⚪ 🔴 1 in 14,494.11
⚪⚪⚪ 1 in 579.76
⚪⚪ 🔴 1 in 701.33
⚪ 🔴 1 in 91.98
🔴 1 in 38.32
The alignment may not look great in your browser. I fiddled with the CSS to get it looking OK in Safari on my computer, but that doesn’t mean it’ll work for you. I suppose I could change the script to have it output an HTML table, but the idea was to have the columns align well in Terminal, which they do:

Again, this is on my computer with my Terminal settings, but the alignment looks good in all the fixed-width fonts I have.
Of course, the main point of the script was the calculation of odds, not formatting. Here’s powerball.py:
python:
1: #!/usr/bin/env python3
2:
3: from math import comb
4:
5: def powerball(matches, power):
6: 'Odds (n for "1 in n") of the Powerball game'
7:
8: if power:
9: return 26 * comb(69, 5)/(comb(5, matches)*comb(69 - 5, 5 - matches))
10: else:
11: return 26/(26 - 1) * comb(69, 5)/(comb(5, matches)*comb(69 - 5, 5 - matches))
12:
13: def mstring(matches):
14: 'Regular match output string'
15:
16: return f'{" "*(5-matches)}{"⚪"*matches}'
17:
18: def pstring(power):
19: 'Powerball output string'
20:
21: if power:
22: return f'🔴'
23: else:
24: return f' '
25:
26: for m in range(5, -1, -1):
27: for p in (True, False):
28: if p or m > 2:
29: print(f'{mstring(m)} {pstring(p)} 1 in {powerball(m, p):,.2f}')
The powerball function takes two arguments: the number of white ball matches and a boolean for whether the Powerball was matched. It returns the inverse of the probability of getting the given results, which is
when the Powerball is matched and
when it isn’t. These are inverses of probabilities because the lottery presents odds in “1 in n” form.
The mstring and pstring functions return the strings with emoji circles for the number of white and red balls that are matched. The circles take up two regular monospace character widths.
The nested loop in Lines 26–29 prints out the results. The if statement prevents the printing of certain combinations (two white ball matches without the Powerball match, for example) because those combinations don’t pay off. The odds are printed to two decimal places because that’s how they’re presented on the Powerball site.