Home>Article>Backend Development> How to sum in python

How to sum in python

coldplay.xixi
coldplay.xixi Original
2021-03-05 15:27:52 21642browse

The method of summing in python: use sum syntax to sum [sum(iterable[, start])], where iterable is an iterable object and start is a parameter that specifies addition. If this is not set value, default is 0.

How to sum in python

The operating environment of this tutorial: Windows 7 system, python version 3.9, DELL G3 computer.

How to sum in python:

1. Sum usage syntax

sum(iterable[, start])
  • iterable -- Iterable objects, such as lists, tuples, and sets.

  • start -- Specify the parameters for addition. If this value is not set, it defaults to 0.

2. Examples of using sum

For example: the result of sum([1,2,3]) is 6

sum((2, 3, 4), 1) The result ofis 10, which means that 1 is added after summing the tuples.

sum([0,1 ,2,3,4], 2) The result ofis 12, which means adding 2 after summing the list.

How to sum in python

Related free learning recommendations :python video tutorial

The above is the detailed content of How to sum in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn