Python is Awesome!
Python is, by far, my favorite programming language. This is largely due to the inclusion of lambda functions and some awesome built-in functions which allow for neat functional programming.
Normally in python, you define functions like this:
def f(x):
return x**2
but you can also define functions inline like this:
f = lambda x:x**2
Simple functions like this come in very handy...for instance let's say you want to sort a list of strings alphabetically, but ignoring the first character:
words = ["hello", "world", "this", "is", "an", "example"]
sorted_words = sorted(words,key = lambda x: x[1:])
This yields the following list: ['hello', 'this', 'an', 'world', 'is', 'example'].
Note that we passed the lambda function as an argument so it knows how to compare elements of the list!
This type of thing can be very useful for dealing with dictionaries (dicts).
You can also use the built-in functions "max", "min", and "sum" similarly:
l = [1,2,3,4,5]
m1 = max(l) #5
m2 = min(l) #1
s = sum(l) #15
These each also take an optional "key" parameter so that their values will be calculated based on a function of the elements and not based on the elements themselves.