Python math.perm() Method

Python math 模块Python math Module

Python math.perm(x, i)The method returns the total number of ways to choose k items from n items without repetition and with order.

Note: The k parameter is optional. If we don't set k, this method will return n! (for example, math.perm(7) will return 5040).

Python Version: 3.8

Syntax

The syntax of the math.perm() method is as follows:

math.perm(n, k)

Parameter Description:

  • x-- Required, a positive number. If the value is not a number, a TypeError is returned.
  • k-- Optional, a positive number. If the value is not a number, a TypeError is returned.

Note: If k is greater than n, it returns 0.

Note: If n or k is negative, a ValueError occurs. If n or k is not an integer, a TypeError occurs.

Return Value

An integer int, returning the total number of ways to choose k items from n items without repetition and with order.

Example

The following example calculates the total number of ways to choose k items from n items:

Example

# Import the math package
import math

# Initialize n
n = 7

# Initialize k
k = 5

# Output the total number of ways to choose k items from n items
print (math.perm(n, k))

Output result:

2520

Python math 模块Python math Module

Other Extensions