Tuesday, 15 January 2013

Python Language Lesson: list


Python Language Lesson: list

A list is an ordered set of values. The values that make up a list are
called its elements or items. The items can be of any type, such as
strings or integers. Items of the list do not have to be of the same type,
so you can have a list that contains both strings and integers.
There are several ways to create a new list. The simplest is to enclose
the elements in square brackets:
mylist = [u"Symbian", u"PyS60", u"MobileArt"]
Each item in the list can be accessed by its index in square brackets
after the list name: in the example, mylist[0] refers to the value
"Symbian" and mylist[2] to "MobileArt". We can also assign
values to specific items in the list:
mylist[1] = "Python"
You can access parts of the list easily with the [start:end]
notation for the index – this is called slicing the list. For instance,
mylist[1:2] returns a new list that includes the items ["Pys60",
"MobileArt"]. Optionally, you can leave out either the start index
or the end index. For instance, mylist[:1] returns a new list with
the items ["Symbian", "PyS60"].
To add another element to the end of the list, use the append()
function. For instance, the following line
mylist.append(u"Fun")
would change the list to contain the following items: ["Symbian",
"PyS60", "MobileArt", "Fun"].

No comments:

Post a Comment