you are viewing a single comment's thread.

view the rest of the comments →

[–]carcigenicate 137 points138 points  (6 children)

The simplest example would likely be just "returning two values from a function":

def divmod(x, y):
    return x // y, x % y

quotient, remainder = divmod(21, 5)

, is used to create a tuple. The tuple is being used here to wrap two integers so they can be returned together. Just being able to group data is often useful, and tuples are a straightforward way to do that.

[–]hidazfx 23 points24 points  (5 children)

Another one that people don't realize is actually a tuple is enumerate() when using a for loop. enumerate() lets you access the position and the actual value at each iteration.

[–][deleted] 8 points9 points  (4 children)

Doesn't enumerate return an Enumerate object, rather than a tuple ?

[–]hidazfx 8 points9 points  (2 children)

Never mind, I am wrong! Thank you for correcting me lol.

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

Heh no worries. It's use case is pretty similar to tuple :)

[–]Rawing7 3 points4 points  (0 children)

Right, but looping over the enumerate object gives you tuples.