python中的math_Python math.perm()用法及代码示例

数学模块Python中的Math库包含许多数学运算,可以使用该模块轻松执行。math.perm()Python中的method方法用于获取从n个项中选择k个项(不重复且有序)的方法数量。评估为n! /(n-k)!当k <= n时,当k> n时为0。此方法是Python版本3.8中的新增功能。

用法: math.perm(n, k = None)

参数:

n:非负整数

k:一个非负整数。如果未指定k,则默认为None

返回:一个整数值,表示从n个项目中选择k个项目(不重复且有序)的方式。如果k为none,则方法返回n!。

代码1:用于math.perm()方法

# Python Program to explain math.perm() method

# Importing math module

import math

n = 10

k = 2

# Get the number of ways to choose

# k items from n items without

# repetition and with order

nPk = math.perm(n, k)

print(nPk)

n = 5

k = 3

# Get the number of ways to choose

# k items from n items without

# repetition and with order

nPk = math.perm(n, k)

print(nPk)

输出:

90

60

代码2:当k> n时

# Python Program to explain math.perm() method

# Importing math module

import math

# When k > n

# math.perm(n, k) returns 0.

n = 3

k = 5

# Get the number of ways to choose

# k items from n items without

# repetition and with order

nPk = math.perm(n, k)

print(nPk)

输出:

0

代码3:如果未指定k

# Python Program to explain math.perm() method

# Importing math module

import math

# When k is not specified

# It defaults to n and

# math.perm(n, k) returns n ! n = 5

nPk = math.perm(n)

print(nPk)

输出:

120

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值