I really start to like Python – the programming language…
Just a few examples of its power:
*) let’s say I want to count and store the frequency of letters in a list:
#letter frequency counter using dict data type
letterlist = [‘a’,’b’,’c’,’a’,’b’]
freq = dict()
for k in letterlist:
freq[k] = freq.get(k,0) + 1 # if letter !there, create it, otherwise inc counter
The IMO beautiful thing here is the last line, which either increments the counter for an already existing letter in the map, or, if the letter isn’t there, creates an entry for it, initializing the count to 0.
*) Tuple swap operation
a = 1
b = 2
a,b = b,a
Gone are the days where you had to use a temp varible for a swap: tuple assignment does the trick for you without any temps.