python 中阶乘怎么打,如何在Python 中计算N的阶乘
如何在Python 中计算N的阶乘
发布时间:2021-03-12 17:10:49
来源:亿速云
阅读:54
作者:Leah
本篇文章为大家展示了如何在Python 中计算N的阶乘,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
1)使用循环计算阶乘def frac(n):
r = 1
if n<=1:
if n==0 or n==1:
return 1
else:
print('n 不能小于0')
else:
for i in range(1, n+1):
r *= i
return r
print(frac(5))
print(frac(6))
print(frac(7))120
720
5040
2)使用递归计算阶乘def frac(n):
if n<=1:
if n==0 or n==1:
return 1
else:
print('n 不能小于0')
else:
return n * frac(n-1)
print(frac(5))
print(frac(6))
print(frac(7))120
720
5040
3)调用reduce函数计算阶乘
说明:Python 在 functools 模块提供了 reduce() 函数,该函数使用指定函数对序列对象进行累计。
查看函数信息:import functools
print(help(functools.reduce))Help on built-in function reduce in module _functools:
reduce(...)
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
import functools
def fn(x, y):
return x*y
def frac(n):
if n<=1:
if n==0 or n==1:
return 1
else:
print('n 不能小于0')
else:
return functools.reduce(fn, range(1, n+1))
print(frac(5))
print(frac(6))
print(frac(7))120
720
5040# 使用 lambda 简写
import functools
def frac(n):
if n<=1:
if n==0 or n==1:
return 1
else:
print('n 不能小于0')
else:
return functools.reduce(lambda x, y: x*y, range(1, n+1))
print(frac(5))
print(frac(6))
print(frac(7))120
720
5040
补充:python求n的阶乘并输出_python求n的阶乘
阶乘是基斯顿·卡曼(Christian Kramp,1760~1826)于1808年发明的运算符号,是数学术语。
一个正整数的阶乘(factorial)是所有小于及等于该数的正整数的积,并且0的阶乘为1。自然数n的阶乘写作n!。
下面我们来看一下使用Python计算n的阶乘的方法:
第一种:利用functools工具处理import functoolsresult = (lambda k: functools.reduce(int.__mul__, range(1, k + 1), 1))(5)
print(result)```
第二种:普通的循环x = 1y = int(input("请输入要计算的数:"))
for i in range(1, y + 1):
x = x * i
print(x)
第三种:利用递归的方式def func(n):if n == 0 or n == 1:
return 1
else:
return (n * func(n - 1))
a = func(5)
print(a)
上述内容就是如何在Python 中计算N的阶乘,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
python 中阶乘怎么打,如何在Python 中计算N的阶乘相关推荐
- Python数据分析小技巧:如何在Pandas中实现数据透视表?
Python数据分析小技巧:如何在Pandas中实现数据透视表? 数据透视表是数据分析中非常有用的工具,可以帮助我们快速了解数据的结构.关联和趋势.在Pandas中,我们可以使用pivot_table ...
- python中range 10 0_如何在python中使用range方法
如何在python中使用range方法 发布时间:2021-01-05 16:55:23 来源:亿速云 阅读:94 作者:Leah 如何在python中使用range方法?很多新手对此不是很清楚,为了 ...
- python 指定证书验证_如何在python中验证SSL证书?
我需要验证我的自定义CA签署了证书.使用OpenSSL命令行实用程序很容易做到: # Custom CA file: ca-cert.pem # Cert signed by above CA: bo ...
- python队列怎么用_如何在Python中使用多处理队列? - python
我很难理解多处理队列如何在python上工作以及如何实现它.假设我有两个python模块,它们从共享文件中访问数据,我们将这两个模块称为writer和Reader.我的计划是让读取器和写入器都将请求放 ...
- python字符串筛选输出_如何在Python中过滤字符串列表
Python使用列表数据类型在顺序索引中存储多个数据.它的工作方式类似于其他编程语言的数字数组.filter()方法是Python的一种非常有用的方法.可以使用filter()方法从Python中的任 ...
- python隐藏启动台_如何在Python中启动后台进程?
如何在Python中启动后台进程? 我正在尝试将shell脚本移植到更易读的python版本. 原始shell脚本在后台使用"&"启动多个进程(实用程序,监视器等). 如何 ...
- python移动文件中某个内容_如何在Python中移动文件
如何在Python中移动文件 我查看了Python $ mv ...接口,但无法找到移动文件的方法. 我如何在Python中执行相当于$ mv ...的操作? >>> source_ ...
- python读取ansi编码文件,如何在Python中同时读取ANSI和Unicode txt文件?
我是python新手,遇到了一个奇怪的问题: 当一个目录中有50个txt文件时,我希望读取每个.txt文件并将其内容保存在一个唯一的变量中,例如:**file = open(fcf[i], 'r') ...
- python绘图背景透明_如何在 Matplotlib 中更改绘图背景
介绍Matplotlib是Python中使用最广泛的数据可视化库之一.无论是简单还是复杂的可视化项目,它都是大多数人的首选库.在本教程中,我们将研究如何在Matplotlib中更改绘图的背景.导入数据 ...
最新文章
- java排序——桶排序
- 两个获取http页面的c#函数
- 学python第二天_学习Python的第二天
- [react] 在React怎么使用Context?
- PL/SQL Developer中文版下载以及使用图解(绿色版)
- 富编译器汇总及二次开发Demo
- 如何有效的使用 for循环和Iterator遍历
- 2020年的前端该怎么学?不吹不黑
- Nginx安装使用及与tomcat实现负载均衡
- 如何解决: ModuleNotFoundError: No module named ‘object_detection‘
- Java 异常Exception e中e的getMessage()和toString()以及 e.printStackTrace();方法的区别
- 本文主要介绍如何将音乐文件打包到程序中并设置为铃声
- svn和maven结合讨论
- linux软盘镜像下载,软盘镜像Image文件制作(WinImage Pro)
- [渝粤教育] 郑州工程技术学院 食品微生物学 参考 资料
- curl: (1) Protocol “‘http“ not supported or disabled in libcurl的解决方法
- 产品经理告诉你什么是PMF?什么是MVP?
- android studio卸载找不到uninstall
- 百谷歌---学习工作好东西啊!!
- 知识图谱是什么,知识图谱有什么特点?