/* */

Thursday, 24 March 2011

Tuples - Intro

Tuples are immutable sequences and are created simply by separating value with commas;
1,2,3, → you have a tuple!
Usually you'd enclose the tuple in parenthesis:
x = 1,2,3
x
(1,2,3)
() → creates an empty tuple
42, → creates a tuple with 1 value (42,). The added comma specifies its a tuple
note the difference a comma can make:
3x(40+2)
126
3x(40+2,)
(42,42,42)

Wednesday, 23 March 2011

tuple

The tuple function takes a sequence and converts it to a tuple:

tuple([1,2,3])
(1,2,3)
tuple(abc)
('a','b','c')
tuple((1,2,3)) → takes a tuple within a tuple and converts it to one tuple
(1,2,3)

Access tuple elements

x = 1,2,3
x[1]
2 → indexing is the same procedure as lists
x[0:2]
(1,2) → again same as lists, index items from 0 up to 2 (excluding 2)

  • Tuples can be used as keys when mapping.

Tuesday, 22 March 2011

Summary

Sequences - data structure with numbered elements
Membership - whether a value can be found in a sequence
Methods - like functions but are tied to a specific value