+4 votes
in Programming Languages by (74.2k 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

+1 vote
by (349k points)
selected by
 
Best answer

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
 

...