python实现整数序列求和
作者:张靖力 时间:2023-12-14 06:53:10
python整数序列求和
整数序列求和,用户输入一个正整数N,计算从1到N(包含1和N)相加之后的结果。
代码如下:
n=input("请输入整数N:")
sum=0
for i in range(int(n)):
sum+=i+1
print("1到N求和结果:",sum)
运行结果:
python常用的序列求和方法
输入正整数n,输出1+2+3+……+n-1+n的和
函数求和
n = int(input('请输入想求和的数字:'))
print(sum(range(1,n+1)))
高阶函数reduce
from functools import reduce
n = int(input('请输入想求和的数字:'))
print(reduce(lambda a,b:a+b,list(range(1,n+1))))
普通求和
n = int(input('请输入想求和的数字:'))
sum = 0
for i in range(1,n+1):
sum = sum + i
print(sum)
递归求和
a = int(input('请输入想求和的数字:'))
def fun(n):
if n == 1:
return n
n = n + fun(n-1)
return n
print(fun(a))
来源:https://blog.csdn.net/qq_40253803/article/details/105596997
标签:python,整数,序列,求和
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
sqlserver服务器验证改为混合验证模式步骤
2024-01-20 18:02:28
![](https://img.aspxhome.com/file/2023/7/110957_0s.jpg)
Pytorch之finetune使用详解
2021-08-31 20:41:44
Python实现爬取亚马逊数据并打印出Excel文件操作示例
2021-09-10 05:10:36
![](https://img.aspxhome.com/file/2023/3/105203_0s.png)
python机器学习使数据更鲜活的可视化工具Pandas_Alive
2022-09-26 04:57:12
![](https://img.aspxhome.com/file/2023/8/112968_0s.png)
微信支付、支付宝支付等常用第三方支付通道接口手续费对比
2022-01-29 12:48:14
公用样式模板的设计制作
2009-09-13 21:27:00
![](https://img.aspxhome.com/file/UploadPic/20099/13/sys1-12s.jpg)
Python基于OpenCV实现人脸检测并保存
2023-10-07 00:43:37
asp我对后台安全的一些做法
2011-09-01 19:22:09
教你如何在WordPress发布文章时自定义文章作者名称
2023-11-12 07:47:11
![](https://img.aspxhome.com/file/2023/9/132249_0s.jpg)
Matplotlib中%matplotlib inline如何使用
2021-11-22 15:17:41
![](https://img.aspxhome.com/file/2023/4/89314_0s.png)
MySQL中字符串索引对update的影响分析
2024-01-20 19:00:10
pandas数据分组groupby()和统计函数agg()的使用
2023-09-07 22:25:46
![](https://img.aspxhome.com/file/2023/2/102332_0s.png)
Windows11下MySQL 8.0.29 安装配置方法图文教程
2024-01-24 09:20:40
![](https://img.aspxhome.com/file/2023/7/117807_0s.jpg)
python 多进程队列数据处理详解
2022-04-10 23:49:44
ASP实例:使用ASP生成图片彩色校验码
2009-01-20 16:27:00
Python 用三行代码提取PDF表格数据
2023-10-08 18:54:57
![](https://img.aspxhome.com/file/2023/9/99459_0s.jpg)
通过自学python能找到工作吗
2021-07-24 04:26:33
详解Python列表赋值复制深拷贝及5种浅拷贝
2022-07-16 16:22:41
Python中pillow知识点学习
2023-11-26 05:27:01
Python编程通过懒属性提升性能
2022-04-01 09:48:43