One major difference between spoken languages and programming languages is that, errors are much more easily forgiven in spoken languages.
However, in programming languages, even a tiny dot or a space character in the wrong place, heck even the lack of a space character can make your program to completely break down or not be able to compile.
This can be a bit stressful to understand and get over in the beginning but don’t worry. Here are the most common Python beginner errors and Python usually has a pretty good feedback mechanism that lets you know which line your error is occuring.
If you’d like to see a related lesson about errors and debugging, you can check out this Python Error Handling lesson.
Here goes the list:
- Your IDE doesn’t read your file paths correctly:
https://google.comSolution: Use r (which stands for raw) in the beginning of your string before the quotation mark. r’https://google.com’
- Something’s wrong with the quotation marks:
You keep typing or pasting a line of code that doesn’t work.Solution: Make sure you got the right type of quotation mark in your code. There are some fancy quotation marks out there that you may want to avoid. You can see an elaborate list here. Maybe there is a mix-up? Also you might wanna make sure you’re using them in pair.
- Syntax error that’s caused by wrong indentation:
In Python space character matters. It’s a way of giving structure to your code and it’s a part of the Python Syntax.
Loops, conditional blocks, try / except statements, break / continue statements, classes and user defined functions all require proper indentation.
Solution: Make sure your indentation is exactly 4 space characters. (If the platform you’re coding doesn’t work properly, try 1 tab character instead.)
Forgetting colons (:):
Again many structures in Python like: Loops, if statements, class definitions, function definitions, lambda functions etc. require colon at the end of the line. This can be a new habit for new programmers and forgetting a necessary colon will cause Python to throw you a syntax error.Solution: Take a minute to make a mental note about which structures require a colon character at the end of the line. You’ll likely rewire your brain and stop making the same mistake.
You’re not used to using try / except statements.
When you’re looping through data and slicing or accessing lists, you might have empty lists or dictionaries which will cause you to get Index Error.
Solution: Implement a proper try / except statement in order to skip the iterations where you’re likely to get an Index Error.
Forgetting parenthesis where they are necessary.:
It can be as simple as forgetting print function’s parenthesis:
Wrong: print new_data
Correct: print (new_data)If you saw a Python 2 example or if you’re coming from another programming language this can be a common error as well.
Solution: Again, just take a moment to make a mental note so you can remember where and when Python needs parenthesis usage. Usually with all the methods and functions
Not being aware of global and local variables.
Side effects: This might not be obvious at all if you’re new to Python. But sometimes the way you create a new list or dictionary can have an impact on both the new and old data.
shallow copy and deep copy.
- Not using comment lines and totally forgetting what parts of code means:
By using # character you can create comments that identify or explain what parts of your code mean what. This lifesaving practice can seem alien or trivial to new programmers. - Here is a simple confusion: Not knowing your Python setup. Python version matters regarding the syntax. Are you using Python 2 or Python 3? At this point Python 3 is recommended since support for Python 2 is phasing out but it can be helpful to make sure you have the right version.
Same applies to the software you’re using to code in Python. If you’re using an IDE it can act different than a text editor and even IDEs can have different behaviors among them.
Solution: Check out your Python version and read about the IDE you decided to use. We suggest Anaconda for simple installation and piece of mind. It’s free and open source and a tutorial can be found here.
- First element is Index 0 and not Index 1:
Again, this can be weird to someone who is just picking up coding in Python.To access list elements, tuple elements, string elements etc. you need to start counting from 0. So, 1st element is index 0 and 2nd element will be index 1 and so on.
i.e.: somelist[0] will give you the first element of “somelist”. - = vs ==:
Python uses == for logical expressions. So every time you’re comparing 2 variables you need to use == rather than =.Correct: if a == b
Wrong: if a = b= on the other hand is an assignment operator and can be used to assign value to variables:
Correct: myage = 30
Wrong: myage == 30It is a very common mistake to use = where == is needed, as your brain is probably very used to using = in similar situations.
Solution: Learn and make a mental note of where == is used in Python.
- Your body signals you with pain. This time it’s a physical error rather than digital. Coding can make you live a very sedimentary life. Don’t forget to keep moving, set interval alarms, take a walk, exercise. We aren’t evolved to sit on a desk for 18 hours a day. If your back, neck and shoulders and what not started hurting since you picked up this amazing hobby called coding, it might be time to listen to your body as it’s trying to tell you something.
Being aware of some of the common beginner mistakes and errors and taking a moment to understand the situation can save you lots of time and effort that can be caused by repetitive errors.
Once you conquer the basic stuff with Python don’t let your efforts stall, continue with a project, keep building stuff and experimenting. If you keep up it’s almost guaranteed that eventually you’ll be building amazing stuff.