Master overflow errors, numerical errors, and software debugging tools to write robust, error-free Python programs.
Occurs when a stored number exceeds the maximum range of its data type. E.g., accessing an array index out of bounds, or a float result too large.
Occurs when a number is lower than the minimum range of its data type. In Python, integers have no size restriction, but floats do.
When there is insufficient memory to store a number, extra digits are cut off. The difference between the true value and the stored value is the truncation error.
Example: Saving π in a variable that stores 3 decimal places → stored as 3.141, truncation error = 0.000592653589...
Three methods of rounding:
If the (n+1)th decimal digit is ≤ 4, truncate. If ≥ 5, truncate and add 1 to the nth digit.
Example: Round off π to 2 decimal places = 3.14; to 4 decimal places = 3.1416
Raise the number to the nearest value at the specified digit. Example: Round up π to 2 decimal places = 3.15
Lower the number to the nearest value at the specified digit. Example: Round down π to 2 decimal places = 3.14
| Method | Stored Value | Error |
|---|---|---|
| Round off | 2.72 | 0.001718171540955 |
| Round up | 2.72 | 0.001718171540955 |
| Round down | 2.71 | 0.008281828459045 |
| Truncation | 2.71 | 0.008281828459045 |
| Syntax Errors | Logical Errors | Run-Time Errors | |
|---|---|---|---|
| Definition | Code cannot be compiled/executed — breaks language rules. | Program runs but produces wrong results due to logic errors. | Program stops unexpectedly during execution. |
| Examples | Missing colons, misspelt keywords, wrong letter case, missing brackets | Wrong sequence, errors in conditional structure, infinite loops | Dividing by zero, square root of negative, array index out of range |
| Detection | IDE detects automatically | Test with test data | IDE shows error message after program stops |
After writing a program, test it systematically. If the test result does not match the expected result, the program contains errors (bugs). Debugging is the process of identifying and fixing these errors.
Using the debug mode of the IDE, you can execute the program step by step and monitor the value of each variable. This method is called a program trace.
A breakpoint is a position where program execution is suspended. When paused, you can view the current values of variables to find errors.
When paused at the breakpoint: avg_mark = 78.5, marks = [98, 79, 56, 68, 77, 93]. The elif branch is executed because 78.5 > 50.
"If debugging is the process of removing software bugs, then programming must be the process of putting them in."
— Edsger Wybe Dijkstra
Debugging is a necessary and normal part of programming. Every programmer encounters bugs — persistence and systematic testing are key.
Answer all 12 questions, then click Submit to see your score.
Click any card to reveal the definition. Click again to flip back.
Filter by difficulty and click 'View Reference Answer' to see model answers.
Size = 400 ÷ 4 = 100 elements. The array num can store 100 floats.
Overflow error. The array has 100 elements (index 0–99). Accessing index 100 exceeds the valid range of the array index, causing an overflow error.
(i) Rounding error. c = √(4+9) = √13 ≈ 3.605551275..., rounded to 2 d.p. = 3.61. (ii) Rounding error = |3.605551275... − 3.61| = 0.004448724...
Breakpoints suspend program execution at a specified line. When paused, we can inspect the current values of all variables in the IDE. By checking whether these values are correct at that point, we can determine if the error occurred before or after the breakpoint. This helps narrow down the location of bugs efficiently.
Trace: D=6, i=0 → B[3]=6%2=0, D=3, i=1 → B[2]=3%2=1, D=1, i=2 → B[1]=1%2=1, D=0, i=3 → loop ends. B = [0,1,1,0] (index 0,1,2,3). Purpose: Convert a decimal integer D to its 4-bit binary representation stored in array B.
i=1: 1+10=11, total=11 | i=2: 4+10=14, total=25 | i=3: 9+10=19, total=44 | i=4: 16+10=26, total=70 | i=5: 25+10=35, total=105 | i=6: 36+10=46, total=151 | i=7: 49+10=59, total=210 | i=8: 64+10=74, total=284 | i=9: 81+10=91, total=375 | i=10: 100+10=110, total=485. Output: 485