This article explains the Python basics and shows a few cool functional programming concepts.
At first we need a way to run Python code. If you don't know how to do it, then please read our Default Python IDE tutorial first.
Note: we will use Python 2 but it should work with Python 3 too.
Yay simple as that. No need for a ";" at the end.
We don't even have to declare the type, variables can be everything (int, string, float, ...).
The last print has nothing to do with the if clause, because of its indentation.
As you can see, Python really makes our lives easier.
Again, incredibly simple and as minimalistic as possible.
So the question is, whats that [ : ] thing?
The [:] thing is one of the greatest things ever invented when it comes to lists. It's called the Colon operator and it specifies a range like [from : to].
Just imagine how complicated it would be to do that in a language like C++ or Java.
This might doesn't look like much, but shorter code is often better code. It improves readability and it makes our keyboard (and fingers) happier.
All that stuff above is cool, but we want to go further. There is a concept that is called functional programming. Python supports this concept!
But functional programming is very close to math and it can become very complicated to understand, so we will only talk about a few things that are actually useful and understandable by humans.
We will take a look at the typical functional programming functions: map, zip, reduce and filter and show how they work with the help of some very easy to understand examples.
Note: those functions usually just do some crazy stuff to a list. For example applying a function to each entry of a list. No scary math involved at all!
The first typical function is filter. As the name says, it filters a list.
It just goes through the list [1, 2, 3, 4] and applys a function (in our case is_bigger_than_two) to each element. If it returns true it takes the element to the result list, otherwise it just ignores it. Hence why the result is the list [3, 4] which are all elements that are bigger than two.
Not too scary yet, so let's keep going.
The map function maps another function to each element of a list. In other words: it applys a function to each element in the list, nothing more.
The above code just maps the inc function to each element of the list [1, 2, 3, 4]. So it increases 1 by 1, increases 2 by 1, increases 3 by 1 and then increases 4 by 1 and returns the result list which is [2, 3, 4, 5].
Again, still not too complicated but incredibly powerful.
The zip function connects values like a zipper. It takes two lists and creates a list with the connected values as tuples ( a tuple is something like (1, 2) ).
It prints [('a', 1), ('b', 2), ('c', 3)]. That's it, no crazy math or anything. Just a combination of two lists.
The last one is the reduce function. It takes a list and a function, but it only returns a single value this time, instead of returning a list like all the previous functions.
Example: if we have the list [1, 2, 3] and call reduce with a multiply function, it would return 1 * 2 * 3. If we would call it with a minus function then it would return 1 - 2 - 3. The fun starts if we call it with more complicated functions, but more about that later.
Hint: it helps to get a pen and a piece of paper to draw the lists and see what exactly happens.