Concept Flow - Single-element tuple
Start with value
Add comma after value?
Yes No
Create tuple
Result: tuple
This flow shows how adding a comma after a single value creates a tuple, otherwise it remains just the value.
a = (5,) b = (5) print(type(a)) print(type(b))
| Step | Code Line | Action | Value/Type |
|---|---|---|---|
| 1 | a = (5,) | Create single-element tuple with comma | a = (5,), type: tuple |
| 2 | b = (5) | Create value in parentheses without comma | b = 5, type: int |
| 3 | print(type(a)) | Print type of a | <class 'tuple'> |
| 4 | print(type(b)) | Print type of b | <class 'int'> |
| 5 | End | No more code | Execution stops |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| a | undefined | (5,) | (5,) | (5,) |
| b | undefined | undefined | 5 | 5 |
Single-element tuple syntax: Use a comma after the single value inside parentheses: (value,) Without comma, parentheses just group the value: (value) Comma is required to create a tuple with one item Example: a = (5,) is a tuple, b = (5) is int