Python Power

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

#tuple swap
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.


About swdevperestroika

High tech industry veteran, avid hacker reluctantly transformed to mgmt consultant.
This entry was posted in development, software and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s