0% found this document useful (0 votes)
8 views11 pages

Python5 1 Errors in Python

The document explains the concept of errors in Python programming, categorizing them into three main types: Compilation Errors (Syntax Errors), Runtime Errors (Exceptions), and Logical Errors (Semantic Errors). It details common causes and examples of each error type, highlighting the challenges in identifying and debugging them. Additionally, it provides a comparison of when these errors occur, their impact on program execution, and their difficulty level in debugging.

Uploaded by

gorleyogeswari
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)
8 views11 pages

Python5 1 Errors in Python

The document explains the concept of errors in Python programming, categorizing them into three main types: Compilation Errors (Syntax Errors), Runtime Errors (Exceptions), and Logical Errors (Semantic Errors). It details common causes and examples of each error type, highlighting the challenges in identifying and debugging them. Additionally, it provides a comparison of when these errors occur, their impact on program execution, and their difficulty level in debugging.

Uploaded by

gorleyogeswari
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

Errors In Python

- By Jagannadham Ch.
What is an Error?
• An error is a mistake or fault that occurs in a program and causes the
program to behave abnormally or produce incorrect results.
• Errors can be made by beginners as well as experienced programmers.
• Programming errors are also known as bugs or faults, and the
process of finding and fixing these errors is called debugging.
• Types of Errors
• In general, programming errors are classified into the following three
types:
o Compilation Error (Syntax Error)
o Runtime Error (Exception)
o Logical Error (Semantic Error)

Dept of IT - Jagannadham Ch 2
Compilation Error / Syntax Error
• Compilation errors, also called syntax errors, occur when the rules
(syntax) of the programming language are not followed.
• These errors are detected by the compiler or interpreter before the
program is executed, and they prevent the program from running.
• Syntax errors are very common among beginners and are usually easy to
identify and fix.
• Common Causes of Syntax Errors
o Missing a keyword - while, if, for not written
o Placing a keyword in the wrong position - Missing : after condition
o Missing symbols such as colon (:), comma (,), or brackets( )
o Misspelling a keyword - Whlie, pritn
o Incorrect indentation - No space/tab before block
o Writing an empty block where a statement is required - No statement inside block

Dept of IT - Jagannadham Ch 3
• Missing a Keyword
o A keyword required by Python is omitted

• Placing a Keyword in the Wrong Position


o The keyword is used, but not in the correct order or place

• Missing Symbols (Colon, Comma, or Brackets)


o Required symbols such as : , ,, ( ) are missing.
o (a) Missing colon

Dept of IT - Jagannadham Ch 4
o (b) Missing comma
o (c) Missing bracket
• Misspelling a Keyword
o Python keywords are case-sensitive and must be spelled correctly

• Incorrect Indentation

• Writing an Empty Block Where a Statement Is Required


o Python does not allow empty blocks.
• Missing quotation marks

o In this case, the interpreter stops execution and reports the error.
o After inserting the missing quotation mark, the program runs correctly.

Dept of IT - Jagannadham Ch 5
Runtime / Exceptional Errors
• Runtime errors occur while the program is running, after
successful compilation.
• These errors cause abnormal termination of the program and are
reported as exceptions.
• Runtime errors occur when the program attempts to perform an
operation that is not possible during execution.
• Common Runtime Errors
o Division by zero
o Performing operations on incompatible data types
o Using a variable that has not been defined
o Accessing a list element, dictionary key, or object attribute that does not
exist
o Trying to access a file that does not exist

Dept of IT - Jagannadham Ch 6
• Division by zero

• Misspelled variable name

• Performing operations on incompatible data types

• Accessing a list element, dictionary key, or object attribute that


does not exist

• Trying to access a file that does not exist

Dept of IT - Jagannadham Ch 7
Logical / Semantic Error
• A logical error occurs when the program runs without any error
messages, but produces incorrect or unexpected results.
• These errors are caused by incorrect logic or reasoning in the
program.
• Logical errors are the most difficult to detect and debug, because
the program executes successfully.
• Common Logical Errors
o Using the wrong variable name
o Incorrect indentation level
o Using integer division instead of floating-point division
o Incorrect operator precedence
o Errors in Boolean expressions
o Off-by-one errors and other numerical mistakes

Dept of IT - Jagannadham Ch 8
• Using the wrong variable name

• Incorrect indentation level

• Using integer division instead of floating-point division

Problem:
// performs integer (floor) division, so the decimal part is lost.
• Operator precedence error
o Multiplication has higher precedence than addition, so the expression is
evaluated as
o in python
o If the intended calculation is:

Dept of IT - Jagannadham Ch 9
• Errors in Boolean expressions

• Operator precedence error


Expected to print 1 to 5, but 5 is excluded.

o Off-by-one errors occur when loop limits or index boundaries are


miscalculated, leading to incorrect or runtime errors.

Error Type When it Occurs Program Runs? Easy to Debug


Syntax Error Before execution No Yes
Runtime Error During execution Stops Moderate
Logical Error During execution Yes Difficult

Dept of IT - Jagannadham Ch 10
Next Exception Handling

Dept of IT - Jagannadham Ch 11

You might also like