Debugging and Types of errors in Python
Debugging
Errors occurring in programming are called as bugs.The process of tracking this bugs is called as debugging.
There are three types of errors that can occur while coding : Syntax Error, Runtime Error and Semantic Error.
Syntax Error
The syntax is a defined structure or set of rules while writing a program.
If someone fails to maintain correct syntax ,then it may lead to Syntax Error.
"""
Author : ITVoyagers (https://itvoyagers.in/)
Date : 14th June 2019
Description : Program to explain Syntax Error
"""
>>> a=1+2
>>> a
3
>>> 1+2=a
SyntaxError: can't assign to operator
>>>
In above example a=1+2 is correct syntax where as 1+2=a is incorrect as in first case value is assigned to variable and in second case we are trying to assign a variable to value.
Runtime Error
This type of error does not appear until program is executed.
In this,program is executed successfully without syntax or logical error and this type of error occurs at runtime i.e. while accepting values from user.
Runtime errors are also called as exceptions or interruptions in program.
"""
Author : ITVoyagers (https://itvoyagers.in/)
Date : 14th June 2019
Description : Program to explain Runtime Error
"""
#Program to explain Runtime Error
#runtime.py
a=2
b=3
c=4
d=input("enter a number")
sum=a+b+c+d
print(sum)
OUTPUT
enter a number5
Traceback (most recent call last):
File "C:/Python34/runtime error.py", line 5, in
sum=a+b+c+d
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In above example program is executed successfully and idle displays the output to enter a number,after entering a number input function sends the value to variable d but without converting to int so Runtime error occurs.
Semantic Error
In this program will be executed without any error on prompt but will not give desired output.Such type of error is called Semantic error.
Semantic errors are also called as logical errors.
It is difficult to track such errors as it requires to go backward to look for output and find errors causing incorrect output.
"""
Author : ITVoyagers (https://itvoyagers.in/)
Date : 14th June 2019
Description : Program to explain Semantic Error
"""
#Program to find sum of two digits
num1=int(input("Enter first number"))
num2=int(input("Enter second number"))
add = num1 - num2
print("sum of two nums is ", add )
OUTPUT
Enter first number200
Enter second number57
sum of two nums is 143
In above example sum of two numbers is expected also program is executed without syntax of runtime error, still desired output is not achieved due to logical error.
Instead of “+” symbol “-” is used.
For other python basics related posts:
Best post on programming language and its types – 1 |
Python Programming (Basics) – History & Features |
Python Programming (Basics)- Debugging & Types of errors |
Python Programming (Basics) – Variables, Values, Types & Keywords. |
Best post on built-in functions in python – 1 |
Python Programming (Basics) – Type Conversion |
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions |
Best post on IDLE & script mode in Python – 1 |
Python Programming (Basics)- Operators in Python |
Best post on Order of Operations – Python |
Simple and compound statements of python in easy way |
Best post on conditional statements in python – Part 1 |
Best post on conditional statements in python – Part 2 |
Best post on looping in python (for loop)-Part 1 |
Best post on looping in python (while loop)-Part 2 |
Best post on nested loop in python(looping) -Part 3 |
Best post on infinite loop in python(looping) -Part 4 |
Best post on control statements(break,continue,pass)-1 |
Best post on Function definition & calling in python -1 |
Easy flow of function execution,parameters,arguments-2 |
Easy fruitful & void function,return values,stack diagrams-3 |
Best post on types of function arguments in python- 4 |
Best post on recursive function, scope of variable-5 |
Best post on import and from statement in python – 1 |
Best post on modules and built-in modules math,random & time-2 |
Best post on user defined modules in python-3 |
Best post on string datatype in python – 1 |
Best post immutable string,string operations python-2 |
Best post on string methods in python – 3 |
Best post on list datatype in python – 1 |
Best post on mutable list, list operations python – 2 |
Best post on List methods in python – 3 |
Best post on dictionary datatype in python – 1 |
Best post on dictionary methods and operations-2 |
Best post on tuple datatype in python – 1 |
Best post on tuple operations and immutable tuple- 2 |
Best post on tuple methods and built-in functions-3 |
17 -Python program to demonstrate Button in tkinter and its event in easy way |
New posts coming soon....... |
For other advanced python related posts:
File Systems and File Handling in Python |
Types of file modes and attributes of file object in Python |
How to read,write and append in a file in python? |
Remaining file methods in Python |
File Positions in Python |
Directory in Python and its methods |
Iterator and Iterables in Python |
Exceptions in Python |
Exception Handling in Python (Part I) |
Exception Handling in Python (Part II) |
Regular Expressions |
Metacharacters or Regular Expression Patterns |
Functions in 're' module(Part I)- Match vs Search |
Functions in 're' module(Part II)-findall(), split(), sub() |
Flags for regular expressions(Modifiers) |
GUI programming in Python and Python GUI Library |
What is Tkinter ? |
Layout Manager (Geometry Manager) in Python |
Events and Bindings in Python along with Widget configuration and styling |
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python |
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python |
Canvas widget of tkinter module – Python |
Widgets in tkinter module – Python |
Label, Text, Entry & Message Widget in Python |
Button, Checkbutton & Radiobutton Widget in Python |
Best post on Menu and Menubutton Widget of tkinter |
Best post- Listbox and Scrollbar(Slider) Widget-Python |
Best post on Frame Widget of tkinter in Python |
Best post: message box widget and its methods |
Best post- LabelFrame, Toplevel, PanedWindow widgets |
Best post on Spinbox and Scale Widget in Python |
Best post : Database connectivity in Python (Part 1) |
Best post : Database Connectivity in Python (Part 2) |
Best post on Create and Insert query : Python + MySQL |
Best post on Select Query to Search & Read: Python + MySQL-4 |
Best post on Update query and Delete query : Python + MySQL-4 |
New posts coming soon....... |