Working with Tuples and Multiple Arguments
When the values appear as arguments to print(), they are supplied as separate arguments rather than as one large number.
Try it: Debug by Tracing Execution
How to debug a program that runs but gives the wrong answer: compare expected and actual output, trace the lines in the order they really run with a variable watch table (or an inserted debug print), find the first line where a value goes wrong, then choose a fix and re-run to check it.
How it works
- State the expected output from the specification, run the program and compare: a difference is the evidence you debug from.
- Trace in execution order, not file order: loops repeat lines, if/elif picks one branch, and the watch table records every variable before each line runs.
- An inserted print('debug: ...', value) shows the same state in the output; it must not change what the program itself prints.
- Look for the first place a value differs from what it should be (an off-by-one range, > instead of >=, a total reset inside the loop, two names for one list, a str from input()). Where the wrong value shows up is only the symptom.
- Change one thing with a reason, re-run, and compare again: a fix that makes an error disappear can still leave the output wrong.
Default run (14 steps): Sum 1..n: Add up the whole numbers from 1 to n (n included) and print the sum. Press Play to trace it line by line. … Finished, but the output is wrong: got sum of 1 to 5 is 10, expected sum of 1 to 5 is 15.
Simplified: The lab's own small Python interpreter runs fixed, hand-written programs (you pick the program, the fix and a value; your text is never executed). Every program the lab can show was run in CPython 3.12 and the lab reproduces its output, its traceback text and its line-by-line trace. Six short programs, one editable value each; input() receives the value you type in the lab. Lines starting with "debug:" are ignored when comparing the output with the expected output.
Loading the simulation…
The Silent Failure
Some programming mistakes stop a program immediately. Others are more difficult: the code runs, produces output, and still does the wrong thing. These are semantic errors. The program follows Python's grammar, but its meaning does not match what the programmer intended.
What do you think happens?
What will print(1,000,000) display?
Reveal answer
1 0 0
Python treats the commas as separators between values. The expression becomes three integers passed as three arguments to print(), and print() places spaces between multiple arguments.
Commas Create Structure
In Python, a comma separates values; it does not format the digits of one integer. The expression 1,000,000 therefore contains three separate integers: 1, 0, and 0. These values form a sequence called a tuple. When the values appear as arguments to print(), they are supplied as separate arguments rather than as one large number.
| Writing | What Python represents | Result when printed |
|---|---|---|
| 1000000 | One integer | 1000000 |
| 1_000_000 | One integer; underscores are ignored in numeric literals | 1000000 |
| 1,000,000 | Three separate integers in a tuple-like sequence | 1 0 0 |
Commas separate values, while underscores can improve the readability of one integer.
Syntax Versus Meaning
A syntax error violates Python's grammar rules. Python catches it immediately and refuses to run the code. Forgetting a colon after an if statement is an example from the source material. A semantic error is different: the code is grammatically correct, so Python can parse and execute it, but the result does not express the programmer's intention.
| Question | Syntax error | Semantic error |
|---|---|---|
| Is the code grammatically valid? | No | Yes |
| Does Python execute the program? | No | Yes |
| Is an error message produced? | Yes | Not necessarily |
| Can output appear? | No | Yes, but it may be incorrect |
Semantic errors are harder to catch because successful execution does not prove that the result is correct.
Multiple Arguments in Action
print(1,000,000)- 1Python reads the comma-separated values as 1, 0, and 0 rather than as one integer.
- 2The three values become separate arguments to
print(). - 3
print()displays each argument with a space between them. - 4The resulting output is 1 0 0.
- The first comma separates 1 from 0.
- The second comma separates 0 from 0.
print()receives three separate arguments.
1 0 0Finding the Meaning of 1,000,000
A programmer expects print(1,000,000) to display one million. Determine what Python actually does.
- 1Inspect the commas
In Python, commas separate values instead of formatting the digits of one integer.
- 2Identify the values
The expression contains three integers: 1, 0, and 0.
- 3Identify the arguments
Those three values are supplied as separate arguments to
print(). - 4Check the display rule
When
print()receives multiple arguments, it places a space between them.
The program prints 1 0 0, not one million.
A Reliable Debugging Trace
When a program runs but its result is surprising, do not treat successful execution as proof that the program is correct. Compare the intended result with the actual output, inspect the values created by the expression, and follow how those values are used. In this case, the divergence appears at the commas: the programmer intends one integer, but Python creates separate values and passes them separately.
- 1Run the program and record what it actually prints.
- 2Compare the output with what you expected.
- 3Inspect expressions containing commas and determine whether they create separate values.
- 4Trace those values into the function call to see whether they become multiple arguments.
- 5Replace the incorrect representation and run the code again.
- 6Verify that the new output matches the intended result.
Expected and Actual Values
The important debugging question is not only whether the program stopped. Ask whether Python created the value you intended. With 1,000,000, the actual intermediate structure is different from the intended one before print() produces any output. Once the values are separate, the final output follows from that earlier interpretation.
1000000
1000000Common Debugging Mistakes
Common mistake Assuming that familiar written number formatting works the same way in Python.
What it looks likeWriting 1,000,000 to represent one million.
Why it failsPython uses commas to separate values, so it reads the expression as 1, 0, and 0.
Do this insteadWrite 1000000 or 1_000_000.
Common mistake Treating a program that runs without an error message as correct.
What it looks likeAccepting the output 1 0 0 because execution completed.
Why it failsSemantic errors can produce output even when the meaning of the code is wrong.
Do this insteadCompare actual output with the expected result every time.
Common mistake Inspecting only the final output instead of tracing the values that produced it.
What it looks likeSeeing three numbers and assuming print() displayed one number incorrectly.
Why it failsThe commas created separate values, which became separate
print()arguments.Do this insteadTrace the expression, the values it creates, and the arguments passed to the function.
Practice and Review
Predict the output of print(1,000,000), then explain why Python produces that result. Next, rewrite the expression so it represents one million as a single integer and verify the output.
Hints
- Count the values separated by commas.
- Remember that
print()places spaces between multiple arguments. - Use either no commas or underscores in the integer literal.
- 1A comma in Python separates values; it does not format the digits of one integer. The expression 1,000,000 therefore represents three values, and
print(1,000,000)displays them as 1 0 0. A syntax error prevents execution because the grammar is invalid. A semantic error allows execution but produces an incorrect result, which makes it harder to notice. Debug semantic errors by comparing expected and actual output, tracing the values created by each expression, and testing the corrected code.
Key Takeaways
- Semantic errors allow code to run while producing results that do not match the programmer's intention.
- Commas separate values in Python, so 1,000,000 becomes three integers rather than one million.
- Multiple comma-separated values passed to
print()are displayed with spaces between them. - Use 1000000 or 1_000_000 to represent one million as one integer.
- Always compare actual output with expected output and trace intermediate values when debugging.