Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.


Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons

LoginGet Started
← Back to Lessons
Edit on Github
Open in Colab

Binomial Probability with Python

Origin in Games

Many historians of probability note the emergence of a theory of probability as linked to humans attempts to understand games of chance. We begin with an early example called Senet that is at least as old as the year 3000 BC. Here, players moves are decided by dropping 4 paddles with two colored faces. The players are allowed to move based on the number of colored paddles that are facing up. Our goal is to explore the likely outcomes of the drops to begin to understand probability.


The table below begins to explore these possible combinations. We will look at further examples with more possible combinations but this will get us started.

sticks# ways to get 0 white1 white2 white3 white4 white5 white

Binomial Distribution

The example above is one example of a more general kind of distribution of events. We have 4 two-sided sticks, and we will treat these as 4 Bernoulli Trials. To determine the probability of an outcome, we must:

  • Count how many ways there are for this specific outcome to occur
  • Mutiply this by the probability of successes and failures

As a function, we would have inputs that determine the outcome (success or failure, red or white, heads or tails, etc.), number of successes and failures, and the probability of a success. Below we have the functional form:

f(k,n,p)=Pr(k;n,p)=Pr(X=k)=(nk)pk(1p)nkf(k, n, p) = \Pr(k;n,p)=\Pr(X=k)={\binom {n}{k}}p^{k}(1-p)^{n-k}

We unpack this as follows:

  • (nk)\binom {n}{k} counts the number of ways an outcome can occur. For example, (42)\binom{4}{2} could be interpreted as how many ways are there to get two white sticks from four total.
  • pkp^k is the probability of a success to the power of the number of successes. For example 122\frac{1}{2}^2 would be interpreted as the probability of getting a white stick twice.
  • (1p)k(1 - p)^k is the probability of a failure nkn-k times. For example (112)2(1 - \frac{1}{2})^2 can be interpreted as the probability of a black stick two out of four attempts.

Using the computer

In [ ]:
from scipy.special import comb
In [ ]:
#ways to pick 2 things from 4
comb(4, 2)
Out[ ]:
In [ ]:
#prob of success to the number of successes
Out[ ]:
In [ ]:
#prob of failures to number of failures
Out[ ]:
In [ ]:
#put it all together
n_choose_k = comb(4, 2)
p_to_the_k = (1/2)**2
one_minus_p_to_the_n_minus_k = (1/2)**2
#probability of getting 2 white sticks from four
Out[ ]:


Suppose we instead play the game of Sennet using 5 sticks.

  1. How many ways are there to get three white sticks?
  2. What is the probability that we will get three white sticks?
  3. Suppose we toss 10 coins. How many different outcomes contain exactly three heads?
  4. Suppose we toss 20 coins. How many different outcomes contain exactly three heads?
  5. Complete the next two rows of the table for sennet. Do you recognize a pattern?