Tuesday, March 28, 2017

Dictionary in Python

A dictionary is a dictionary. It's a lookup and contains key value pair. Dictionaries are mutable.

#create an empty dictionary
>>> capital = {}

@asssign elements to the dictionary
>>> capital['India'] = 'Delhi'
>>> capital['Germany'] = 'Berlin'

#print the dictionary
>>> capital
{'Germany': 'Berlin', 'India': 'Delhi'}

Monday, March 27, 2017

Tuple in Python

Tuple is like a list. Only difference is it's immutable whereas list is mutable. What it means is that one can change the elements of a list after initial assignment, however the elements of the tuple can't be changed. Tuple in that sense provides a write protection guarantee.

#Create a tuple of Country and Capital and we don't want to change it
#Note that tuple use parenthesis for definition
>>>tuple = (['India','Delhi'],['USA','Washington'],['Germany','Berlin'])

#Trying to change the tuple element fail
>>> tuple[0] = ['India','Pune']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

#accessing element of tuple
>>> tuple[0]
['India', 'Delhi']

#accessing sub-element of tuple
>>> tuple[0][1]

#be careful you can change sub-element
>>> tuple[0][1] = 'Pune'
>>> tuple
(['India', 'Pune'], ['USA', 'Washington'], ['Germany', 'Berlin'])

Is your Big Data an Asset or Liability?

Balance sheets are funny things. In one stroke, they tell your positives and negatives. And it takes no time for assets to convert into liabilities also. The best examples of assets and liabilities can be taken from our daily life. If the kid turn out to be good (whatever it means to one), the kids are assets otherwise they become liability. A marriage or a partnership can become both asset or liability. 

Companies should also look into their data from a similar perspective. Data in itself has no meaning if you can't mint it. You can boast of having Terabytes and Petabytes of data, having clusters and farms and ranches  and villas and islands of servers to store them.  However if you are not able to make any money about of it, is it even worth the salt?

Counter in Python

Counter is a simple way to find the text occurrences in a given text. You can use it to create tag cloud also. Let's look into an example to see how this works

Let's see the word occurrences from a given url in the web. The code can be used to process any palin text also. Just pass it to the Counter method as a list of words.

Listifying String and Strigifying List in Python

At times we want to convert a text into individual elements and to convert a list of words into texts

Let's say we want to convert the following text into a list of words. Let's have the famous Lorem ipsum text. This text is a scrambled version of De finibus bonorum et malorum, a 1st century BC Latin text by Cicero. 

Lists in Python

Python Lists are implemented as arrays. The elements of the list can have any types. Let's work with a list example to see the various ways of interacting with it.

Let's create a list first. A list can be created by putting elements in the square brackets:

# A list of famous indian comics and their debut year. List can hold mix data types
>>> comics = ["Lot Pot", 1969, "Tinkle", 1980,  "Nagraj", 1986, "Doga", 1992, "Chacha Chaudhary", 1971]

Sunday, March 26, 2017

String handling in Python

A string in python can be declared simply as:

>>> first_string="Pythonidae"
>>> print first_string