Python Errors Summary
1. Syntax Errors
- if True print("Hello") -> Missing colon (:) after if statement
- for i in range(5) print(i) -> Missing colon (:) after for loop
- def func() print("hi") -> Missing colon (:) after function definition
- a = [1, 2, 3 -> Missing closing bracket ]
- print "Hello" -> Missing parentheses in print function
- x == 3 -> Used comparison operator instead of assignment
- def 123func(): pass -> Function name cannot start with a number
- class MyClass -> Missing colon (:) after class definition
- return -> Return statement outside a function
- break -> Break statement outside a loop
2. Runtime Errors
- print(1/0) -> Division by zero (ZeroDivisionError)
- int('abc') -> Invalid integer conversion (ValueError)
- a = [1, 2]; print(a[5]) -> List index out of range (IndexError)
- d = {}; print(d['key']) -> Key not found in dictionary (KeyError)
Python Errors Summary
- open('no_such_file.txt') -> File not found (FileNotFoundError)
- import not_a_module -> Module not found (ModuleNotFoundError)
- len(5) -> Invalid type for len() function (TypeError)
- a = '5' + 2 -> Unsupported operand types for + (TypeError)
- next(iter([])) -> StopIteration on empty iterator
- a = 10; a() -> Int object is not callable (TypeError)
3. Semantic Errors
- def add(a, b): return a - b -> Wrong operation: subtraction instead of addition
- for i in range(1, 10): total += i -> Missing initialization of total
- while x > 0: x += 1 -> Infinite loop due to wrong increment
- def is_even(n): return n % 2 == 1 -> Wrong condition for checking even number
- def multiply(a, b): return a + b -> Wrong operation: addition instead of multiplication
- if name == "john" or "John": -> Incorrect conditional logic
- a = [1, 2, 3]; a.sort(); b = a -> List not copied independently
- max = 0; for x in lst: if x > max: max = x -> Wrong initial value for all-negative list
- for i in range(10): if i=5: break -> Assignment instead of comparison
Python Errors Summary
- if x = 5: -> Assignment inside condition instead of comparison