What are common programming errors or 'gotchas' in Python?
Answers
There are 3 main/common programming errors in Python.
1. Syntax errors.
These errors occur due to the violation of rules of the programming language.
The interpreter detects the errors, shows the error message(s) and halts execution.
Common causes include:
- Misspelling a keyword.
- Leaving out vital symbols such as commas, brackets, etc.
- Using a keyword in the wrong place.
- The use of upper/lower case letters wherever it isn't necessary. [eg: print ≠ Print.]
2. Logical errors.
These errors occur due to mistakes in the logic of the source code, leading to undesired results.
These errors are hard to spot as the program can execute successfully, but the result needn't always be right.
Example: Using integer division instead of float type division.
>>>5//4
1 [result]
The most probable expected output from this would be 1.25. But since we've used integer division instead, it shows only the quotient.
Other causes include:
- Assigning a value to the wrong variable.
- Adding two numbers instead of multiplying.
- Leaving out brackets wherever necessary.
3. Runtime errors.
These errors occur during the execution of a program. While inputting values, assigning the wrong value can result in such errors.
Some causes include:
- Dividing a number by 0.
- Adding strings and numbers.