Total Pageviews

Donate

Dec 29, 2010

Python String Literals

Some times while dealing with strings using python we find that a string starts with a character before the string quotes. for example:
r'here is my string' 
or:
u'here is another string'
what are the 'r' and 'u'??

In python strings, we may use the backslash (\) character to escape characters that has different meanings such as:

  • New Line (\n)
  • Tab (\t)
  • backslash it self (\\) :)
  • ...etc

Also strings may optionally be prefixed with a letter - like the examples above - and here are there different meanings:

  • A prefix letter 'r' or 'R', these strings are called "Raw Strings" and they use different rules for the backslash skipping rule ...
    • All characters following the backslash are included in the string as they are. For example "\n" is not a new line ... it is a backslash followed by a small character 'n'. Also, "\"" will give a string error because the backslash followed by a double quotes is interpreted as they are and hence, this string contains three double quotes and this is invalid. 
    • Also if a single backslash followed by a newline is interpreted as two characters as a part of the string, not as a line continuation !!
  • A prefix letter 'u' or 'U' makes it a "Unicode String" which used the Unicode character set defined by the Unicode Consortium ISO 10646.
    • Here the backslash only escape the codes for characters. Let's see examples:
      • u"\u0062" is a small letter 'b'.
      • u"\u0025" is the percentage sign '%'
      • u"\n" here the backslash will not escape the new line character and the string will remain as it is "\n"  
Hope this was useful :)

Dec 15, 2010

Changing default python version in Ubuntu

It happens a lot that you have installed more than one version of python on Ubuntu. let's say python2.5 and python2.6 and you want to change the default python version.

It is very simple and with a single line of shell script


For example if I have python2.6 is the default and I wanna to change it to python2.5
Here is the default python when you just type python in the command

~$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>


Now let's change it

~$ sudo rm /usr/bin/python && sudo ln -s python2.5 /usr/bin/python
Now check the verstion

~$ python
Python 2.5.4 (r254:67916, Jan 20 2010, 21:45:54)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
and also to change it back use the same command again

~$ sudo rm /usr/bin/python && sudo ln -s python2.6 /usr/bin/python
How this works??
1) we remove the executable from /usr/bin/python
2)we link (ln) the python version we want to be copied again to the location of python executable

Very simple :)
Hope it is useful :)

Dec 14, 2010

Removing duplicate Items from a python List with a single line of code !!

It is very common that you have generated a list and it has duplicates. And for a reason or another you wanna remove these duplicate.

For sure you may construct an empty list and go for each element in the original one and if it is not in this unique list you append the element to the unique list.
The following lined of code illustrate that idea
>>>myList=[1,1,2,2,'a','a','b','b']
>>>uniqueList=[]
>>>for ele in myList:
>>>... if ele not in uniquList:
>>>... uniquList.append(ele)

>>>uniqueList
[1,2,'a','b']
>>>

you know, it works ... but many lines of code to do such a simple thing ... REMOVE the DUPLICATES !!!

Here is the optimum way ... using Sets
simply convert the list to a set and then convert to list again ... lets see how
>>>myList=[1,1,2,2,'a','a','b','b']
>>>uniquList = list( set(myList) )
>>>uniqueList
[1,2,'a','b']

yah ... that's true ... very simple and straightforward :)

Donate