Types of Errors

tags: #python/intro/errors

Python errors can be categorized into 3 general types of errors:

1. Syntax Errors

Syntax errors occur when the source code violates the "grammatical" rules of python. When the code is not written according to these rules, the Python interpreter cannot interpret or execute it properly, resulting in a syntax error.

# Syntax Error: Missing closing parenthesis
print("Hello, World!"
2. Logic Error (Semantic errors)

Logic errors occurs when you have good syntax but produces an incorrect or unintended results due to flawed logic or reasoning in the algorithmic implementation.

# Logic Error: Incorrect condition
x = 10
y = 5

if x > y:
    print("x is greater than y")
elif x > 10:
    print("x is greater than 10")
else:
    print("x is less than y")

# Incorrect condition, should be elif x >= 10:
3. Semantic Error

A semantic error refers to mistakes or inconsistencies in the meaning or understanding of code (i.e., grammatically correct and has logical flow, but does not make sense).

# Semantic error: Incorrect variable usage
radius = 5

circumference = 2 * 3.14 * radious  # Misspelled variable name

print("The circumference is:", circumference)
Powered by Forestry.md