Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Functional Programming

00:00 In the previous lesson, I showed you how iterable objects work. This lesson is the last in this section, and it covers the built-in functional programming functions, map() and filter().

00:11 I suspect I’m going to say the word function and its derivatives a lot in this lesson.

00:17 Functional programming is a separate coding paradigm where you use pure functions to get your results. A pure function is one with no side effects.

00:29 That means no global state and each function call is stateless. Any data you’re using needs to be passed into the function for it to operate upon it. Functional programming typically requires the language to treat functions as first-class objects, so you can do more than call them.

00:42 You can also pass references to them, treating the function block as a kind of data itself. Everything in Python is an object, even the functions, so no problem there. You often use these kinds of references for callbacks, where some caller in the future calls your function as part of the calculation.

01:02 Python as a language has a flavor of a little bit of everything. It supports a functional programming approach, but it isn’t what I’d call a functional programming language. A more pure language wouldn’t allow you to do non-functional programming things like create side effects.

01:18 Python’s attitude is right tool for the job, so if you want to use this approach, or any of the others, you’re free to do so as you see fit. There are two built-in functions that are common in functional programming.

01:30 They’re filter() and map(). filter() returns an iterator containing each item that passes a test in an iterable, giving the test as an argument, while map() returns an iterator that performs a transformation on each item in an iterable.

01:46 There are other functional programming tools in the Python standard library, but they’re tucked away in modules. Take a look at the itertools and functools modules for a whack of them.

01:57 Let’s go look at filter() and map(). I’ll start out with the filter() function. This takes two arguments, a callable that gets run on each item in an iterable, and the iterable to operate upon.

02:10 If the callable evaluates to True, then the value being examined gets included in the result. Otherwise, it’s filtered out, hence the name.

02:22 Starting with some data.

02:29 Here, the callable I passed in was a lambda. If you’ve never seen these before, they’re an anonymous inline function. The n on the left of the colon is the argument, and to the right of the colon is the expression that gets evaluated and turned into the response for the function.

02:46 Mine here is a Boolean expression evaluating if the number is divisible by 2. filter() invokes this callable for each value in the numbers iterable and returns an iterator object, of course.

03:04 One list() trick later, and you can see the three even values contained in the result. If you pass None as your callable, filter() evaluates the truthiness of each item,

03:23 which gives you the number and letters and ignores the empty stuff. Any callable works, including your own functions.

03:46 That’s a little silly, as all you’ll ever get back is a list of Bobs, but it demonstrates that any callable you pass in can work. map() is a transformational function, allowing you to run an operation on each value in an iterable.

04:05 You think I’d get tired of that, huh? Say it with me, list() trick to the rescue.

04:13 Like with filter(), map() takes a callable and an iterable. This time, though, instead of filtering, it runs the callable on each item.

04:21 Here, I’ve used a lambda that multiplies by 2, thus doubling each of the values in the numbers list. You can use callables that take multiple arguments by passing in multiple iterables.

04:33 This is kind of like zip() in that the nth value from each iterable forms a collection, and that collection becomes the arguments to the callable.

04:41 Let me start with a bit of new data.

04:52 Here, I’ve used the built-in pow() function, which raises the first argument to the exponent of the second. Each of the three values in bases then gets raised to the corresponding value in the exponent’s iterable.

05:05 Way back long ago, I showed you that you can use the pow() function or the double star operator. One reason Python supports both is so that you can pass in the function as a reference to functional programming mechanisms.

05:18 You can’t do that with an operator. In fact, as a quick aside, Python has a whole module dedicated to this idea. The operator module contains these kinds of functions.

05:31 Here, I’ve imported add(). add() takes two arguments and adds them together. It’s the functional equivalent of the plus sign.

05:44 And by mapping them, I get the sum of the pairs. To truly demonstrate the functional programming philosophy, I need to chain some of this stuff together. Let’s square the even numbers in an iterable.

06:06 That’s a lot. Let me explain it a piece at a time. Starting on the right side, I’m building a filter(), which returns only even numbers, and the result of that filter is then fed to the map() as an iterable.

06:20 The operation the map uses is to square the value, so the end result is the squares of 10, 6, and 50. Like I said up top, Python isn’t the purest of functional programming languages, but you can do loads of functional programming with it. Once I was at a function where we discussed the function of functional programming functions, buffalo, buffalo, buffalo, etc.

06:43 Another common functional programming function is reduce(), which is kind of like map(), but it feeds the result of the first operation into the second operation as an argument.

06:54 Combining reduce() with add() gives you sum().

06:57 reduce() used to be a built-in function, but it got moved into the functools module, so if you want to play with it, that’s where you can find it now.

07:06 You’ve probably noticed a pattern by now. Yes, there’s more. In fact, with functional programming, there’s lots more. On filters, on maps, on the reduce() function, which I briefly mentioned, or if that’s not enough, there’s a full learning path on functional programming with multiple courses, tutorials, and quizzes for you.

07:27 That’s all for the iterables section of built-in functions. Next up, I’ll look at functions for output.

Become a Member to join the conversation.