Everybody makes mistakes in real life. Similarly, every programmer make mistakes while programming!
Mistakes in programming are called Errors or Bugs.
A Python program can only run if it is free of errors. Otherwise, the translation process stops and you receive an error message. In fact, a program may contain four different types of errors, and we must learn to distinguish between them.
“Syntax” refers to the rules of the language defined by its designers, which determine the structure of the program. Every language has its own syntax. For example, in French, a sentence must begin with a capital letter and end with a period.
In normal text, small syntax mistakes are often tolerated. This can happen (for example in poetry), and we may still understand the text despite grammatical errors. However, in a computer, any syntax error always causes the program to crash and display an error message.
A syntax error are mistakes in writing the code (like grammar mistakes in a language).
During your first weeks of learning, you will certainly encounter many errors and spend a long time fixing them. But professionals can do this much faster. Keep in mind that words and symbols do not have meaning by themselves—they are simply sequences of symbols that are automatically converted into binary numbers.
Therefore, we must be extremely careful to follow strict syntax rules when writing code.
Every detail matters:
Even a small mistake—like forgetting a comma—can change the meaning significantly and cause major problems.
Fortunately, when learning Python (an interpreted language), debugging is relatively easy. But with compiled languages like C++, you must recompile the entire program after every change, even if it is small.
Let’s create a syntax error by removing the last quotation mark (“) in the Hello World program. Now run the code below, It will not execute and will display an error.
print("Hello, World)Let us analyze the error: SyntaxError: EOL while scanning string literal
There are two terms in this message that might be unfamiliar:
String Literal means a piece of text enclosed in quotation marks (” “), such as “Hello, World”.
EOL (End of Line) means the end of a line.
This message indicates that Python reached the end of a line while reading a string literal, but the string was not properly closed.
String literals must always be enclosed within quotation marks (” “), otherwise, a syntax error will occur. Since the closing quotation mark (“) is missing, Python considers everything after the first quotation mark as part of the string literal, including the closing parenthesis ().
The program executes without any error, but the logic is wrong (e.g., doing steps in the wrong order).
# Find average of two numbers a = 10 b = 20 avg = a + b / 2 # ❌ wrong logic print(avg)
Above program will give 20 as output which is incorrect. Correct Output should be 15. Here, real issue is operator precedence. In Python, division (/) is evaluated before addition (+), so:
avg = a + b / 2
is interpreted as:
avg = a + (b / 2)
You need parentheses so that addition happens first:
# Find average of two numbers a = 10 b = 20 avg = (a + b) / 2 print(avg)
Let us understand logical error with another example.
radius = 5 circumference = 3.14 * radius # ❌ wrong logic print(circumference)
Here, formula of circumference is wrong. The formula for circumference is 2πr. So, it should have been 2*3.14*radius in the code.
In this type of error, the program is correct in structure and logic and it also runs without any error message, but the result is unexpected. You intended something different. Finding semantic errors is a difficult task. You will need patience and must examine your program step by step to identify where the logical mistake occurs.
Let us understand semantic errors with the help of an example!
For example, sometimes people write large numbers with commas like 1,000,000.
Python treats this incorrectly as a single number and instead interprets it as separate numbers:
print(1,000,000)
This is called a semantic error—the code runs, but the result is 100, something not what you expected. There is not logical error. The issue is misunderstanding of language meaning.
radius = 5 area = 2 * 3.14 * radius # ❌ this is circumference, not area print(area)
Here, 23.14 * radius is formula of circumference and not of area. The formula of Area is .14×radius2.
The fourth type of error is the runtime error, which appears only when the program is running. Specific conditions must occur for this error to appear. For example, when the program tries to read a file that no longer exists. These errors are called exceptions, because they indicate that something unusual has happened.
You will encounter these errors especially when working on large projects, and later in your learning, you will learn how to handle them properly.
IDLE is the built-in IDE (Integrated Development Environment) that comes with Python. IDLE highlights the incorrect line of code (print(“Hello, World)) in red to help you quickly identify the issue.
One of the most important skills to acquire through practice is putting the program into debugging mode. When your program produces an error or unexpected result, you begin the process of debugging.
All the techniques used to detect and fix errors are called debugging.
This activity may sometimes drive you crazy, but it is always rich in information, as you will learn many things and ideas. This work is similar in many ways to investigative work. You examine a set of facts, form hypotheses, and reconstruct the sequence of events that logically led to the results you observe.
This activity is also closely related to scientific experiment. Debugging is like a —you form a hypothesis, test it, and refine your understanding.
If your prediction is correct, you move one step forward toward a program that works without problems. If your prediction proves wrong, you must create a new hypothesis.
For some people, “programming” and “debugging” seem to be the same thing. In reality, programming is a continuous process of modifying and correcting the program until it behaves as intended.
The idea is to build a program that starts with a basic structure that already does something (and is already working), then gradually add layers of small changes, fixing errors step by step at every stage of development.
For example, you know that Linux is an operating system (a very large program) containing thousands of lines of code. It began as a small, simple program written by Linus Torvalds to test a feature of the Intel 80386 processor.
This does not mean that we want you to learn programming purely through trial and error without structure. When you start a programming project of a certain size, you should define detailed specifications as precisely as possible, upon which the program will be built.
Here are four useful strategies for debuggig:
Sometimes, simply explaining your problem to someone else (or even to yourself!) can help you discover the solution. In the next article we will read about Data Types in Python.
No Comments