Tuesday, August 11, 2009

Python lists

Here are some methods which can be used with lists in Python

We will first create a list to represent a bag of flowers:

flowers = ['rock banana', 'swollen finger grass', 'velvet leaf', 'turmeric', 'rock banana']

Let's first determine how many rock banana flowers we have in the list:

>>>flowers.count('rock banana')
2

At what index in the list does 'swollen finger grass' appear?

>>>flowers.index('swollen finger grass')
1

Let's insert a rose in there at index 2
>>>flowers.insert(2, 'rose')
>>>print flowers
['rock banana', 'swollen finger grass', 'rose', 'velvet leaf', 'turmeric', 'rock banana']

Let's say we want to delete the rose
>>>flowers.pop(2)
rose
Notice that the pop() method returns the element. If we just want to delete without returning the element, then we can use the del() built-in function in Python. I have discussed built-in functions in this blog post.

What if we want to remove an element by name. Let's remove 'swollen finger grass' using the remove() method.

>>> flowers.remove('swollen finger grass')
>>> print flowers
['rock banana', 'velvet leaf', 'turmeric', 'rock banana']

We have already spoken about slicing in the blog post on sequences. There are a few more things we can do with slicing, like replacing elements, and removing them as well.

Let's create a simple list of numbers:
numbers = [3, 5, 4, 7, 4]

Let's insert a number at the 3rd position in the list
>>>numbers[2:2] = [10]
>>>print numbers
[3, 5, 10, 4, 7, 4]
Notice that when we use slicing to assign insert into a list, we have to use an iterable for the value we insert. This is the reason we use [10] and not 10

Let's replace some elements of the list
>>> print numbers
[3, 5, 10, 4, 7, 4]
>>> numbers[3:5] = [14, 17]
>>> print numbers
[3, 5, 10, 14, 17, 4]

Let's delete the 4th element from th list
>>> numbers[3:4] = []
>>> print numbers
[3, 5, 10, 17, 4]

Monday, August 10, 2009

Python built in functions

Some built in functions in Python

Let's make a simple list of numbers

>>>numbers = [2,4,3,6,5]

To get the number of elements in numbers, we can use the len() method

>>>len(numbers)
5

To get the max element in the list we can use the max() method

>>>max(numbers)
6

The max() method is not restricted to lists of numbers only. It can work with any iterable.

Just like the max function returns the largest value from an iterable, the min() function returns the smallest value.

>>>min(numbers)
2

Suppose we have a String "Hello" and we want to create a list of the characters of the String, we can use the built-in list() function.

>>>list("Hello")
['H', 'e', 'l', 'l', 'o']

The list() function is not limited to String, we can give it any iterable and it will return a list of the elements of the iterable in the same order as they appear in the iterable.

If we want to delete an element in an iterable, we can use the del function
>>> hello_list = list("Hello")
>>> del hello_list[0]
>>> print hello_list
['e', 'l', 'l', 'o']

We can determine the type of an object using the type() function.
>>> type(hello_list)

Slicing in Python

Python supports slicing of sequences. Just watched a video on Python slicing here.

My notes follow:

Slicing a sequence is the act of taking a sequence and getting back some elements from it. These elements couls be consecutive elements or they could be spaced at an interval.

Let's create a list and then slice it

my_list = [2,3,4,5,6,7,8,9,]

print my_list[2:7]
Will return all elements from the 2nd to the (7-1)st position.
[4, 5, 6, 7, 8]


print my_list[2:]
Will print all elements from the 2nd element to the end of the list.
[4, 5, 6, 7, 8, 9]


We need not get only consecutive elements. They can also be spaced.
print my_list[2:7:2]
Will print elements spaced at a distance of 2 from the 2nd element to the 6th element.
[4, 6, 8]


We can also address the list using -ve indexes.
print my_list[-5:-1]
Will return all the elements starting from the 5th element from the end going upto the element before the 1st element from the end.
[5, 6, 7, 8]


If we want to print from the 5th element from the end to the last element, we need to use
print[-5:]
[5, 6, 7, 8, 9]


Lists using -ve indexes can also be spaced.
print my_list[-5:-1:2]
[5, 7]

Python Sequences

I just viewed some videos on Python sequences and lists at adaptivelearningonline.net . My notes follow:

  • A populated tuple need not be enclosed in parameters. Does that mean that
the print method in
print "Hello", "Again"

takes a tuple of Strings?
  • Even though ['h','e','l','l','o'] and "Hello" are both sequences, we cannot concatenate them, because we cannot concatenate the non list with a list.
  • Sequences can be multiples... "Hello"*3 will result in "HelloHelloHello"
  • Lists support a sort method. This method will take a list of objects and sort the list. This method does not return anything.
  • Python has a sorted() function which takes a String and sorts it. It returns a new string which is the sorted string.

Python Strings

Just watched the basic and advanced tutorials on Python Strings over at adaptivelearningonline.net . Here are my notes on Python Strings.

When we concatenate a String with another object, we will get an Exception if the other object is not a String. In such cases we should convert the other object to a String. There are a few methods which will convert non string objects to Strings. Assuming obj is a non string.

  1. str(obj)
  2. repr(obj)
  3. `obj`
Strings have a find method which will find a substring within a String and return the index where the substring starts.

We can change the case of a String using the upper() and lower() methods. Note that these methods return the changed String and do not change the existing String (since String objects are immutable)

>>> s = "HeLlo"
>>> upper(s)
>>> s.upper()
'HELLO'
>>> s.lower()
'hello'
>>> s
'HeLlo'

Strings also have a join method which can be used to join a list with a String, such that the String delimits every element from the list.

>>> words = ['hello', 'howdy', 'namaste']
>>> s = ':'
>>> s.join(words)
'hello:howdy:namaste'

We can replace characters or parts of a String using the replace() method.

>>> hello = 'hello'
>>> hello.replace('e', 'g')
'hgllo'
>>> hello.replace('hell', 'heaven')
'heaveno'

Wednesday, August 5, 2009

Random learnings 2009-Aug-05

VIM split the screen:
:split
:vsplit

To determine the type of a Javascript object at runtime, use typeof(o)

I changed the DATE_FORMAT and TIME_FORMAT properties of the DjangoJSONEncoder in code.

In Python class attributes are declared in the class definition, wheres instance attributes are declared in the init method.