Finding exact $p$ value for the binomial test for a single proportion, using the table with probabilities under the binomial distribution

Assuming a table for a certain number of trials $n$, with a column per true probability $P$, and a row for each possible number of successes $X$
Two sided
$p$ value is the probability of finding the observed number of successes or a more extreme number, given that the null hypothesis is true.
Except for the case where $\pi_0$ (the population proportion of successes according to the null hypothesis) is $0.5$, the sampling distribution of the observed number of successes $X$ is not symmetric under the null hypothesis. Finding the two sided $p$ value for non-symmetric distributions is a bit complicated, and you probably don't need to be able to do this by hand.
Right sided
$p$ value is the probability of finding the observed number of successes or a larger number, given that the null hypothesis is true.
  1. Find the table for the appropriate number of trials $n$, which is equal to the sample size $N$
  2. Find the column with true probability $P = \pi_0$ (the value according to the null hypothesis)
  3. Use the table to find the probability that the number of successes is equal to your observed number of successes $X$, the probability that the number of successes is one more than your observed number of successes $X$, the probability that the number of successes is two more than your observed number of successes $X$, etc, up to and including an observed number of successes equal to the total number of trials $n$
  4. Sum all these probabilities. This is your right sided $p$ value
Example: suppose that your null hypothesis is that $\pi = 0.4$, your alternative hypothesis is that $\pi > 0.4$, the number of successes in your sample is $8$, and the number of failures in your sample is $2$. The total number of trials (the total sample size) is equal to $n = 2 + 8 = 10$. Then the right sided $p$ value is equal to $0.011 + 0.002 = 0.013$.

Exact p value binomial test right sided
Left sided
$p$ value is the probability of finding the observed number of successes or a smaller number, given that the null hypothesis is true.
  1. Find the table for the appropriate number of trials $n$, which is equal to the sample size $N$
  2. Find the column with true probability $P = \pi_0$ (the value according to the null hypothesis)
  3. Use the table to find the probability that the number of successes is 0, the probability that the number of successes is 1, etc, up to and including your observed number of successes $X$
  4. Sum all these probabilities. This is your left sided $p$ value
Example: suppose that your null hypothesis is that $\pi = 0.4$, your alternative hypothesis is that $\pi < 0.4$, the number of successes in your sample is $2$, and the number of failures in your sample is $8$. The total number of trials (the total sample size) is equal to $n = 2 + 8 = 10$. Then the left sided $p$ value is equal to $0.006 + 0.040 + 0.121 = 0.167$.

Exact p value binomial test left sided