Changes

Jump to navigation Jump to search
810 bytes added ,  10:20, 6 April 2015
Line 83: Line 83:  
==== List ====
 
==== List ====
 
==== Tuple ====
 
==== Tuple ====
 +
Inmutable, declared by ()
 +
* My_tuple = (3, 2, 1)
 +
You can iterate trough tuples.<br />
 +
You can also refer to the items of a tuple like in a list.<br />
 +
<source lang="python">
 +
My_tuple[0]
 +
3</source><br />
 +
They support multple assignment, usefull for example to iterate trough dictionaries:<br />
 +
<source lang="python">
 +
d = dict()
 +
d['steve'] = 2
 +
d['susan'] = 4
 +
 +
for (k, v) in d.items():
 +
    print k, v
 +
</source>
 +
Tuples are comparable. They compare the leftmost first, then the next and so on...<br />
 +
<source lang="python">
 +
>>> (0, 1, 2) < (5, 1, 2)
 +
True
 +
>>> (0, 1, 30000000) < (0, 3, 1)
 +
True
 +
</source>
 +
This can be used to sort dictionaries by keys.
 +
<source lang="python">
 +
>>> d = {'c':22, 'b':1, 'a':10}
 +
>>> t = d.items()
 +
>>> t
 +
[('c', 22), ('b',1), ('a', 10)]
 +
>>> t.sort()
 +
>>> t
 +
[('a', 10), ('b',1), ('c', 22)]
 +
 
==== Dictionary ====
 
==== Dictionary ====
 
Key - Value pairs. They are called diferent in diferent languages:<br />
 
Key - Value pairs. They are called diferent in diferent languages:<br />

Navigation menu