0% found this document useful (0 votes)
7 views

python_errors_summary

The document provides a summary of common Python errors categorized into three types: Syntax Errors, Runtime Errors, and Semantic Errors. It lists specific examples of each error type along with brief explanations of the issues. This serves as a helpful reference for identifying and correcting common mistakes in Python programming.

Uploaded by

swkim079
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

python_errors_summary

The document provides a summary of common Python errors categorized into three types: Syntax Errors, Runtime Errors, and Semantic Errors. It lists specific examples of each error type along with brief explanations of the issues. This serves as a helpful reference for identifying and correcting common mistakes in Python programming.

Uploaded by

swkim079
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like