Pythonic Magic: Lambda expressions

All Justin Bieber needs is a Beauty and a Beat. Personally, I am happy with a Beauty and Python. Yeah, honestly, python offers one so much, that it’s difficult to ask for more (except for speed maybe, but that’s a tradeoff for python’s amazing expressiveness. And people try to address it as we speak).

So, as you may already have understood, in this second article at my featured series of Pythonic Magic, I am going to talk about Lambda Expressions. But what are Lambda Expressions, one might wonder. Lambda expressions are one of the most powerful (in my opinion) features of python, but people easily tend to misunderstand it or ignore it all together. Which is bad. ‘Cause it is awesome!

Lambda expressions are essentially a language construct that allows one to create anonymous (that is, not bound by any name) functions (more rather function objects) at runtime. That’s it! Really! I will admit that it’s implementation may be hard for some new python programmers to grasp, but once you get the hold of it, it’s really not that difficult. Before we go any further, it would be useful to provide an example of what a Lambda Expression looks like.

Supposing we want to create a function that returns the square of its input. In python, we would do something like this:


>>> def square(x):
...     return x * x
...

Pretty straight forward if you ask me. Nothing magical in this. Now let’s take a look at the output the above function produces:


>>> square(3)
9
>>> square(2.2)
4.840000000000001

Again, nothing magical (if you ignore that number following the trailing zero’s there, it’s all normal. That behavior is also normal, and has to do with hardware limitations on floating point number calculations.) Now that we got this, it’s time to see how the above could be implemented using Lambda Expressions.


>>> g = lambda x: x ** 2 # ** is the exponent operator in python. I could have also said x * x, but I refrained to do so, for clarity's sake
>>> g
<function <lambda> at 0x7f17656045f0>
>>> g(2)
4

Very nice if you ask me. As far as the lambda expression is concerned, it can do pretty much anything that the named function can do. And as you can see in my code snippet, using the lambda keyword, you get to have a lambda function object. Having a function object means that the object is callable (I mean obviously, it’s a function), which in python means that you can bind to any reference, and still call it using the parenthesis as in g(2).

If it still is kind of obscure to you, then maybe it should be worth for me to try to explain it a little bit better. Lambda forms have this general form: lambda [parameter list]: expression This is essentially the keyword lambda, followed by a number of parameters, and an expression, the value of which gets returned when you call the lambda function object.


>>> g = lambda x: x ** 2 # In this case x is the parameter, it is used in the expression to calculate the square of it, and the value of the calculated expression (in this case, the square) gets returned when the object is called.

Now I may like to call it with one parameter, but there is really no one stopping you from calling it with more than one parameters. For example take a look at the following


>>> sum = lambda x, y: x + y
>>> sum(4, 2)
6

>>> # Essentially the same as
... def sum(x, y):
...     return x + y
...

Of course I barely even touched the matter. However I believe that lambdas are a very powerful feature, which although not terribly useful all the time, may be a killer feature for when you really need that. If you feel like you want to know more about lambda expressions, I formed a neat list with various links explaining more than I am on the matter:

Advertisements

Pythonic Magic: List Comprehensions

Disclaimer: This will be the beginning of a series of articles, inside which I will select some of the features in python that I like the most and why I do like them, along with some description and usage of the feature at hand.

Disclaimer: I do not claim to be an expert in python (or any other language for that matter). Anything that follows is my own personal opinion, and may even be wrong, as it is dependent on my understanding on the matter. However I am doing my best to make sure that I do not include wrong information.

—————————————————————————————————————-

You may have heard that the Python programming language is easy to both learn and use. You should slap those that told you so, for blatantly lying to you. Python is not easy to learn. It is *very* easy. *Very very easy*. As a matter of fact it’s so easy that even babies could learn python.  Hyperbole, but you get the point.

I am not going to go to lengths as to why you should learn python. As a matter of fact, a google search can possibly give you many reasons to do so. However the ultimate reason for one to learn python, is to simply want to do so. Why you would want to do so, is an entirely different matter which I will not discuss here, or I will miss the original subject of this article. Which is: wait for it… List Comprehensions! Who could imagine!

Before I go on to explain what list comprehensions are, maybe I should give a description of what lists in python are. Lists are well…lists. Pretty surprising huh? No, on a more serious note, lists in python are a data structure, that acts like a container for any amount of objects, in a given order. If you already have been introduced to programming via another programming language, you may be able to think of the list as an array of objects, which has variable length. In fact Lists are much more powerful than that, following the tradition that Python follows, which is to ensure that you have all the tools that you may possibly need to work right out of the box. In fact, lists (and the other data structures that python provides by default, such dictionaries, sequences, tuples, etc) are so powerful and versatile, that 99% of the time, you will not need to use anything else than what Python already provides. Below is a demonstration of the lists in python.

>>> my_list = [3, 4, 5] # this creates a list with 3 integer objects
>>> my_list
[3, 4, 5]

>>> my_list = [] # this creates an empty list
>>> my_list
[]

>>> my_list.append(5) # this adds 5 to the list
>>> my_list
[5]

>>> my_list.insert(0,3) # this adds 3 in the first position of the list
>>> my_list
[3, 5] # python like many other languages is 0 indexed. This means that list indices begin with 0 instead of 1.

>>> my_list.remove(3) # nothing unexpected here
>>> my_list
[5]

>>> item = my_list.pop() # removes the object from the list, and returns it to be stored at item
>>> item
5

>>> my_list
[]

This pretty much sums the basic usage of Lists in python. I won’t go to any greater extend to because that would doesn’t concern this article. If you would like to read more about lists and their usage, I recommend that you study the official Python tutorials on Lists.

Now that we have talked about Lists, it’s time to talk about List Comprehensions. List Comprehensions are simply a sophisticated way to create Lists that have specific contents. If that doesn’t help you, maybe we should look at an example.

Let’s say we want to create a list of the even numbers in the range between 0 up to (but not including) 10. Normally you would do something like this:

>>> my_list = [] # constructing an empty list
>>> for number in range(10):
        if number % 2 == 0:
            my_list.append(number)
>>> my_list
[0, 2, 4, 6, 8]

List comprehensions allow you to do just the same thing, just times easier (and more sexy, by the way):

>>> even_nums = [x for x in range(10) if x % 2 == 0]
>>> even_nums
[0, 2, 4, 6, 8]

Holly floppy McBaggies. Did you see that? Human words fail to describe the Beauty and the Epic-ness in this.

You might be wondering now. How could I possibly use this to get the most out of my life  it? Well to be honest, there are many ways you could possibly use this feature. Yours trully uses List Comprehensions to create Lists out of  Strings (which are Immutable, that means they are unable to be changed. The reason is here.) to allow for easier manipulation of those. For example:

>>> name = 'fotis'

>>> name[1] = 'u'
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

>>> name_list = [letter for letter in name] # creating a list out of name's characters
>>> name_list
['f', 'o', 't', 'i', 's']
>>> name_list[1] = 'u'

>>> name = ''.join(name_list)
>>> name
'futis'

As you can see we are talking about an extremely powerful feature, that one could use to create lists that have objects that are computed on the fly. As to the possible usage cases for this? Only your fantasy could possibly stop you from using it.

Why I believe that Python should be taught over C as a first programming language in universities.

It is (or maybe was) a worldwide trend for universities and colleges around the globe to introduce students to the C programming language as their first programming language. While some schools have now changed to Java instead, some are still teaching C as the first language one gets exposed to.

While there is nothing wrong with C (disclaimer: I love C, and I also consider it a must for software developers to learn, whether working high or low level), I firmly believe that is not suited to be used for such roles. C, being a medium level programming language, is somehow high level (this of course, is relative to comparison) while still managing to stay relatively close to the metal. C of course, derives much of its power from this single fact. Hardware interaction, while not useful to every single programmer, is still very useful for some tasks. One task I can think of, due to my work, is computer infrastructure projects. Compilers, Virtual Machines, and more, all can benefit from being close to the metal, and have a direct interaction with the computer’s hardware. I am sure this is all there is to it, but the aforementioned examples, can serve to demonstrate the need for low level interactivity.

However, tradeoff for this power is the fact that low level facilities get in your way when you program in C. Pointers quickly come to mind. No matter how powerful pointers are, most people are unable to effectively use them. This can be seen in most high level languages today, where pointers are pretty much extinct. Not only this, but to properly understand C, you need to have, at least some, knowledge of computer architecture. While you still can use C, regardless of the prevalence of such knowledge, it’s not uncommon to see students learn that a C x86 integer is 4 bytes, but not being able to answer why it is so if asked. Not to mention that the way student’s are taught C, by not telling them about the C standard library, they are getting reduced to exercises about array manipulation that get repetitive and tedious over a short amount of time, and don’t really add much to the student’s knowledge. The end result is that students feel incapable of delivering anything more than something trivial. Not to mention that there are schools (at least here in Greece) that teach C programming in Windows.

On the other hand, higher level languages today, such as javascript and python, at least in my opinion are more suited to serve as a language in an introductory course. That is because these languages are high level enough to abstract the underlying architecture in such a way, so that programming concepts can be demonstrated without the need to have knowledge of the underlying architecture. This way, algorithms, data structures, and more can be taught, without having the language’s idioms get in the way. What’s more, is that these languages are known to have an easier learning curve, and last but not least, they integrate a great standard library, that student’s can use to deliver something useful, and see real world applications development faster than in C. What’s not to love?

While I personally believe that a serious programmer should know at least one of the so called medium level languages (C, C++), even if they learn to one only to understand more about the way the machine works, I firmly believe that such languages should not be used in introductory courses in computer science.

So how do you feel about that? Do you think that C should be taught at introductory courses in computer science? Or do you feel (like me) that other languages are better suited, and why?