Errors in Python Program
Errors in a Python program are divided into two categories:
1. Syntax errors
2. Semantic errors
1.Syntax errors
Name error
This error occurs when a referenced variable is not found. See an example:
>>> print(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
The error occurs because you try to print the value of the variable x without assigning any value to it.
1. Syntax errors
2. Semantic errors
1.Syntax errors
Programming beginners mostly make typographical errors in their programs. Such errors are called syntax errors. Syntax is the set of rules for constructing well-formed expressions or statements in a language. A computer generates a syntax error when an expression or sentence is not well formed. When Python encounters a syntax error in a program, it halts execution with an error message indicating the reason for the error. Following are some of the syntax errors:
Name error
This error occurs when a referenced variable is not found. See an example:
>>> print(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
The error occurs because you try to print the value of the variable x without assigning any value to it.
Syntax error
A very common mistake while writing programs is to omit the required parentheses,as shown below:
>>> print x
File "<stdin>", line 1
print x
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
A very common mistake while writing programs is to omit the required parentheses,as shown below:
>>> print x
File "<stdin>", line 1
print x
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
Indentation error
Indentation is very significant in Python code. Each line of code must begin in the leftmost column, with no leading spaces. The only exception to this rule occurs in control statements and definitions, where nested statements must be indented one or more spaces. An indentation error is raised when there is an incorrect indentation.
See below:
>>> print(x)
File "<stdin>", line 1
print(x)
IndentationError: unexpected indent
Note the extra space between the prompt >>> and print(x).
Type error
Python is a strongly typed programming language. This means that the interpreter checks the data types of all operands before performing any operation. If the type of an operand is not appropriate, the interpreter halts execution with an error message. This error checking prevents a program from attempting to do something that it cannot do. See an example:
>>> 5+'3'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
2.Semantic Errors
Semantics is the set of rules that allows the computer to interpret the meaning of expressions or statements. A semantic error is detected when the action that an expression describes cannot be carried out, even though that expression is syntactically correct. Division by zero is the most common semantic error.
>>> 7/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
Comments
Post a Comment