A syntax error is a type of mistake that occurs when writing or interpreting code. It violates the rules of the programming language, causing the program to fail during compilation or execution. A syntax runtime error is a syntax error that occurs during the execution of the program.
print("Hello, world!)
Common Errors
Missing or mismatched parentheses, brackets, or quotes.
Improper indentation or inconsistent use of tabs and spaces.
Invalid variable names (e.g., starting with a number, containing spaces).
Using reserved keywords as variable names.
Incorrect or missing punctuation, such as colons or commas.
Missing or extra operators (e.g., == vs = for comparison and assignment).
# What error type will this code result in?
name = input('What's your name?') if name == 'bob': print('Nice to meet you, bob')
What is a syntax error?
Which type of error occurs when a program violates the rules of a specific programming language?
What is the purpose of suppressing error messages and advice messages?
Runtime errors
Runtime errors occur during the execution of a program. These errors cause the program to terminate abnormally. Common runtime errors include division by zero, accessing an invalid memory location, and trying to perform operations on incompatible data types.
num = 0result = 10 / num # Division by zeroprint(result)Common errors
Division by zero (ZeroDivisionError).
Accessing an index that's out of range (IndexError).
Trying to convert an invalid string to a number (ValueError).
Accessing a non-existent key in a dictionary (KeyError).
Opening a file that doesn't exist (FileNotFoundError).
Trying to perform an unsupported operation (e.g., appending to an integer).
What type of error occurs when you type input)) instead of input()
When does a syntax error occur?
What type of error occurs when the program's logic is incorrect?
Logical Errors
Logical errors occur when the code runs without any syntax or runtime errors, but it does not produce the expected output or behavior. These errors arise due to flaws in the algorithm or incorrect logical reasoning. Detecting and fixing logical errors can be challenging as the code technically functions correctly, but the desired outcome is not achieved.num1 = 10num2 = 5sum = num1 - num2 # Incorrect operation for additionprint("Sum:", sum)Common errors
Incorrect order of operations in mathematical calculations.
Using the wrong operator (e.g., using + for subtraction).
Off-by-one errors in loops or array indices.
Incorrect handling of conditional statements.
Misunderstanding boolean logic (e.g., using and when or is intended).
What is a logical error?
Which type of error is caused by incorrect if-else conditions?
What is the main difference between syntax and logical errors?