top of page
Writer's pictureLaxmi Murmu

Five Python Mistakes I Made and What They Taught Me




As I spent more time attending Python classes; reading tutorials; taking part in Hackathons I realized that mistakes in the lives of programmers is common. However, those mistakes helped me understand and improve my coding skills. In the following blog, I am sharing the five most common mistakes I made as a beginner. Knowing these pitfalls will help you avoid them and grow as a Python programmer.

1.      Indentation

The 1st thing that you learn as a Python Developer is that Python enforces the indentation model for coding. In Python indentation not only improves readability it also defines the structure of the code. This structure tells the Python interpreter that a group of statements belongs to specific block. If this principle is violated Python will throw the error: IndentationError: unexpected indent

 

Example 1: One statement in the code is not in line with the others. This throws the error: IndentationError: unexpected indent


Example 2: The contents under the if block is not indented correctly. This throws the error: IndentationError: unexpected indent

 In summary Indentation to Python is its identity. Do make sure that when you code you pay close attention to Indentation.


2.      Syntax Errors

Another common error with Beginners is Syntax Errors. Like any programming language Python also has defined rules and structure. If the rules and structure are broken or not followed a SyntaxError is thrown.

 

What is it? A SyntaxError happens when Python cannot understand your code because it doesn't follow the language's rules. It’s like making a grammatical mistake in a sentence, leaving Python unable to process it.

 

Common Causes of Syntax Errors:

Unclosed strings: Forgetting to close a string with a matching quote.

Example: x = "Hello (missing closing quote)

Fix: Ensure all strings have both opening and closing quotes: x = "Hello".

 

Misusing the assignment operator (=): Using = instead of == in conditional statements.

Example: if x = 5: (should be if x == 5:)

Fix: Replace = with == when performing comparisons.

 

Misspelled keywords: Typing keywords like while or if incorrectly.

Example: whille x < 5: (should be while)

Fix: Check and correct spelling.

 

 

Missing brackets, parentheses, or braces: Forgetting to close grouping symbols.

Example:

numbers = [1, 2, 3  # Missing closing bracket

print("Hello"  # Missing closing parenthesis

Fix: Ensure every opening symbol has a corresponding closing symbol.

In summary, like any human language, a programming language has its own syntax, which gives it its unique characteristics. Always understand the syntax before coding. It will go a long way in reducing common errors.


3.      Attribute Errors

For beginners with no coding knowledge or experience, it is difficult to grasp that in the programming world, an object also has a characteristic or property just like anything you see in the real world.

 

Let’s take a real-world example:

Color is an attribute of a car

Address & color are attributes of a house.

 

If someone tries to check the address of a car, they will not get any result since there is no attribute associate with a car. In python this would generate an AttributeError 

 

Below are a few Common Examples which generate an AttributeError 

 

Incorrect object type: Calling a method on an object that doesn’t support it.

Example: 

x = 5

x.append(10)  # Numbers don't have an append method

Fix: Use the correct data type:

x = [5]

x.append(10)  # Works with lists

 

Misspelled attribute or method names:

Example: my_string.upperr() (should be upper)

Fix: Double-check the spelling of the attribute or method.

 

In summary attributes are fundamental to everything in Python. The sooner you understand what attributes are, and how they work, the sooner you’ll have a deeper understanding of Python.

4.      Type Errors and Value Errors

Another common error faced by Beginners is understanding the difference between Type and Value Errors in Python.

 

A ValueError is raised when a built-in operation or function receives an argument that has the right type but an inappropriate value

On the other hand, passing arguments of the wrong type (e.g., passing a list when an int is expected) should result in a TypeError.

Confused let’s understand with an example:

Trying to convert a list to float gives a TypeError since float needs a string or a real number as an argument.


Now if you try to use a string as an argument for float a ValueError is thrown.

 

In summary pay close attention and detail to the error being thrown. They usually provide a good hint as to what is causing the error.


5.      Error Handling and Debugging Techniques


 Beginners are often overwhelmed when they see errors and find their code not working. I spent a lot of time trying to debug by looking at the Traceback logs or using print statements until I discovered the try-except block

Below is an example of an error-handling technique using a try-except block: 

Therefore, everyone needs to invest some time and effort in familiarizing oneself with the common types of errors in Python and learn how to use exception handling to handle them.


Conclusion

Coding means that you will make different mistakes. As you learn a language, you will stop making some mistakes and discover new ones to make. It is only when you acknowledge these mistakes and ensure you don’t repeat them that you truly learn from them.

I hope this blog makes it easier for Python coding beginners to identify and resolve common issues, allowing them to enjoy coding.


Reference


21 views

Recent Posts

See All
bottom of page