Sunday, July 20, 2014

django - Some more Python

We have seen many functions/ methods related to string data type in our previous post.  There are many such and useful methods for different data types.  We will cover few more about them in this post before moving on to django.

I will mention about methods like 'center', 'ljust' and 'rjust'.  As the names suggest, these methods help in formatting the string value by centering it, left justifying or right justifying.

All these take two parameters or arguments: width of the resulting string and the character to fill in the remaining spaces.  It could be a white space.  Note that, the value for width should be greater the length of the string itself.  Else, it will not work properly.



The above screen shot shows how these methods return the values by using a white space and "*".
If you want to separate the contents of a string value, you can use the 'partition' method.  It takes a separator value (any character including the white space present in that string) and returns a list containing the first block of string up to that separator, the separator itself and the remaining block of the string. 



I shall now explain about two of other data types commonly used: 'tuple' and 'dictionary'.

We have seen the 'list' or an 'array' before and have worked out few examples.  'tuple' is similar to the 'list'.  But whereas you can modify the 'list' after it is declared, we cannot modify the 'tuple'.  And, while the 'list' is defined with square brackets ('[]'), 'tuple' is defined with normal brackets.



The above screen shows how a 'list' and 'tuple' are declared and what you can or cannot do with them.  Both 'list' and 'tuple' work in the same way.  Note that, 'append' method is used to add a new item at the end of the 'list'.

Also note that, while we are able to modify the contents of the 'list', Python does not allow any modifications to the 'tuple'.

'dictionary' is where you define values in 'name:value' pairs.  It is usually enclosed within curly brackets ({}).  Below screen shows the example:



As observed in the screen, 'name', 'type' and 'year' are like keys.  They hold the values 'django', 'language' and '2014' respectively.  'items' will return the contents of the dictionary. 

You can delete the contents from the dictionary by using 'pop' or 'popitem' methods.  'pop' removes the item based on the given key while 'popitem' removes the first item.

 

In order to retrieve the contents of any key, you can use the 'get' method.  And you can directly use the key to update the value.

 

To add a new pair of key/ value item into the dictionary, you can use the 'update' method.  It will insert the new item at the beginning of the dictionary.



You can store data by combining lists and dictionaries as the following example shows:



Here, we have two dictionaries inside a list.  Note that the key names should be the same.  You can use the combination of index and key to get the desired values.

So, that ends our brief but extended overview of Python.  I will cover more in other posts but for now, we shall start looking about django.

No comments:

Post a Comment