June 2009
Beginner to intermediate
504 pages
16h 27m
English
So far, we have seen two kinds of sequence object: strings and
lists. Another kind of sequence is called a tuple. Tuples are formed with the comma
operator
, and typically enclosed
using parentheses. We’ve actually seen them in the previous chapters,
and sometimes referred to them as “pairs,” since there were always two
members. However, tuples can have any number of members. Like lists and
strings, tuples can be indexed
and
sliced
, and have a length
.
>>> t = 'walk', 'fem', 3>>> t ('walk', 'fem', 3) >>> t[0]
'walk' >>> t[1:]
('fem', 3) >>> len(t)
Tuples are constructed using the comma operator. Parentheses are a more general feature of Python syntax, designed for grouping. ...
Read now
Unlock full access