+4 votes
in Programming Languages by (14.6k points)
edited by
What is the Pythonic way to compute the number of ways to choose k items from n items without repetition and with order?

1 Answer

0 votes
by (25.6k points)

You can use the perm() function of Python module 'math'.

math.perm(n, k=None)

  • Return the number of ways to choose k items from n items without repetition and with order.
  • Evaluates to n! / (n - k)! when k <= n and evaluates to zero when k > n.
  • If k is not specified or is None, then k defaults to n and the function returns n!.
E.g.
>>> import math
>>> math.perm(7,3)
210
>>> math.perm(3,1)
3
>>> math.perm(3,4)
0
>>> math.perm(5,3)
60
 
...