Next

Types of Errors

  • This lesson explains different types of errors that can occur in Python programs.

  • What is an Error in Python?

    An error is a problem in a program that prevents it from running properly.

    Errors are also called exceptions.

    Why Do Errors Occur?

    Errors occur due to:

    • Mistyped code

    • Wrong logic

    • Invalid input

    • Resource issues (like file not found)

    1.Syntax Errors

    What is a Syntax Error?

    A syntax error occurs when Python cannot understand your code due to wrong syntax.

    ⚠️ These errors are detected before program execution.

Syntax Error Example

This code has a missing colon, causing a syntax error.

# Missing colon at the end
if 5 > 2
    print("Hello Python")  # ❌ SyntaxError
Note:

Common mistakes: missing :, wrong indentation, missing brackets , Must be corrected before running the program

  • 2.Runtime Errors

    What is a Runtime Error?

    A runtime error occurs during program execution.
    The code is syntactically correct but fails when running.

Runtime Error Example

Dividing by zero causes a runtime error.

x = 10
y = 0
print(x / y)  # ❌ ZeroDivisionError
  • 3.Logical Errors

    What is a Logical Error?

    A logical error occurs when a program runs without crashing but gives wrong output.

Logical Error Example

This program tries to calculate the average but uses wrong formula.

marks = [80, 90, 70]
average = sum(marks) * len(marks)   # ❌ Logical error
print("Average:", average)

✅ Correct formula:
average = sum(marks) / len(marks)
Next