This question is old but as it comes up high on search results I will point out that scipy has two functions for computing the binomial coefficients:

  1. scipy.special.binom()
  2. scipy.special.comb()

    import scipy.special
    
    # the two give the same results 
    scipy.special.binom(10, 5)
    # 252.0
    scipy.special.comb(10, 5)
    # 252.0
    
    scipy.special.binom(300, 150)
    # 9.375970277281882e+88
    scipy.special.comb(300, 150)
    # 9.375970277281882e+88
    
    # ...but with `exact == True`
    scipy.special.comb(10, 5, exact=True)
    # 252
    scipy.special.comb(300, 150, exact=True)
    # 393759702772827452793193754439064084879232655700081358920472352712975170021839591675861424
    

Note that scipy.special.comb(exact=True) uses Python integers, and therefore it can handle arbitrarily large results!

Speed-wise, the three versions give somewhat different results:

num = 300

%timeit [[scipy.special.binom(n, k) for k in range(n + 1)] for n in range(num)]
# 52.9 ms ± 107 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit [[scipy.special.comb(n, k) for k in range(n + 1)] for n in range(num)]
# 183 ms ± 814 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)each)

%timeit [[scipy.special.comb(n, k, exact=True) for k in range(n + 1)] for n in range(num)]
# 180 ms ± 649 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

(and for n = 300, the binomial coefficients are too large to be represented correctly using float64 numbers, as shown above).

Answer from Shaun Coutts on Stack Overflow
Top answer
1 of 16
167

This question is old but as it comes up high on search results I will point out that scipy has two functions for computing the binomial coefficients:

  1. scipy.special.binom()
  2. scipy.special.comb()

    import scipy.special
    
    # the two give the same results 
    scipy.special.binom(10, 5)
    # 252.0
    scipy.special.comb(10, 5)
    # 252.0
    
    scipy.special.binom(300, 150)
    # 9.375970277281882e+88
    scipy.special.comb(300, 150)
    # 9.375970277281882e+88
    
    # ...but with `exact == True`
    scipy.special.comb(10, 5, exact=True)
    # 252
    scipy.special.comb(300, 150, exact=True)
    # 393759702772827452793193754439064084879232655700081358920472352712975170021839591675861424
    

Note that scipy.special.comb(exact=True) uses Python integers, and therefore it can handle arbitrarily large results!

Speed-wise, the three versions give somewhat different results:

num = 300

%timeit [[scipy.special.binom(n, k) for k in range(n + 1)] for n in range(num)]
# 52.9 ms ± 107 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit [[scipy.special.comb(n, k) for k in range(n + 1)] for n in range(num)]
# 183 ms ± 814 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)each)

%timeit [[scipy.special.comb(n, k, exact=True) for k in range(n + 1)] for n in range(num)]
# 180 ms ± 649 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

(and for n = 300, the binomial coefficients are too large to be represented correctly using float64 numbers, as shown above).

2 of 16
143

Note that starting Python 3.8, the standard library provides the math.comb function to compute the binomial coefficient:

math.comb(n, k)

which is the number of ways to choose k items from n items without repetition
n! / (k! (n - k)!):

import math
math.comb(10, 5)  # 252
math.comb(10, 10) # 1
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
1 week ago - Also called the binomial coefficient because it is equivalent to the coefficient of k-th term in polynomial expansion of (1 + x)ⁿ.
🌐
Medium
medium.com › @whyamit404 › understanding-binomial-coefficient-in-numpy-90fd505d2121
Understanding Binomial Coefficient in NumPy | by whyamit404 | Medium
March 6, 2025 - The binomial coefficient helps you find out how many unique pairs of books you can choose. In this case, it’s C(5, 2) = 10. Now, you might be wondering, “Can’t I just do this with basic Python?”
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › binomial-coefficient-dp-9
Binomial Coefficient - GeeksforGeeks
July 23, 2025 - # Python implementation to find # Binomial Coefficient using memoization def getnCk(n, k, memo): # k cannot be greater than n so we return 0 here if k > n: return 0 # base condition when k and n are equal or k = 0 if k == 0 or k == n: return 1 # Check if pair n and k is already # calculated then return it from here if memo[n][k] != -1: return memo[n][k] # Recursive add the value and store to memo table memo[n][k] = getnCk(n - 1, k - 1, memo) + \ getnCk(n - 1, k, memo) return memo[n][k] def binomialCoeff(n, k): # Create table for memoization memo = [[-1 for _ in range(k + 1)] for _ in range(n + 1)] return getnCk(n, k, memo) n, k = 5, 2 print(binomialCoeff(n, k))
🌐
AskPython
askpython.com › home › calculate the binomial coefficient in 5 ways
Calculate the Binomial Coefficient in 5 Ways - AskPython
April 10, 2025 - A binomial coefficient is defined as positive integers that are the numerical coefficients of the binomial theorem. Read on to find out about the statistics module in Python
🌐
30 Seconds of Code
30secondsofcode.org › home › python › binomial coefficient
Python - Binomial coefficient - 30 seconds of code
May 9, 2024 - The binomial coefficient is the number of ways to choose k items from n items without repetition and without order. In Python, you can calculate the binomial coefficient using the math.comb() function.s
Find elsewhere
🌐
GitHub
gist.github.com › ebe734dcc6f4ff450abf
A fast way to calculate binomial coefficients in python ...
def binomial(n, k): if not 0 <= k <= n: return 0 b = 1 for t in range(min(k, n-k)): b *= n b /= t+1 n -= 1 return int(b)
🌐
Rosetta Code
rosettacode.org › wiki › Evaluate_binomial_coefficients
Evaluate binomial coefficients - Rosetta Code
January 24, 2026 - begin % calculates n!/k! % integer procedure factorialOverFactorial( integer value n, k ) ; if k > n then 0 else if k = n then 1 else % k < n % begin integer f; f := 1; for i := k + 1 until n do f := f * i; f end factorialOverFactorial ; % calculates n! % integer procedure factorial( integer value n ) ; begin integer f; f := 1; for i := 2 until n do f := f * i; f end factorial ; % calculates the binomial coefficient of (n k) % % uses the factorialOverFactorial procedure for a slight optimisation % integer procedure binomialCoefficient( integer value n, k ) ; if ( n - k ) > k then factorialOverFactorial( n, n - k ) div factorial( k ) else factorialOverFactorial( n, k ) div factorial( n - k ); % display the binomial coefficient of (5 3) % write( binomialCoefficient( 5, 3 ) ) end.
🌐
Delft Stack
delftstack.com › home › howto › python › calculate binomial coefficient python
Binomial Coefficient in Python | Delft Stack
March 4, 2025 - The above code imports the math module and uses the comb() function to calculate the binomial coefficient. Specifically, it calculates C(10, 5), which represents the number of ways to choose 5 objects from a set of 10. The result is then printed to the console. This code demonstrates how to utilize Python’s math.comb() function to easily compute binomial coefficients, providing exact integer values without additional libraries or packages.
🌐
Python
bugs.python.org › issue35431
Issue 35431: Add a function for computing binomial coefficients to the math module - Python tracker
December 6, 2018 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/79612
🌐
Final Round AI
finalroundai.com › articles › binomial-coefficient
Binomial Coefficient (With Visualization and Codes)
October 3, 2025 - Learn how to solve the binomial coefficient problem with optimized algorithms. Includes Python, Java, and C++ code examples with time complexity analysis.