On 4 October 2016 at 19:11, Alan Gauld via Tutor <tutor@python.org> wrote: >> """Define a function sum() and a function multiply() that sums and >> multiplies (respectively) all the numbers in a list of numbers. For >> example, sum([1, 2, 3, 4]) should return 10, and multiply([1, 2, 3, 4]) >> should return 24.""" > > <rant> > I do wish teachers would not ask students to reinvent the wheel.
You may be misunderstanding the teacher's objectives here. Sometimes Python is used to introduce the concept of programming more generally rather than specifically to prepare someone for professional programming using Python. I don't know about you but I came to Python after a number of other programming languages. Now that I teach programming I can see that it's important in a first programming introduction to cover nuts and bolts type algorithmic thinking. sum() is a good example of an algorithm that most people are already used to performing on pen and paper making it a good candidate for learning to use loops and functions. > Python already has a perfectly good sum() function. Writing your own sum function helps to understand what actually happens when using the standard sum function. I do the same thing with my students in C teaching them how to make their own e.g. strlen function. I will tell them that strlen exists afterwards but I want them to understand what the null byte is and writing your own (or just looking at a) strlen implementation helps with that. Likewise simply using a function e.g. strcat without understanding its implementation can lead to segfaults so there can be a relevance in understanding the implementation even if you don't intend to reimplement something in your own code. The implications of the implementation of sum are also non-trivial which is why the stdlib contains at least 3 different sum functions. It's great that Python comes with so many functions to do common tasks. However from personal experience teaching Python as a first language can lead to the disadvantage that many students become spoilt programmers. They will learn to assume that there is a function for exactly what they want and all they ever need to do is ask a precise question in a search engine and read the first stackoverflow page. That may be the correct approach for many problems but it isn't always. A programmer should have the ability to implement basic algorithms (e.g. sum) for themselves when necessary. > And both problems become trivial if using functools.reduce() > </rant> Different people think in different ways but I reckon most people - and especially most beginners - would find a loop more straightforward than reduce. I doubt that the OP is at a stage where using reduce seems trivial. -- Oscar _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor