Как найти сумму всех чисел массива питон

A simple way is to use the iter_tools permutation

# If you are given a list

numList = [1,2,3,4,5,6,7]

# and you are asked to find the number of three sums that add to a particular number

target = 10
# How you could come up with the answer?

from itertools import permutations

good_permutations = []

for p in permutations(numList, 3):
    if sum(p) == target:
        good_permutations.append(p)

print(good_permutations)

The result is:

[(1, 2, 7), (1, 3, 6), (1, 4, 5), (1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 1, 7), (2, 3, 
5), (2, 5, 3), (2, 7, 1), (3, 1, 6), (3, 2, 5), (3, 5, 2), (3, 6, 1), (4, 1, 5), (4, 
5, 1), (5, 1, 4), (5, 2, 3), (5, 3, 2), (5, 4, 1), (6, 1, 3), (6, 3, 1), (7, 1, 2), 
(7, 2, 1)]

Note that order matters — meaning 1, 2, 7 is also shown as 2, 1, 7 and 7, 1, 2. You can reduce this by using a set.

Задачи по функции sum() и решения к ним у нас в телеграм канале PythonTurbo

Давайте разберем, что такое функция sum() в Python и почему это питонический способ суммирования.

Встроенная функция sum() – это эффективный и питонический способ суммирования списка числовых значений. Сложение нескольких чисел является обычным промежуточным шагом во многих вычислениях, поэтому sum() – довольно удобный инструмент для программиста Python.

Еще с помощью sum() можно объединять списки и кортежи. Это интересный дополнительный вариант использования, полезный, когда вам нужно сгладить список списков.

Приведенная ниже информация поможет вам эффективно решать проблемы суммирования в вашем коде с помощью sum() или других альтернативных и специализированных инструментов.

Понимание проблемы суммирования

Суммирование числовых значений – довольно распространенная задача в программировании. Например, предположим, что у вас есть список чисел a = [1, 2, 3, 4, 5] и вы хотите сложить элементы и получить сумму. Используя стандартную арифметику, вы сделаете что-то вроде этого:

1 + 2 + 3 + 4 + 5 = 15

Что касается математики, это выражение довольно простое.

Можно выполнить этот конкретный расчет вручную, но представьте себе другие ситуации, в которых это может быть невозможно. Если у вас очень длинный список чисел, добавление вручную будет неэффективным и, скорее всего, вы допустите ошибку. А если вы даже не знаете, сколько элементов в списке? Наконец, представьте сценарий, в котором количество элементов, которые вам нужно добавить, изменяется динамически или вообще непредсказуемо.

В подобных ситуациях, независимо от того, есть ли у вас длинный или короткий список чисел, Python может быть весьма полезен для решения задач суммирования.

Использование цикла for

Если вы хотите суммировать числа, создав собственное решение с нуля, вы можете использовать цикл for:

numbers = [1, 2, 3, 4, 5]
total = 0
for number in numbers:
    total += number
print(total)
# 15

Здесь вы сначала инициализируете сумму и приравниваете её к 0. Эта переменная работает как аккумулятор, в котором вы сохраняете промежуточные результаты, пока не получите окончательный. Цикл перебирает числа и обновляет общее количество.

Цикл for можно заключить в функцию. Благодаря этому вы сможете повторно использовать код для разных списков:

def sum_numbers(numbers):
    total = 0
    for number in numbers:
        total += number
    return total

sum_numbers([1, 2, 3, 4, 5])
# 15
sum_numbers([])
# 0

В sum_numbers() вы берете итерируемый объект в качестве аргумента и возвращаете общую сумму значений элементов списка.

Прямо сейчас можете попробовать решить задачку «Напишите программу на Python для суммирования всех элементов в списке»

def sum_list(items):
    ваш код

print(sum_list([1, 2, -8])) #В выводе должно быть -5

Условие и решение есть в наших поста тут и тут

Использование рекурсии

Вы также можете использовать рекурсию вместо итерации. Рекурсия – это метод функционального программирования, при котором функция вызывается в пределах ее собственного определения. Другими словами, рекурсивная функция вызывает сама себя в цикле:

def sum_numbers(numbers):
    if len(numbers) == 0:
        return 0
    return numbers[0] + sum_numbers(numbers[1:])

sum_numbers([1, 2, 3, 4, 5])
# 15

Когда вы определяете рекурсивную функцию, вы рискуете попасть в бесконечный цикл. Чтобы предотвратить это, нужно определить как базовый случай, останавливающий рекурсию, так и рекурсивный случай для вызова функции и запуска неявного цикла.

В приведенном выше примере базовый случай подразумевает, что сумма списка нулевой длины равна 0. Рекурсивный случай подразумевает, что общая сумма – это первый элемент плюс сумма остальных элементов. Поскольку рекурсивный случай использует более короткую последовательность на каждой итерации, вы ожидаете столкнуться с базовым случаем, когда числа представляют собой список нулевой длины.

[python_ad_block]

Использование reduce()

Другой вариант суммирования списка чисел в Python – использовать reduce() из functools. Чтобы получить сумму списка чисел, вы можете передать либо оператор add, либо соответствующую лямбда-функцию в качестве первого аргумента функции reduce():

from functools import reduce
from operator import add

reduce(add, [1, 2, 3, 4, 5])
# 15

reduce(add, [])
# Traceback (most recent call last):
#     ...
# TypeError: reduce() of empty sequence with no initial value

reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
# 15

Вы можете вызвать reduce() с folding-функцией и итерируемым объектом в качестве аргументов. reduce() использует переданную функцию для обработки итерируемого объекта и вернет единственное кумулятивное значение.

В первом примере folding-функция – это add(), которая берет два числа и складывает их. Конечный результат – это сумма чисел во входном итерируемом объекте. Но если вы вызовете reduce() с пустым итерируемым объектом, получите TypeError.

Во втором примере folding-функция – это лямбда-функция, которая возвращает сложение двух чисел.

Поскольку суммирование является обычным явлением в программировании, писать новую функцию каждый раз, когда нам нужно сложить какие-нибудь числа, — бессмысленная работа. Кроме того, использование reduce() – далеко не самое удобочитаемое решение.

Python предоставляет специальную встроенную функцию для решения этой проблемы. Это функция sum(). Поскольку это встроенная функция, вы можете использовать ее в коде напрямую, ничего не импортируя.

В настоящее время функция sum() является предпочтительным способом для суммирования элементов:

sum([1, 2, 3, 4, 5])
# 15

sum([])
# 0

Здорово, не правда ли? Код читается как обычный текст и четко передает действие, которое вы выполняете. Использование sum() значительно упрощает код. Более того, эта функция не вызывает TypeError, если вы передали пустой список.

У sum() есть два аргумента:

  • iterable – обязательный аргумент, который может содержать любой итерируемый объект Python. Итерируемый объект обычно содержит числовые значения, но также может содержать списки или кортежи.
  • start – необязательный аргумент, который может содержать начальное значение. В конце суммирования элементов это значение добавляется к окончательному результату. По умолчанию равен 0.

Суммирование числовых значений

Основная цель sum() – предоставить питонический способ сложения числовых значений. До этого момента вы видели, как использовать функцию для суммирования целых чисел. Кроме того, вы можете использовать sum() с любыми другими числовыми типами Python, такими как float, complex, decimal.Decimal и fractions.Fraction.

Вот несколько примеров использования sum() со значениями разных числовых типов:

from decimal import Decimal
from fractions import Fraction

# Sum floating-point numbers
sum([10.2, 12.5, 11.8])
# 34.5
sum([10.2, 12.5, 11.8, float("inf")])
# inf
sum([10.2, 12.5, 11.8, float("nan")])
# nan

# Sum complex numbers
sum([3 + 2j, 5 + 6j])
# (8+8j)

# Sum Decimal numbers
sum([Decimal("10.2"), Decimal("12.5"), Decimal("11.8")])
# Decimal('34.5')

# Sum Fraction numbers
sum([Fraction(51, 5), Fraction(25, 2), Fraction(59, 5)])
# Fraction(69, 2)

Объединение последовательностей

Несмотря на то, что функция sum() в основном предназначена для работы с числовыми значениями, вы также можете использовать ее для объединения последовательностей, таких как списки и кортежи. Для этого вам нужно указать соответствующее значение для аргумента start:

num_lists = [[1, 2, 3], [4, 5, 6]]
sum(num_lists, start=[])
# [1, 2, 3, 4, 5, 6]

# Equivalent concatenation
[1, 2, 3] + [4, 5, 6]
# [1, 2, 3, 4, 5, 6]

num_tuples = ((1, 2, 3), (4, 5, 6))
sum(num_tuples, start=())
# (1, 2, 3, 4, 5, 6)

# Equivalent concatenation
(1, 2, 3) + (4, 5, 6)
# (1, 2, 3, 4, 5, 6)

Ключевым требованием для работы этих примеров является выбор подходящего значения для start. Например, если вы хотите объединить списки, то start должен быть равен [].

num_strs = ["123", "456"]
sum(num_strs, "0")
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: sum() can't sum strings [use ''.join(seq) instead]

Если же вы попытаетесь использовать sum() для объединения строк, вы получите ошибку TypeError. Она говорит нам о том, что для объединения строк в Python следует использовать str.join() .

Примеры использования sum() в Python

До сих пор мы говорили про основы работы с sum(). В этом разделе вы увидите еще несколько примеров того, когда и как использовать sum() в вашем коде. Из этих практических примеров вы узнаете, что эта встроенная функция очень удобна, когда вы выполняете вычисления, требующие на промежуточном этапе нахождения суммы ряда чисел.

Кроме того, мы разберем, как применять sum() при работе со списками и кортежами. Мы также рассмотрим особый пример, когда нужно объединить несколько списков.

Расчет среднего значения выборки

Один из практических вариантов применения sum() – использовать ее в качестве промежуточного вычисления перед дальнейшими вычислениями. Например, вам нужно вычислить среднее арифметическое для выборки числовых значений. Среднее арифметическое, также известное как среднее значение, представляет собой общую сумму значений, деленную на количество значений в выборке.

Если у вас есть выборка [2, 3, 4, 2, 3, 6, 4, 2] и вы хотите вычислить среднее арифметическое вручную, вы можете решить эту операцию так:

(2 + 3 + 4 + 2 + 3 + 6 + 4 + 2) / 8 = 3,25

Если вы хотите ускорить это с помощью Python, вы можете разбить решение на две части. Первая часть – вы складываете все числа – это задача для sum(). В следующей части, где вы делите на 8, используется количество чисел в вашей выборке. Чтобы определить свой делитель, вы можете использовать len():

data_points = [2, 3, 4, 2, 3, 6, 4, 2]
sum(data_points) / len(data_points)
# 3.25

Здесь sum() вычисляет общую сумму в нашей выборке. Затем мы используем len(), чтобы получить общее количество. Наконец, выполняем деление, чтобы вычислить среднее арифметическое значение выборки.

Нахождение скалярного произведения двух последовательностей

Другая проблема, которую мы можем решить с помощью sum(), – это нахождение скалярного произведения двух числовых последовательностей равной длины. Скалярное произведение – это алгебраическая сумма произведений каждой пары значений во входных последовательностях. Например, если у вас есть последовательности (1, 2, 3) и (4, 5, 6), вы можете вычислить их скалярное произведение вручную, используя сложение и умножение:

1 × 4 + 2 × 5 + 3 × 6 = 32

Чтобы извлечь последовательные пары значений, мы можем использовать zip(). Затем воспользуемся генератором для умножения каждой пары значений. Наконец, sum() поможет суммировать произведения:

x_vector = (1, 2, 3)
y_vector = (4, 5, 6)

sum(x * y for x, y in zip(x_vector, y_vector))
# 32

Объединение списков

Объединение списков – обычная задача в Python. Предположим, у вас есть список списков, и вам нужно объединить его в единый список, содержащий все элементы из исходных вложенных списков. Вы можете использовать любой из нескольких подходов к объединению списков в Python. Например, можно воспользоваться циклом for, как в следующем коде:

def flatten_list(a_list):
    flat = []
    for sublist in a_list:
        flat += sublist
    return flat

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]

flatten_list(matrix)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

Внутри flatten_list() цикл перебирает все вложенные списки, содержащиеся в a_list. Затем он объединяет их в один. В результате вы получаете единый список со всеми элементами из исходных вложенных списков.

Но можно ли использовать функцию sum() для объединения списков, как в примере выше? Да! Вот как:

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]

sum(matrix, [])
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

Это было быстро! Одна строка кода — и мы получили единый список. Однако использование sum() не кажется самым быстрым решением.

Важным недостатком любого решения, предполагающего конкатенацию, является то, что за кулисами каждый промежуточный шаг создает новый список. Это может быть довольно расточительным с точки зрения использования памяти.

Список, который в итоге возвращается, является самым последним созданным списком из всех, которые создавались на каждом этапе конкатенации. Использование генератора списка вместо этого гарантирует, что вы создадите и вернете только один список:

def flatten_list(a_list):
    return [item for sublist in a_list for item in sublist]

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]

flatten_list(matrix)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

Эта новая версия flatten_list() более эффективна и менее расточительна с точки зрения использования памяти. Однако вложенные генераторы могут быть сложными для чтения и понимания.

Использование .append(), вероятно, является наиболее читаемым и питоничным способом объединить списки:

def flatten_list(a_list):
    flat = []
    for sublist in a_list:
        for item in sublist:
            flat.append(item)
    return flat

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]

flatten_list(matrix)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

В этой версии функция выполняет итерацию по каждому подсписку в a_list. Внутри внутреннего цикла for она перебирает каждый элемент подсписка, чтобы заполнить новый список с помощью .append(). Как и в предыдущем случае, это решение создает только один список в процессе. Преимущество его в том, что оно хорошо читается.

Альтернативы sum()

Как вы уже поняли, функция sum() полезна для работы с числовыми значениями в целом. Однако, когда дело доходит до работы с числами с плавающей запятой, Python предоставляет альтернативный инструмент. В библиотеке math вы найдете функцию под названием fsum(), которая поможет вам улучшить общую точность вычислений.

Вам может понадобиться объединить или связать несколько итерируемых объектов, чтобы работать с ними как с одним. Для этого можно использовать модуль itertools().

Также у вас может возникнуть необходимость объединить строки. Для этого нельзя использовать sum(). Самая питоническая альтернатива – применить str.join().

Суммирование чисел с плавающей запятой: math.fsum()

Эта функция выполняет вычисления с плавающей запятой более тщательно, чем sum(), что повышает точность.

Согласно документации, fsum() «позволяет избежать потери точности, отслеживая несколько промежуточных частичных сумм». В документации приводится следующий пример:

from math import fsum

sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
# 0.9999999999999999

fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
# 1.0

Используя fsum(), вы получите более точный результат. Однако следует отметить, что fsum() не устраняет ошибку представления в арифметике с плавающей запятой. Следующий пример раскрывает это ограничение:

from math import fsum

sum([0.1, 0.2])
# 0.30000000000000004

fsum([0.1, 0.2])
# 0.30000000000000004

В этих примерах обе функции возвращают одинаковый результат. Это связано с невозможностью точного представления значений 0,1 и 0,2 в двоичной системе с плавающей запятой:

f"{0.1:.28f}"
# '0.1000000000000000055511151231'

f"{0.2:.28f}"
# '0.2000000000000000111022302463'

Однако, в отличие от sum(), fsum() поможет вам уменьшить неточность, когда вы складываете очень большие и очень маленькие числа вместе:

from math import fsum

sum([1e-16, 1, 1e16])
# 1e+16
fsum([1e-16, 1, 1e16])
# 1.0000000000000002e+16

sum([1, 1, 1e100, -1e100] * 10_000)
# 0.0
fsum([1, 1, 1e100, -1e100] * 10_000)
# 20000.0

Ух ты! Второй пример довольно неожиданный и полностью дискредитирует sum(). С помощью sum() в результате вы получите 0,0. Это довольно далеко от правильного результата 20000.0, который вы получите с помощью fsum().

Объединение объектов с помощью itertools.chain()

Если вы ищете удобный инструмент для объединения или связывания итерируемых объектов, рассмотрите возможность использования chain() из itertools. Эта функция может принимать несколько объектов и строить итератор, который выдает элементы из первого, из второго и так далее, пока не исчерпает все входные итерации:

from itertools import chain

numbers = chain([1, 2, 3], [4, 5, 6], [7, 8, 9])
numbers
# <itertools.chain object at 0x7f0d0f160a30>
next(numbers)
# 1
next(numbers)
# 2

list(chain([1, 2, 3], [4, 5, 6], [7, 8, 9]))
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

В данном примере вы получаете доступ к последовательным элементам из чисел с помощью next(). Если вместо этого вы хотите работать со списком, вы можете применить list() для использования итератора и возврата обычного списка Python.

chain() также является хорошим вариантом для объединения списков в Python:

from itertools import chain

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

list(chain(*matrix))
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

Чтобы объединить списки с помощью chain(), вам нужно использовать итеративный оператор распаковки *. Этот оператор распаковывает все входные итерации, так что chain() может работать с ними и генерировать соответствующий итератор. Последний шаг – вызвать list() для создания желаемого плоского списка.

Объединение строк с помощью str.join()

Как мы уже видели, функция sum() не объединяет строки. Если вам нужно это сделать, то предпочтительным и самым быстрым инструментом, доступным в Python, является str.join(). Этот метод принимает последовательность строк в качестве аргумента и возвращает новую объединенную строку:

greeting = ["Hello,", "welcome to", "Pythonist!"]

" ".join(greeting)
# 'Hello, welcome to Pythonist!'

Обратите внимание, что .join() использует строку, для которой вы вызываете метод, в качестве разделителя во время конкатенации. В этом примере вы вызываете .join() для строки, состоящей из одного символа пробела " ", поэтому исходные строки разделяются пробелами.

Заключение

Итак, сегодня мы разобрали, что такое функция sum() в Python. Теперь вы можете использовать её для сложения числовых значений. Эта функция обеспечивает эффективный, читаемый и питонический способ решения задач сложения в коде. Также мы поговорили про альтернативы функции sum() и в каких случаях их лучше использовать.

Успехов в написании кода!

Перевод статьи «Python’s sum(): The Pythonic Way to Sum Values».

To find the sum of numbers in an iterable in Python, we can

  • Use the built-in sum() function
  • Use a loop
  • Use recursion

In today’s post, we’ll discuss how to use each of the methods above and highlight some of the things to take note when using the built-in sum() function.

For our practice question, we’ll work on a function that accepts multiple inputs (of various data types) and returns the sum of all numerical inputs.

Here are some concepts we’ll cover in today’s post:

  • How to find sum of numbers in an iterable in Python
  • How to use recursion
  • The difference between the sum() and fsum() functions

Using the built-in sum() function

The easiest way to find the sum of numbers in an iterable is to use a built-in function called sum(). This function accepts two arguments – the iterable to sum and an optional value to add to the sum. Its syntax is as follows:

sum(iterable, value_to_add)

If the function is unable to sum the numbers in the iterable, it throws a TypeError exception. Let’s look at some examples:

Finding sum of numbers in a list, set, dictionary and tuple

#Sum numbers in a list
myList = [1, 2, 3.5, 2.1, 6]
print(sum(myList))

#Sum numbers in a set
mySet = {1, 5, 2.7, 8}
print(sum(mySet))

#Sum numbers in a dictionary
myDict = {1:101, 2:102, 3:103, 4:104}
print(sum(myDict))

#Sum numbers in a tuple
myTuple = (1, 2, 6.7, 1.1, 5)
print(sum(myTuple))

If you run the code above, you’ll get the following output:

14.6
16.7
10
15.799999999999999

The sum() function adds the numbers in an iterable and returns the result.

For instance, in the first example, the function adds the numbers in myList (1 + 2 + 3.5 + 2.1 + 6) and returns 14.6 as the result.

In the second example, the function adds the numbers in mySet (1 + 5 + 2.7 + 8) and returns 16.7 as the result.

In the third example, we pass a dictionary to the sum() function. When we do that, the function sums the keys of the dictionary, not the values. Hence, we get 1 + 2 + 3 + 4 = 10 as the result.

Finally, in the fourth example, the function sums the values in myTuple and returns the result. However, notice that the result is not exact? Instead of getting 1 + 2 + 6.7 + 1.1 + 5 = 15.8, we get 15.799999999999999.

This is not due to a bug in our code or in Python. Rather, it has to do with the way floating-point numbers (i.e. numbers with decimal parts) are represented in our computers. We’ve seen another example of this issue previously when we learned to square a number in Python.

If you are interested in finding out more about how floating-point numbers work, you can check out the following website: https://docs.python.org/3/tutorial/floatingpoint.html

If you need to ensure that you always get an exact result when finding the sum of floating-point numbers, you can use another function called fsum(). We’ll discuss this function in a later section.

Passing a second argument to the sum() function

Next, let’s look at an example of how the second argument for the sum() function works.

If we do not pass any value for the second argument, the sum() function simply adds the numbers in the iterable and returns the result. However, if we pass a value for the second argument, the function adds the value of the argument to the sum of numbers in the iterable. Let’s look at an example:

myList = [1, 2, 3.5, 2.1, 6]

#Without second argument
print(sum(myList))

#With second argument
print(sum(myList, 1000))

If you run the code above, you’ll get the following output:

14.6
1014.6

Passing invalid arguments to the sum() function

Last but not least, let’s look at what happens when we pass an invalid argument to the sum() function.

When that happens, the function throws a TypeError exception. Examples of invalid arguments include passing a string to the function, or passing a list that includes strings as elements.

For instance, the examples below will all give us a TypeError exception if we try to run them.

myString = '123456'
print(sum(myString))

myStrList = [1, 2, 3, '4', 5]
print(sum(myStrList))

myList = [1, 2, 3.5, 2.1, 6]
print(sum(myList, '100'))

myList2 = [1, 2, 3, 4, [1, 5]]
print(sum(myList2))

The first three examples fail as the sum() function does not work with strings. If we pass a string to the function, the function throws an exception. The last example fails as myList2 contains a nested list.

Using a loop

Now that we are familiar with the built-in sum() function, let’s proceed to discuss how we can use a loop to find the sum of numbers in an iterable.

The code below shows an example of using a for loop to sum the numbers in a list:

myList = [1, 2, 3.5, 2.1, 6]
sum = 0

for i in myList:
    sum += i

print(sum)

On lines 1 and 2, we declare and initialize two variables – myList and sum.

Next, we use a for loop to iterate through myList. For each element in myList, we add its value to sum (on line 5).

After we finish iterating through myList, we use the print() function (outside the for loop) to print the value of sum.

If you run the code above, you’ll get

14.6

as the output.

Using a for loop gives us more flexibility in terms of what we want to sum. For instance, if we use the built-in sum() function to sum the elements in a dictionary, it returns the sum of the keys in the dictionary. If we want to sum the values instead, we can use a for loop:

myDict = {1:101, 2:102, 3:103, 4:104}
sum = 0

for i in myDict:
    sum += myDict[i]

print(sum)

When we use a for loop to iterate through a dictionary, Python accesses the key of the items in the dictionary, not the value. For instance, in the for loop above, i stores the keys of the items in myDict.

If we want to sum the values of the items, we need to add myDict[i] (instead of i) to sum, as shown in line 5 above.

If you run the code above, you’ll get the following output:

410

The loop sums the values in myDict. Hence, we get 101 + 102 + 103 + 104 = 410 as the output.

Using recursion

Next, let’s discuss the final method to find the sum of numbers in an iterable. This method involves using recursion. We’ve discussed recursion multiple times in previous posts. If you are not familiar with recursion, you should definitely check out some of those posts (e.g. here and here).

Technically speaking, using recursion is kind of unnecessary in this case, as using the built-in sum() function or a simple loop is much more straightforward. However, recursion can be a very powerful technique for solving more complex questions like a Sudoku or permutation problem. Hence, it helps to see more examples of how recursion works, especially with easier questions like the current one.

As mentioned previously, recursion can be used to solve problems where solution to the problem relies on solving smaller instances of the same problem.

When it comes to finding the sum of numbers in an iterable, suppose we have a list with five numbers. We can find the sum of all five numbers by adding the first number to the sum of the remaining four numbers (a smaller instance of the same problem).

This can be easily implemented using recursion.

myList = [1, 2, 3.5]

def sumRec(iterToSum):
    if len(iterToSum) > 1:
        return iterToSum [0] + sumRec(iterToSum [1:])
    else:
        return iterToSum [0]

print(sumRec(myList))

Here, we first declare and initialize a list called myList.

Next, we define a function called sumRec() (from lines 3 to 7) that has one parameter – iterToSum.

Inside the function, we check if the length of iterToSum is greater than 1 (line 4). If it is, we add the first element in iterToSum (i.e. iterToSum [0]) to the result of sumRec(iterToSum[1:]) and return the sum (line 5).

sumRec(iterToSum[1:]) represents a smaller instance of sumRec(iterToSum).

While the latter sums all numbers in iterToSum, the former sums all elements except the first (as the slice [1:] selects all elements starting from index 1 to the end of the list).

We keep calling the sumRec() function recursively until the length of the list we pass to it is no longer greater than 1. When that happens, we have what is known as a base case (lines 6 and 7).

A base case is the case the stops the recursion. When the length of the list to sum is no longer greater than 1, we do not need to use recursion to find the sum of the elements in the list. As there is only one element in the list now, the sum is simply the number itself. For instance, the sum of [12] is simply 12.

Once we reach the base case, the recursion stops and the result is returned to the caller of each recursive call. This gives us the result of the sum of all numbers in the list.

To see how this works, let’s suppose we call the sumRec() function with [1, 2, 3.5] as the argument.

The recursive calls and return values are shown in the diagram below:

recursive function to find sum of numbers in iterable

Line 9 starts by calling sumRec([1, 2, 3.5]), which in turn calls sumRec([2, 3.5]), which calls sumRec([3.5]).

sumRec([3.5]) is the base case. It returns 3.5 to its caller sumRec([2, 3.5]).

sumRec([2, 3.5]) (which is not a base case) in turn returns 2 + sumRec([3.5]) = 2 + 3.5 = 5.5 to its caller sumRec([1, 2, 3.5]).

sumRec([1, 2, 3.5]) (which is also not a base case) returns 1 + sumRec([2, 3.5]) = 1 + 5.5 = 6.5 to its caller line 9.

Therefore, if you run the code above, you’ll get

6.5

as the output.

That’s it. That’s how recursion can be used to help us find the sum of numbers in an iterable. You may need to read through this section more than once to fully appreciate how recursion works.

Working with floating-point numbers

We’ve covered three main ways to find the sum of numbers in an iterable in Python. Now, let’s move on to a topic we skipped previously.

In the previous section, we learned to use the built-in sum() function to sum numbers in an iterable. While this function is very easy to use, we saw that it does not always return an exact value. For instance, when we pass the tuple (1, 2, 6.7, 1.1, 5) to the function, we get 15.799999999999999 as the output.

If it is crucial for us to get an exact answer when finding the sum of numbers in an iterable, we should use another built-in function – fsum().

This function is available in the math module.

Let’s look at an example:

import math

myTuple = (1, 2, 6.7, 1.1, 5)

#Using sum()
print(sum(myTuple))

#Using fsum()
print(math.fsum(myTuple))

If you run the code above, you’ll get the following output:

15.799999999999999
15.8

The fsum() is very similar to the sum() function. The main difference is that the fsum() function gives an exact output while the sum() function does not always do that.

Another difference is the fsum() does not have a second optional argument. Other than that, the two functions are very similar.

That’s it. We are now ready to proceed to the practice question for today.

Practice Question

Today’s practice question requires us to write a function called mySum() that prompts users to enter a list of values, separated by commas. The values entered can be of any data type. For instance, they can be text, symbols, or numbers. The function needs to return the sum of all the numbers entered. This sum should be rounded off to 2 decimal places.

Expected Results

To test your function, you can run the function and enter the following inputs:

1, 2, 3.1, 4, hello, [2, 3], #, $

The function should return 10.1 as the result.

Suggested Solution

The suggested solution is as follows:

Click to see the suggested solution

def mySum():
    userInput = input('Pleae enter the values, separated by commas: ')
    myList = userInput.split(',')

    result = 0

    for i in myList:
        try:
            result += float(i)
        except:
            pass

    return round(result, 2)

sumOfNumbers = mySum()
print(sumOfNumbers)

In the code above, we first define a function called mySum() on line 1.

Inside the function, we use the built-in input() function to prompt users for an input. This function prompts users for an input and returns the input as a string. We store this string in a variable called userInput.

Next, we use a built-in function called split() to split userInput into a list of substrings, using a comma (https://docs.python.org/3/library/stdtypes.html#str.split) as the delimiter. If the input string is '1, 2, 3, 4, 5', we’ll get ['1', ' 2', ' 3', ' 4', ' 5'] as the result, which we store in a variable called myList.

Next, we declare and initialise a variable called result.

We then use a for loop to iterate through myList.

Inside the for loop, we use a try-except statement to try converting i to a float. If that can be done, we add the value of the resulting float to result (line 9).

On the other hand, if i cannot be converted to a float, the float() function throws an exception and the except block is executed. Inside the except block (lines 10 to 11), we use the pass statement to indicate that we do not want to do anything when i cannot be converted to a float.

After we finish iterating through all the elements in myList, we use the round() function to round the sum to 2 decimal places and use the return statement to return the result.

With that, the function is complete.

Lists are very commonly used to store sequences of values (for example, numbers) in Python. When working with lists, it can be handy to know how to quickly get the sum of values in a list. For example, you have a list of your recorded footsteps in the last seven days and you want to know the total sum. In this tutorial, we will look at how to get the sum of the elements in a list in Python with the help of some examples.

How to get the sum of a list of numbers in Python?

sum of elements in a python list

You can use the python built-in sum() function to get the sum of list elements. Alternatively, you can use a loop to iterate through the list items and use a variable to keep track of the sum.

Let’s look at the above-mentioned methods with the help of some examples.

Using sum() to get the total in a list

The built-in sum() function in Python is used to return the sum of an iterable. To get the sum total of a list of numbers, you can pass the list as an argument to the sum() function.

# create a list
ls = [10, 15, 20, 25]
# sum of list elements
sum(ls)

Output:

70

We get the sum of the values in the list as a scaler value.

Note that the sum() function may result in loss of precision with extended sums of floating-point numbers. For example –

# create a list
ls = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
# sum of list elements
sum(ls)

Output:

0.8999999999999999

As an alternative, you can use the math standard library’s fsum() function to get an accurate sum of floating-point numbers and prevent loss of precision.

import math

# create a list
ls = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
# sum of list elements
math.fsum(ls)

Output:

0.9

We get the accurate result this time. For more on the fsum() function, refer to its documentation.

Using loop to get the sum

Alternatively, you can use the straightforward method of iterating through the list elements and keeping track of the sum.

# create a list
ls = [10, 15, 20, 25]
# use a loop to get the sum
total = 0
for item in ls:
    total += item
print(total)

Output:

70

We get the sum of the values in the list.

You might also be interested in –

  • Python – Find Average of values in a List
  • Python – Get median of a List
  • Find Mode of List in Python

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

    View all posts

Sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list. 

Syntax:

sum(iterable, start)  
iterable : iterable can be anything list , tuples or dictionaries ,
 but most importantly it should be numbers.
start : this start is added to the sum of 
numbers in the iterable. 
If start is not given in the syntax , it is assumed to be 0.

Possible two syntaxes:

sum(a)
a is the list , it adds up all the numbers in the 
list a and takes start to be 0, so returning 
only the sum of the numbers in the list.
sum(a, start)
this returns the sum of the list + start 

Below is the Python implementation of the sum() 

Python3

numbers = [1,2,3,4,5,1,4,5]

Sum = sum(numbers)

print(Sum)

Sum = sum(numbers, 10)

print(Sum)

Output:

25
35

Error and Exceptions

TypeError : This error is raised in the case when there is anything other than numbers in the list. 

Python3

arr = ["a"]

Sum = sum(arr)

print(Sum)

Sum = sum(arr, 10)

print(Sum)

Runtime Error :

Traceback (most recent call last):
  File "/home/23f0f6c9e022aa96d6c560a7eb4cf387.py", line 6, in 
    Sum = sum(arr)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

So the list should contain numbers Practical Application: Problems where we require sum to be calculated to do further operations such as finding out the average of numbers. 

Python3

numbers = [1,2,3,4,5,1,4,5]

Sum = sum(numbers)

average= Sum/len(numbers)

print (average)

Output:

3

using for loop 

In this , the code first defines a list of numbers. It then initializes a variable called total to 0. The code then iterates through the list using a for loop, and for each number in the list, it adds that number to the total variable. Finally, the code prints the value of total, which is the sum of the numbers in the list.

Python3

numbers = [10, 20, 30, 40, 50]

total = 0

for num in numbers:

    total += num

print("The sum of the numbers is:", total)

Output

The sum of the numbers is: 150

The time complexity of this approach is O(n), where n is the number of elements in the list. The sum() function iterates through the list once to calculate the sum.

The auxiliary space is O(1), as the only additional memory used is for the total variable, which is a constant amount of memory.

Last Updated :
24 Feb, 2023

Like Article

Save Article

Понравилась статья? Поделить с друзьями:

Не пропустите также:

  • Как найти заголовки h1 на сайте
  • Как составить программу адаптации нового сотрудника
  • Как общаться найти тему
  • Эта диаграмма недоступна в вашей версии excel как исправить
  • Как найти количество ватт

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии