A tuple
is created by surrounding objects
with ()
's and separating the items with commas
(,
). An empty tuple
is empty
()
's. An interesting question is how Python tells an
expression from a 1-tuple
. A 1-element
tuple
has a single comma:
(1,)
. An expression lacks the comma:
(1)
. A pleasant consequence of this is that an extra
comma at the end of every tuple
is legal; for
example, (9, 10, 56, )
.
Examples:
xy= (2, 3)
personal= ('Hannah',14,5*12+6)
singleton= ("hello",)
The elements of a tuple
do not have to be
the same type. A tuple
can be a mixture of any
Python data types, including other tuple
s.