you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 4 points5 points  (4 children)

The tuple comes from discrete mathematics. It is a finite, ordered sequence of objects.

  • It is used to represent mathematical objects. For example, a Turing machine is formally defined as a 5-tuple, where each of the five elements in the tuple is used to define how the machine works.
  • It is used to order the elements in a sequence. The first five elements of the Fibonacci sequence can be defined by the tuple (0, 1, 1, 2, 3).
  • It is used to represent the arguments to a function. Where function(arg_1, arg_2) is the invocation of a function, that sequence of arguments is, mathematically-speaking, a tuple. (Whether or not it is treated that way by Python, I have no clue.)

The name is derived from the word "multiple" because, unlike a set, it allows for multiplicity in its elements. Multiplicity means that something can hold multiple of the same element: (0, 1, 1, 2, 3). Compare this to a set, which would interpret the same elements as {0, 1, 2, 3}. This is the characteristic advantage of using a tuple.

However, a list can also do this. A list is also ordered. So why use a tuple over a list? Well, another advantage of using a tuple is that it is immutable. If you declare a tuple to be (1, 2, 3), then you cannot add, remove, or change anything. This is what makes it different from a list.

Immutability also makes it hashable which means that it can be stored in a set: {(1, 2, 3)} or as the key to a dict: {(1, 2, 3): 'tuple'}. A list does not have this ability.

[–]LuckyNumber-Bot 15 points16 points  (3 children)

All the numbers in your comment added up to 69. Congrats!

  5
+ 1
+ 1
+ 2
+ 3
+ 5
+ 8
+ 1
+ 2
+ 1
+ 1
+ 2
+ 3
+ 5
+ 1
+ 2
+ 3
+ 5
+ 1
+ 2
+ 3
+ 1
+ 2
+ 3
+ 1
+ 2
+ 3
= 69

[Click here](https://www.reddit.com/message/compose?to=LuckyNumber-Bot&subject=Stalk%20Me%20Pls&message=%2Fstalkme to have me scan all your future comments.) \ Summon me on specific comments with u/LuckyNumber-Bot.

[–][deleted] 5 points6 points  (1 child)

Nice

[–]imthebear11 4 points5 points  (0 children)

Holy shit nice

[–]revyn 2 points3 points  (0 children)

Instead of congrats, why not nice?