Dealing with common errors in Python

Dealing with common errors in Python

▶ Introduction

Python is a popular programming language known for its simplicity and ease of use. It is a high-level, interpreted, and object-oriented language. It is a dynamically typed or loosely typed language. One need not worry about the data types of the variables at the time of declaration. There is also no worry about taking care of the curly braces like that in C++ or C#. Indentation takes care of it in the case of Python. Due to so many advantages of Python, many beginners prefer to choose Python as their first language to start their coding journey.

However, even experienced developers can run into errors when coding in Python. In this article, I will outline some of the most common errors in Python and provide examples of how to debug them.

1️⃣Syntax Error

The syntax is like the grammar of any computer language. There are certain rules which are defined as the syntax for any programming language. Syntax errors occur when the interpreter encounters code that is not in agreement with Python syntax. This can be due to a variety of reasons, such as misspelled keywords, missing parentheses or quotes, or incorrect indentation.

Example:

# Syntax error due to missing colon after the if statement
if x > 5
    print("x is greater than 5")

To fix this error ✅, simply add a colon after the if statement:

# Fixed syntax error
if x > 5:
    print("x is greater than 5")

2️⃣Name Errors

Name errors occur when a variable or function is referenced before it is defined. This can be due to typos or incorrect scope.

Example:

# Name error due to referencing a variable that has not been defined
print(x)

x = 5

To fix this error ✅, simply define the variable before referencing it:

# Fixed name error
x = 5

print(x)

3️⃣Type Errors

Type errors occur when you try to perform an operation on data of the wrong type, such as trying to concatenate a string and an integer.

Example:

# Type error due to trying to concatenate a string and an integer
name = "John"
age = 30

print(name + age)

To fix this error ✅, convert the integer to a string by type casting before concatenating it:

pythonCopy code# Fixed type error
name = "John"
age = 30

print(name + str(age))

4️⃣Index errors

Index errors occur when you try to access an index that is out of range in a list or string. This error also occurs in the case of using for loop with the range function. One should not forget that the range function does not include the rightmost limit.

Example:

# Index error due to trying to access an index that does not exist in the list
my_list = [1, 2, 3]

print(my_list[3])

To fix this error ✅, make sure that the index is within the range of the list:

# Fixed index error
my_list = [1, 2, 3]

print(my_list[2])

5️⃣Attribute errors

Attribute errors occur when you try to access an attribute or method that does not exist for an object. It is important to be well-versed in the methods and attributes associated with different containers like lists, dictionaries, tuples, etc.

Example:

# Attribute error due to trying to access a non-existent attribute
my_list = [1, 2, 3]

print(my_list.name)

To fix this error ✅, make sure that the attribute or method exists for the object:

# Fixed attribute error
my_list = [1, 2, 3]

print(len(my_list))

⏹Conclusion

Python errors can be frustrating, but with the right approach, they can be easily debugged. It is important to identify the bugs before one can start to debug them. By understanding these common errors and using the appropriate debugging techniques, you can quickly identify and fix errors in your Python code. Most of the time, the compiler gives an idea about the probable errors as listed above but the compiler is not intelligent enough to guide us to write the correct logic of a program. That is completely left to the programmers.

Did you find this article valuable?

Support Swati Sarangi by becoming a sponsor. Any amount is appreciated!