Python3 求1到100和的几种方法
2020-02-02 18:21
399 查看
##使用for循环 total = 0 for i in range(101): total += i print(total)
##使用while循环 total = 0 i = 1 while i <= 100: total += i i += 1 print(total)
##使用内建函数 print(sum(range(1,101)))
##使用reduce函数 from functools import reduce print(reduce(lambda a,b:a+b,range(1,101)))
转载于:https://my.oschina.net/u/3875850/blog/3021933
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- python输出1到100之和的几种方法
- Python实现从url中提取域名的几种方法
- 详解使用Python下载文件的几种方法
- 用Python解析XML的几种常见方法的介绍
- Python几种创建list的方法的效率对比
- Windows下用Python你会几种copy文件的方法?
- Python OOP类中的几种函数或方法总结
- python和shell变量互相传递的几种方法
- Python代码调试的几种方法总结
- Python模块安装的几种方法
- python实现线性搜索的几种方法
- Python调用shell命令的几种方法(在新进程中执行shell命令)
- Windows下用Python你会几种copy文件的方法?
- Python中字典创建的几种方法及适用场景
- python中判断数字位数的几种方法
- python导入数据的几种方法
- Java代码调用python的几种方法
- python中实现字符串反转的几种方法
- 对python-3-print重定向输出的几种方法总结
- python 字符串“修改”的几种方法