Basics of Python – Error

In this Page, We are Providing Basics of Python – Error. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Basics of Python – Error

Error

An error (or software bug) is a fault in a computer program that produces an incorrect or unexpected result or causes it to behave in unintended ways. Most bugs arise from mistakes and errors made by people in either a program’s source code or its design. Usually, errors are classified as: syntax error, run-time error, and logical error.

Syntax error

Syntax error refers to an error in the syntax of tokens and/or sequence of tokens that are intended to be written in a particular programming language. For compiled languages, syntax errors occur strictly at compile-time. A program will not compile until all syntax errors are corrected. For interpreted languages, however, not all syntax errors can be reliably detected until run-time.

>>> prin ' Hi '
SyntaxError: invalid syntax 
>>> print " Hi '
SyntaxError: EOL while scanning string literal

Run-time error

A run-time error is an error that can be detected during the execution of a program. The code appears to be correct (it has no syntax errors), but it will not execute. For example, if a programmer has written the correct code to open a file using the open ( ) function, and if the file is corrupted, the application cannot carry out the execution of the open ( ) function, and it stops running.

Logical error

A logical error (or semantic error) is a bug in a program that causes it to operate incorrectly, but not terminate abnormally. A logical error produces an unintended or undesired output or other behavior, although it may not immediately be recognized. The logic error occurs both in compiled and interpreted languages.

Unlike a program with a syntax error, a program with a logical error is a valid program in the language, though it does not behave as intended. The only clue to the existence of logic errors in the production of wrong solutions. For example, if a program calculates the average of variables a and b, instead of writing the expression c= (a+b) / 2, one can write c=a+b / 2, which is a logical error.

>>> print a+b / 2 
6 . 5
>>> print ( a+b ) / 2
5 . 0