Python - strategie Jiří Znamenáček Sum of the elements of the list 2015-04-30

Write a function which takes a list of numbers as its sole argument and returns a sum of all these numbers.

What is known and what we have to do?

• Desired function should return a number.
• This number will be computed by successively adding numbers from the input list.
• We need to remember this number between each step. Its starting value must be 0 to not spoil the output.

Let's try to implement this.

The fist task is to create a function which retuns a number:

def sečti(): return součet

Since we use Python this code will not break until we try to call the function sečti(). In that moment the language interpreter will try to locate a value for the variable součet and fail miserably:

NameError: global name 'součet' is not defined

So let's modify our function this way:

def sečti(): součet = 0 return součet

This will work. And more importantly – it makes sence because for empty list the return value will be zero, which should be expected.

Our function already returns a reasonable output however it takes no input yet. Let's amend this:

def sečti(xs): součet = 0 return součet

Since Python is a dynamically typed language we don't tell it that an argument `xs` will be of type list. In fact – there's no reason for it to be of this exact type! However strange things can happend if it's not ^_^

Statically typed languages keeps track of what is what and nothing can be change after the compile time. It is more work to start with but in many cases it can save your a** later.

We've got a list at the input and the number (zero) at the output. What is missing? To add all numbers from the list together! `for-in` loop comes to the rescue:

def sečti(xs): součet = 0 for x in xs: součet += x return součet

This works as advertised. Try it: `print( sečti([1, 2, 3, 4, 5, 6, 7,]) )`

Weel, it works. However there are some hidden corner cases:

• The `for-in` loop doesn't care at all what the actual type of the argument `xs` is as long as it's an iterable one. (And in Python you can iterate over almost anything.)
• Almost the same applies to the `+` operator – it adds/concatenates everything which can be added. However since we supplied 0 as the starting point it will oppose heavilly against anything else than numbers.
Try it: `print( sečti(['a', 'b', 'c', 'd',]) )`

We can "correct" it in several ways:

1. We can assert at the beginning of the function that the supplied list is the "correct" one and refuse to work otherwise. (For example you can use some variant of the `assert` statement for this.)
2. Modify the sum-function in some way so that it would be able to "summarize" other types aside from numbers also.

In fact many functions and methods in Python behaves the second way – if it makes some sence to do the calculation for the supplied arguments, the calculation is carried away whatever the types are. (To learn more search for "duck typing".)

PS: There's an easier way how to sum up all the number elements from the list – just use the built in function `sum()` ^_~