![]() |
Prothon
Tutorial
|
![]() |
Prothon Home | Previous Page | Tutorial Outline | Next Page |
A Prothon list is a sequential list of objects. Each object can be of any type and you can mix types in a list. You specify a list object (yes, a list is also an object) as a pair of brackets with the items seperated by commas. You may put an extra comma at the end for convenience. Also lines automatically continue when a left bracket is present and the right bracket has not been typed yet. The following are all legal lists: [1, 2, 3] # a simple list with 3 numbers [] # an empty list [1, 'abc', 3.5] # a list with mixed types [1, [2,3], 4] # a list in a list x = [ "one", # a list in multiple lines "two", "three", # extra comma for convenience ] The items in a list can be indexed just like the characters in a string. You can index them with a single number with the first item at the zero position. You can access a slice with a pair of numbers, where the first number indexes the first item and the second number indexes one past the last item. You can use three numbers where the third indicates a step amount and the sign indicates a direction. All of this is identical to the way string indexing works. >>> list = [0,1,2,3,4,5] [0, 1, 2, 3, 4, 5] >>> print list[2], list[-1], list[:2], list[3:], list[-4:-2], list[::-1] 2 5 [0, 1] [3, 4, 5] [2, 3] [5, 4, 3, 2, 1, 0] >>> All of the numeric types and the string type we discussed before are immutable types. That means that you cannot change them once they are created. When you say "x = 1 + 2" you are creating a new integer and not modifying any existing integer. The list type is a mutable type. This means you can change the object itself instead of just using it to help create a new object. There are two ways to change a list object. One is to assign a value to an indexed item or slice of the list, and the second is to use a function that modifies the list. To modify items in a list by assignment, just place an indexed list on the left side of an assignment statement. You can also delete using the "del" command: >>> list = [0,1,2,3,4,5] [0, 1, 2, 3, 4, 5] >>> list[1] = 111 # replace one item 111 >>> print list [0, 111, 2, 3, 4, 5] >>> list [2:] = [222,333] # replace a big slice [222, 333] # with a smaller one >>> print list [0, 111, 222, 333] # it is shorter now! >>> del list[0] # delete one item >>> print list [111, 222, 333] >>>>>> del list[1:] # delete a slice >>> print list [111] >>> There are a number of list functions, just like there are string functions. Some of the list functions modify the list object itself (they mutate the object). These functions all have a special feature in that their name ends with an exclamation mark. This is to remind you that you are changing the list object. When you write functions yourself, you should always put an exclamation mark at the end of your function name if it modifies the object also: >>> list=[1,4,3,5,2,0] [1, 4, 3, 5, 2, 0] >>> Len(list) # Len is the same as for strings 6 >>> list.sort!() # fast sort even on big lists [0, 1, 2, 3, 4, 5] # changes list and returns list >>> list.min() 0 >>> list.max() 5 >>> list.count(2) # count the number of 2's 1 >>> list.index(4) # find the index of the first 4 4 >>> list * 2 # does not change list [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5] >>> list + [6,7] # does not change list [0, 1, 2, 3, 4, 5, 6, 7] >>> list.append!(6) # these change the list [0, 1, 2, 3, 4, 5, 6] >>> list.extend!([7]) [0, 1, 2, 3, 4, 5, 6, 7] >>> list.insert!(2,99) # insert 99 at index 2 [0, 1, 99, 2, 3, 4, 5, 6, 7] >>> list.remove!(5) # remove the first 5 [0, 1, 99, 2, 3, 4, 6, 7] >>> list.pop!() 7 >>> list.pop!(2) 99 >>> print list [0, 1, 2, 3, 4, 6] >>> list.reverse!() [6, 4, 3, 2, 1, 0] >>> Tuples are almost exactly like lists except for one big difference. They are immutable. This means you cannot change the items in a tuple once the tuple has been created, you can only use them in expressions on the right side of an assignment like strings and numbers. You create a tuple like a list except you use parentheses instead of brackets. Also, when there is only one item, you must follow it with a comma. >>> tuple = (1,2,3) (1, 2, 3) >>> tuple[1] 2 >>> (2,) (2) >>> You can do anything with a tuple that you can do with a list except assign to it on the left side of an assignment statement, use the del command on it, or use any mutable function on it. These functions are easy to tell because they are the ones that end with an exclamation mark. >>> tuple = (1,2,3) (1, 2, 3) >>> tuple + (4,5,6) (1, 2, 3, 4, 5, 6) >>> tuple * 2 (1, 2, 3, 1, 2, 3) >>> tuple.min() 1 >>> tuple.max() 3 >>> tuple.count(2) 1 >>> tuple.index(2) 1 >>> A dictionary is a mutable (changeable) collection of objects like the list, but the objects are grouped into pairs of objects and there is no order to them. Each pair consists of a key and a value. Whenever you store anything into the dictionary you must specify both a key and a value. To retrieve a value, you specify just the key, and it looks up the value by finding the pair with the matching key. There cannot be two keys in a dictionary that are equal to each other. If you attempt to add a second key when that key is already present, the first key/value pair is replaced with the new key/value pair. The definition of "equal" for keys varies from type to type but for common types like integer and string the common-sense meaning is valid. One rule about keys is that they must be immutable objects. One of the main reasons tuples exist is so that they can be used as keys since lists are mutable and cannot be used. Dictionaries are entered in Prothon code much like lists and tuples, except that braces are used instead of brackets or parentheses, and items are entered as key/value pairs seperated by a colon: >>> print {1:2, 3:'a', 'b':'c'}, {}, {1:2} {1:2, 'b':'c', 3:'a'} {} {1:2} >>> Dictionaries are accessed to look up values much like lists and tuples, but any immutable object can be used as the index since the index is the key to be searched for. There are no slices in dictionaries so there is no equivalent of multiple indexes. Since dictionaries are mutable they can be on the left side of an assignment statement and the del command can be used to remove key/value pairs. >>> dict = {1:2, 3:'a', 'b':'c'} {1:2, 'b':'c', 3:'a'} >>> dict[1] # simple lookup 2 >>> dict[4] = 5 # adding a new key/value pair 5 >>> print dict {1:2, 'b':'c', 3:'a', 4:5} # not in any order >>> del dict['b'] # delete command >>> print dict {1:2, 3:'a', 4:5} >>> dict[4] = 99 # replacing 4:5 with 4:99 99 >>> print dict {1:2, 3:'a', 4:99} >>> Here are some of the dictionary functions. Once again since dictionary is mutable, there are functions that modify the dictionary objects themselves and so their names end with exclamation marks: >>> dict = {1:2, 3:'a', 'b':'c'} {1:2, 'b':'c', 3:'a'} >>> dict.items() # get list of tuple pairs [(1, 2), ('b', 'c'), (3, 'a')] >>> dict.keys() # get list of keys [1, 'b', 3] >>> dict.values() # get list of values [2, 'c', 'a'] >>> dict.get(1,99) # get value for key 1 2 # if no key 1, default to 99 >>> dict.get(4,99) 99 >>> dict.update!({4:5,6:7}) # merge other dictionary in {1:2, 'b':'c', 3:'a', 4:5, 6:7} >>> dict.popItem!() # remove arbitrary tuple pair (1, 2) >>> dict.popItem!() ('b', 'c') >>> dict.clear!() # erase all contents {} >>> |
Prothon Home | Previous Page | Tutorial Outline | Next Page |