Errors in Python Class 11 CBSE Computer Science: Syntax Errors, Logical Errors and Runtime Errors
Class 11 · Computer Science
Errors in Python (CBSE Class 11 Computer Science)
Writing a program without mistakes is almost impossible. During program development, programmers often make mistakes called errors. Some errors prevent a program from running, while others produce incorrect output. Understanding different types of errors helps programmers identify problems quickly and write efficient programs.
Learning Objectives
- Understand Errors in Python.
- Learn Syntax Errors.
- Learn Logical Errors.
- Learn Runtime Errors.
- Understand Debugging.
- Identify and correct common programming mistakes.
Quick Syntax Reference
| Concept | Purpose |
|---|---|
| Syntax Error | Violates Python grammar rules |
| Logical Error | Program executes but produces incorrect output |
| Runtime Error | Error occurs during program execution |
| Debugging | Finding and fixing program errors |
What is an Error?
An Error is a mistake in a program that causes incorrect execution or prevents the program from running properly.
Types of Errors
Errors
│
├── Syntax Error
├── Logical Error
└── Runtime Error
1. Syntax Error
A Syntax Error occurs when the rules (syntax) of the Python language are violated. The Python interpreter detects these errors before executing the program.
Example 1: Missing Colon
age = 18
if age >= 18
print("Eligible")
Error
SyntaxError
The colon (:) is missing after the if statement.
Correct Program
age = 18
if age >= 18:
print("Eligible")
Example 2: Missing Parenthesis
print("Welcome"
Error
SyntaxError
Common Causes of Syntax Errors
- Missing colon (
:) - Missing parentheses
- Incorrect indentation
- Missing quotation marks
- Misspelled keywords
2. Logical Error
A Logical Error occurs when the program executes successfully but produces an incorrect result because of faulty logic.
Example
length = 10
breadth = 5
area = 2 * (length + breadth)
print(area)
Output
30
The program executes successfully, but the formula is incorrect.
Correct Formula:
area = length * breadth
Correct Output
50
Dry Run Table
| Step | Operation | Result |
|---|---|---|
| 1 | length = 10 | 10 |
| 2 | breadth = 5 | 5 |
| 3 | 2 × (10 + 5) | 30 |
3. Runtime Error
A Runtime Error occurs while the program is executing. The syntax is correct, but an unexpected situation causes the program to stop.
Example 1: Division by Zero
a = 20
b = 0
print(a / b)
Error
ZeroDivisionError
Example 2: Invalid Input
age = int(input("Enter Age: "))
If the user enters:
ABC
Error
ValueError
Comparison of Errors
| Error Type | Detected When | Program Executes? |
|---|---|---|
| Syntax Error | Before execution | No |
| Logical Error | After execution | Yes |
| Runtime Error | During execution | Partially |
Debugging
The process of finding and correcting errors in a program is called Debugging.
Steps of Debugging
Write Program
↓
Run Program
↓
Identify Error
↓
Correct Error
↓
Run Again
↓
Correct Output
Think Like a Programmer
Identify the type of error.
| Program | Error Type |
|---|---|
| if x > 5 | Syntax Error |
| 10 / 0 | Runtime Error |
| Area = 2(l+b) | Logical Error |
Output Prediction Challenge
Will the following program execute successfully?
x = 10
y = 0
print(x // y)
Click to View Answer
No. It raises a ZeroDivisionError because division by zero is not allowed.
Real-Life Example
- Syntax Error: Writing an English sentence with incorrect grammar.
- Logical Error: Solving a mathematics problem using the wrong formula.
- Runtime Error: A car running out of fuel during a journey.
Common Beginner Mistakes
- Forgetting the colon after
if,for, orwhile. - Incorrect indentation.
- Division by zero.
- Using incorrect formulas.
- Providing invalid input to
int()orfloat().
Visual Memory Box
| Error | Remember |
|---|---|
| Syntax Error | Grammar Mistake |
| Logical Error | Wrong Logic |
| Runtime Error | Occurs While Running |
Memory Trick
- S → Syntax → Stop Before Execution
- L → Logical → Wrong Logic
- R → Runtime → Running Error
Exam Tips
- Know all three error types with examples.
- Learn the difference between logical and runtime errors.
- Remember that debugging means finding and correcting errors.
- Practice identifying error types from programs.
CBSE Important Questions
2 Marks
- Define debugging.
- What is a syntax error?
3 Marks
- Differentiate between syntax, logical, and runtime errors.
- Explain runtime errors with examples.
5 Marks
- Explain all types of Python errors with suitable examples and debugging steps.
Practice Yourself
- Write one example of each type of error.
- Correct a program containing syntax errors.
- Write a program that generates a runtime error and then fix it.
- Identify the logical error in a given program.
- Explain the process of debugging.
Frequently Asked Questions (FAQs)
1. What is debugging?
Debugging is the process of identifying and correcting errors in a program.
2. Which error prevents program execution?
Syntax Error.
3. Which error produces incorrect output?
Logical Error.
4. Which error occurs while the program is running?
Runtime Error.
5. Is division by zero a syntax error?
No. It is a Runtime Error (ZeroDivisionError).
Summary
- Python programs may contain Syntax, Logical, or Runtime Errors.
- Syntax Errors violate Python grammar rules.
- Logical Errors produce incorrect results despite successful execution.
- Runtime Errors occur during program execution.
- Debugging is the process of finding and fixing program errors.