โ† Back

Home โ€บ Output practice โ€บ the comma, not the brackets

Predict the output: the comma, not the brackets

๐Ÿ”ฎ Predict the outputTopic: tuplesDifficulty: easyNo login needed

The snippet

(1) is not a tuple at all โ€” parentheses are just grouping, so that is the integer 1 with cosmetics. One element needs the trailing comma, and the difference shows up the moment you multiply or unpack.

a = (1)
b = (1,)
print(type(a).__name__, type(b).__name__)
print(a * 2, b * 2)

Your prediction

Type exactly what this program prints โ€” one line per print() call:

The output

int tuple
2 (1, 1)

Verified against CPython 3.11 in this repo's build

Line-by-line explanation

  1. a = (1)
    Redundant brackets around an int. a is 1, type int.
  2. b = (1,)
    The comma creates the tuple; brackets only make it readable. b is a one-element tuple.
  3. print(type(a).__name__, type(b).__name__)
    int tuple โ€” the names, not <class 'int'> reprs, because __name__ is a plain string.
  4. a * 2
    Integer multiplication โ†’ 2.
  5. b * 2
    Sequence repetition โ†’ (1, 1). Same *, two different operators, decided by the left operand's type.

The rule you can reuse

t = 1, is a valid tuple without brackets. Need one element? Write the comma. () is the empty tuple, and type(()) proves it.

Why most predictions break here

Writing (1, 1) for a * 2, or expecting type(a) to print tuple. Also: x, = 5 raises TypeError: cannot unpack non-iterable int object โ€” unpacking needs a sequence, e.g. x, = [5].

Quick fire

Same rule, two more shapes. Predict first, then open.

Predict this too
x, = [5]
print(x, type(x).__name__)
print(len(()), len((None,)))

Output
5 int
0 1

One-name unpacking takes the only item โ†’ 5 int, and the two lengths are 0 1 โ€” a one-element tuple has length 1 even when the item is None.

Predict this too
a, b = 1, 2
a, b = b, a
print(a, b)

Output
2 1

The right side builds a tuple first, so swapping needs no temp variable โ†’ 2 1.

Share your result

Challenge a friend โ€” the link contains the question, never the answer:

Copied โœ“

Keep practising this rule

More output questions โ†’ All interactive problems

Questions this page answers

Why not require brackets like lists?

Because the tuple is defined by packing with commas โ€” 1, 2, 3 is a tuple, and brackets exist only for grouping or clarity. Empty tuple is the one case that needs them.

What is a real use of a one-element tuple?

Returning one value from return x, style APIs and slicing-style arguments (a[1,] in numeric libraries), plus any code that must treat "one item" and "many items" as the same shape.