Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Code With Functional Programming
00:00Welcome to this lesson, where you’ll explore the fascinating world of functionalprogramming and pure functions. As the name suggests,functional programming relies on functions to perform tasks,but what sets it apart from other programming paradigms is the use of purefunctions. Pure functions are functions that don’t change the program’s state.
00:23This means that when you call the function with a specific input,it always returns the same output.It doesn’t modify any variable outside of its scope or depend on any externalstate. Now, you might be wondering, why is this important? Well,pure functions make your code more predictable, testable,and easier to reason about. When you use pure functions,you can be confident that your program will behave consistently.
00:49Python provides functional language features, such as anonymous functions,map(),filter(), andreduce(). You might be wondering what anonymous functions are.
00:59They’re basically nameless functions that you use when you needa function, but not for a very long time.You just need them for a short period of time. They’re also called lambdafunctions, andmap() andreduce() are also functions that are really important.
01:16In the next lessons,you’re going to dive deeper into all three concepts and learn all about them.
01:25Let’s explore pure functions and how they work in practice. In this example,you have two functions. One is a pure function, and the other is not.
01:35The function on the right is not a pure function because it uses a globalvariable namedtotal that is set to0.When theadd_numbers() function is called with two variables,a andb,it adds them together, updates the globaltotal variable,and then returns it. Right here,because the function modifies the global state—and by that,I mean the value of thetotal variable—calling it twice with the same input won’t always produce the sameresult. The output will depend on the current state of thetotal variable.
02:10The first time theadd_numbers() function runs,thetotal variable is initialized to0.
02:16After adding1 and2 together,the function updatestotal to3 and returns it as the result.
02:23On the second iteration,theadd_numbers() function adds the sum of1 and2 to the current value of thetotal variable, which is3 from the previous iteration,resulting in a return value of6. In conclusion,theadd_numbers() function you just explored is not a pure function because itmodifies the global state.
02:45You might be wondering, if this isn’t functional programming, then what is it?The answer is imperative programming,which is the opposite of functional programming.
02:55You’ll hear more about this shortly. On the other hand,the function on the leftis a pure function,since it does not modify any global variable and always returns the same outputfor identical inputs. As you can see,the first time you run the function with inputs1 and2, it returns3.
03:15And when you run it again, it also returns3.This is great because you can rely on this function to always produce the sameoutput for the same inputs.
03:28In the previous slide, you briefly learned about imperative programming,which is the opposite of functional programming.To conclude everything you just learned about, imperative programming changes theprogram’s state, and functional programming doesn’t.
03:42You’ll see anonymous functions,a.k.a. lambda functions, being used a few times in this course,so let’s explore them in the next lesson.
Course Contents
- Get Started With filter()
- Use filter() for Functional Programming
- Code With Pythonic Style

