CH 1
CH 1
Chapter 1
Errors in Python
1
Syntax errors:
These occur when a programmer makes mistakes in typing the code's syntax
correctly. The most commonly occurring syntax errors in Python language
are:
Example:
• Missing “)” “(”
• Missing “:”
Semantic Errors:
Semantics refers to the meaning of a statement. So, when a statement doesn’t
make any sense and is not meaningful then we can say it is a Semantic error.
For example,
a=5 #Sattement 1
b=7 #Statement 2
a+b=res #Statement 3
In the above example, we can see a syntax error but actually it is a semantic
error,
2
Logical Error:
Sometimes, we do not get the output we expected after the compilation and
execution of a program. Even though the code seems error-free, the output
generated is different from the expected one. These types of errors are called
Logical Errors.
For example, in place of addition, subtraction is done.
Other Errors
3
4
Q. Put the Tick (√) marks in an immutable datatype?
5
Q. Look at each program and identify the errors in each.
Classify them as either syntax or logic errors. Make sure you can explain
your answer and provide the corrected code.
Example
This program should divide the first number by the second
02 Syntax Missing speech marks at end of number2 = input (“Please enter another
string number”)
Q. Rewrite the following code in Python after removing all errors in order to get the output.
Code:
• PI=3.14
• radius = float(input("Enter the radius of the circle: ")
• area = PI * (radius * 2)
• perimeter = 2 * PI * radius
• print("Area of the circle:" area)
• print("Perimeter of the circle:",perimete)
Correct code:
output:
7
Q. totalAge = age1 + age2 + age3 + age4
numAge = 4
aveAge = totalAge/numAge
print(aveAge)
Type of error:___________________________________________________________
Q. Print "I cant't wait until Thanksgiving! I'm going to eat and eat and eat!"
Type of error:_________________________________________________________
8
NextGen International Coding and Engineering
(NICE) School & College
Worksheet
Type of error:_________________________________________________________
Type of error:______________________________________________________
Type of error:______________________________________________________
9
Q. What kind of errors do the following statements produce?
Type in the following into the SHELL: Is there an error? What is the reason?
print (hello world)
SyntaxError: invalid syntax. Perhaps you forgot a comma?
03 number1
03 number2
04 Sum
10