Tuple

A tuple is a list of elements:

(17, 3, "My name is Parker")

(Tuples are usually notated with parentheses to differentiate them from arrays.)

Like arrays, tuples are ordered and you can access elements by their index:

cake_tuple = ('angel', 'bundt') cake_tuple[0] # returns: 'angel'

But tuples are immutable! They can't be edited after they're created.

cake_tuple = ('angel', 'bundt') cake_tuple[1] = 'carrot' # raises: TypeError: 'tuple' object does not support item assignment

Tuples can have any number of elements (the 'tu' in tuple doesn't mean 'two', it's just a generic name taken from words like 'septuple' and 'octuple').

(90, 4, 54) (True, False, True, True, False)

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .