python sum

Python sum() function is used to get the sum of numbers of an iterable.

Python sum()函数用于获取可迭代数的总和。

Python sum() (Python sum())

Python sum() function syntax is:

Python sum()函数语法为:

sum(iterable[, start])

start is an optional number with default value of 0. If start is provided, then the sum of start and all the numbers in the iterable is returned.

start是一个可选数字,默认值为0。如果提供了start,则将返回start和可迭代对象中所有数字的总和。

Python sum()数字列表 (Python sum() list of numbers)

s = sum([1, 2, 3])
print(s)s = sum([1, 2, 3], 10)
print(s)

Output:

输出:

6
16

Note that sum() method doesn’t take keyword arguments, so if we write sum([1, 2, 3], start=10) then it will throw exception as TypeError: sum() takes no keyword arguments.

请注意,sum()方法不带关键字参数,因此,如果我们编写sum([1, 2, 3], start=10) ,它将抛出异常,因为TypeError: sum() takes no keyword arguments

Python整数序列的总和 (Python sum of a sequence of integers)

Since sum accepts iterable as argument, we can pass tuple, bytes of numbers too.

由于sum接受iterable作为参数,因此我们也可以传递tuple ,数字字节。

s = sum(bytes([1, 2]))
print(s)s = sum(bytearray([1, 2]), 10)
print(s)# sum of integers in different formats, tuple of numbers
s = sum((1, 0b11, 0o17, 0xFF))
print(s)s = sum((1, 0b11, 0o17, 0xFF), 0xF)
print(s)

Output:

输出:

3
13
274
289

Python的浮点数总和 (Python sum of floats)

s = sum([1.5, 2.5, 3])
print(s)

Output: 7.0

输出: 7.0

If you want to add floating point values with extended precision, you can use math.fsum() function.

如果要添加扩展精度的浮点值,则可以使用math.fsum()函数。

Python的复数和 (Python sum of complex numbers)

sum() function works with complex numbers too.

sum()函数也适用于复数。

s = sum([1 + 2j, 3 + 4j])
print(s)s = sum([1 + 2j, 3 + 4j], 2 + 2j)
print(s)s = sum([1 + 2j, 2, 1.5 - 2j])
print(s)

Output:

输出:

(4+6j)
(6+8j)
(4.5+0j)
GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23147/python-sum

python sum

python sum_Python sum()相关推荐

  1. Python中的numpy.sum()

    Python numpy sum() function is used to get the sum of array elements over a given axis. Python numpy ...

  2. Python sum() TypeError: ‘int‘ object is not callable xxxxxxxxx XXXXXXXXXX

    Python sum() TypeError: 'int' object is not callable xxxxxxxxx XXXXXXXXXX 代码中定义了 sum 变量,导致sum()方法异常.

  3. Python中的“ @”(@)符号有什么作用?

    我正在看一些使用@符号的Python代码,但我不知道它的作用. 我也不知道要搜索什么,因为搜索Python文档时会出现,或者当包含@符号时Google不会返回相关结果. #1楼 此代码段: def d ...

  4. sql avg和count_SQL sum(),count()和avg()函数

    sql avg和count Hey, readers! In this article we will be focusing on some of the extensively used aggr ...

  5. csh sum算总和_如何在R中使用sum()–在R中查找元素的总和

    csh sum算总和 Let's learn how to find the sum of the values with the help of the sum() in R. In this tu ...

  6. 如何使用Python numpy.where()方法

    In Python, we can use the numpy.where() function to select elements from a numpy array, based on a c ...

  7. Python字符串title()

    Python String title() Python字符串title() Python String title() function returns a title cased version ...

  8. Python字符串isdecimal()

    Python String isdecimal() function returns True if all the characters in the string are decimal char ...

  9. Python字符串isalnum()

    Python string isalnum() function returns True if it's made of alphanumeric characters only. A charac ...

最新文章

  1. Oracle RMAN CATALOG数据库的配置
  2. bootstrap table php,bootstrap table Tooltip
  3. 2招按摩轻松解除黑眼圈 - 生活至上,美容至尚!
  4. c语言冒泡排序(解析)
  5. 【qduoj - 1011】数组中出现最多的元素(巧妙方法 或 排序 或 map)
  6. failed to keep to the max pss of 66560
  7. php导入json文件_[php]导入超大json文件
  8. Java Web:使用jstl标签
  9. SEO思维的优化源于生活
  10. 基于Windows Azure 搭建基于SharePoint 2010 Intranet、Extranet、Internet (3): 配置负载均衡...
  11. 轻松调整,提升写作效率,让 Word 更好地为你所用
  12. Javadoc注释的用法
  13. linux上机考试题(Linux基础)
  14. 在计算机里的键盘叫什么名字,电脑键盘最长的一个键叫什么名字
  15. c# FlexCell自动换行
  16. 什么是客户旅程分析(customer journey mapping)
  17. 计算机制作贺卡教案,WORD电子贺卡制作的优秀教案_电子贺卡
  18. 怎样用计算机绘制幂函数图像,几何画板如何画幂函数的图像 绘制方法介绍
  19. 【论文阅读】Recurrent Chunking Mechanisms for Long-Text Machine Reading Comprehension
  20. Android 蓝牙连接,蓝牙配对,自动连接蓝牙

热门文章

  1. html5 video标签兼容性与自定义控件
  2. maven更新总结与tomcat发布方法总结
  3. [转载] python的面向对象和类与对象
  4. [转载] Spring的数据库操作---- Spring框架对JDBC的整合---- spring集成jdbc概述
  5. 终极版Servlet——我只能提示您路过别错过
  6. elementUI 日期选择控件少一天的问题解决方法
  7. 85. Maximal Rectangle 由1拼出的最大矩形
  8. Winfrom窗体无法关闭问题--检查是否存在重写
  9. Python与数据结构[4] - 散列表[1] - 分离链接法的 Python 实现
  10. @Controller深入详解