code: def choose(n, k):
      
      if 0 <= k <= n:
          ntok = 1
          ktok = 1
          for t in xrange(1, min(k, n - k) + 1):
              ntok *= n
              ktok *= t
              n -= 1
          return ntok // ktok
      else:
          return 0
docs: A fast way to calculate binomial coefficients by Andrew Dalke (contrib).