Lists

Tuples


In [50]:
# empty tuple
t = tuple()
# or
t = ()

In [52]:
# non-empty tuple
t = (0,1)

In [53]:
# get its length
len(t)


Out[53]:
2

In [54]:
# one element tuple
t = (0,)

In [74]:
# create tuples
t1 = (1,2,3)
t2 = (4,5,6)
t = t1+t2
t3 = 6,7,8
print "t = %s and t3=%s" % (t, t3)


t = (1, 2, 3, 4, 5, 6) and t3=(6, 7, 8)

In [62]:
# get the second element
t[1]


Out[62]:
2

In [63]:
# get the nex to last element
t[-2]


Out[63]:
5

In [75]:
# slice the tuple
t[1:4]


Out[75]:
(2, 3, 4)

In [65]:
# this wont work
t.count(3)


Out[65]:
1

In [76]:
# safe as for list
t.index(3)


Out[76]:
2

In [71]:
# min and max
min(t), min(t)


Out[71]:
(1, 1)

In [42]:
# tuple from a list
t = tuple([1,2,3,4])


Out[42]:
(1, 2, 3, 4)

In [ ]:
t + (5,)