Skip to main content
Engineering LibreTexts

4.13: One Item Tuples Need a Trailing Comma

  • Page ID
    14446
  • Also, one minor details about tuples: if you ever need to write code about a tuple that has one value in it, then it needs to have a trailing comma in it, such as this:

    oneValueTuple = (42, )
    

    If you forget this comma (and it is very easy to forget), then Python won’t be able to tell the difference between this and a set of parentheses that just change the order of operations. For example, look at the following two lines of code:

    variableA = (5 * 6)
    variableB = (5 * 6, )
    

    The value that is stored in variableA is just the integer 30. However, the expression for variableB’s assignment statement is the single-item tuple value (30, ). Blank tuple values do not need a comma in them, they can just be a set of parentheses by themselves: ().

    • Was this article helpful?