
If you’re getting into the world of programming, Python is often recommended for beginners because it is easier to read and write than many other programming languages.
But of course, everyone still makes mistakes when they are learning. Errors are a normal part of coding, and every programmer, from beginners to professionals, encounters them. Some mistakes are particularly common for beginners to trip over however.
To help you as you get started, we’ll look at some of the most common Python errors beginners make and how to solve them. They’ll be useful to keep in mind on your Python learning journey.
1. Forgetting Colons (:)
One of the most common mistakes is forgetting to add a colon at the end of statements such as if, for, while, and def.
Incorrect:
if application_ready
print(“Welcome to Northcoders!”)
Correct:
if application_ready:
print(“Welcome to Northcoders!”)
It’s easy to focus too much on the condition and its logic, and forget punctuation details. So if your if statement isn’t working, your first check should be to make sure you have colons where they’re needed.
2. Indentation Errors
Python uses indentation to show the code structure, or which lines belong together. Indentation can be created with spaces or tabs. 4 spaces is usually the preferred format. Unlike some other programming languages, indentation is not optional with Python.
Incorrect:
if application_ready :
print(“Welcome to Northcoders!”)
Correct:
if application_ready :
print(“Welcome to Northcoders!”)
Just like with the colon, it’s easy to forget this formatting detail while you’re focusing on all the newer elements of your code, or to mix up spaces and tabs. Make sure to use consistent indentation throughout your code.
3. Misspelling Variable Names
Python treats different spellings as completely different variables, so a simple misspelled word can derail your code.
For example:
student_name = “North”
print(studant_name)
Results in:
NameError: name ‘studant_name’ is not defined
We all make typos, especially when working fast or on longer programs. If something’s not working right, carefully check variable names and use your code editor’s autocomplete feature if available.
4. Using a Variable Before Creating It
You must create a variable before you can use it. Beginners sometimes assume Python knows what the variable should be, but you should make sure every variable has been assigned a value before you try to use it. The order of the code is very important here.
Incorrect:
print(student_name)
student_name = “Alex”
Correct:
student_name = “Alex”
print(student_name)
5. Mixing Strings and Numbers
Beginners often expect Python to convert values like text and numbers automatically, but it won’t always be able to. You can use str() to convert numbers into text, as in the example below.
Incorrect:
completed_challenges = 12
print(“I have completed ” + completed_challenges + ” challenges”)
Result: TypeError
Correct:
completed_challenges = 12
print(“I have completed ” + str(completed_challenges) + ” challenges”)
6. Forgetting Quotation Marks Around Text
Python treats unquoted words as variable names. Remember that text values must always be enclosed in single or double quotation marks.
Incorrect:
bootcamp = Northcoders
Correct:
bootcamp = “Northcoders”
7. Incorrect Comparison Operators
Many beginners confuse = with ==. The symbols look similar, but they have different jobs.
= is to assign a value, and == is to compare values.
Incorrect:
if week = 3:
print(“Backend week”)
Correct:
if week == 3:
print(“Backend week”)
8. Going Beyond a List’s Length
Lists have a fixed number of items, so trying to access an item that doesn’t exist causes an error. A key thing to remember: Python starts counting list positions from 0, not 1!
Incorrect:
modules = [“Python”, “SQL”, “JavaScript”]
print(modules[3])
Result: IndexError
Correct:
print(modules[2])
Even though we’re listing 3 modules, since Python starts counting from 0, in this example:
- modules[0] is Python
- modules[1] is SQL
- modules[2] is JavaScript
- and modules[3] doesn’t exist
9. Forgetting Parentheses in Functions
Functions need parentheses when they are called. Whenever you call a function, add brackets after its name.
Incorrect:
print “Submitting project to GitHub”
Correct:
print(“Submitting project to GitHub”)
10. Ignoring Error Messages
Many new programmers see an error message and immediately feel stuck. That’s understandable, as when everything feels new it can seem like a lot to consider at once. However, the text in the error message can be very helpful.
For example:
NameError: name ‘student_nmae’ is not defined
This tells you exactly what’s wrong: Python cannot find a variable called student_nmae. Now that you know what caused the issue, you can look at this variable specifically. Did you forget to define it? Write it in the wrong order? Or is it simply a typo?
To fix it, you should read the error carefully and look for:
- The error type
- The line number
- The explanation
These clues usually point you directly to the issue.
Getting Started With Python
Making mistakes is a normal part of learning Python. Every error teaches you something new and helps you become a better programmer.
When you encounter an error:
- Read the error message carefully.
- Check your spelling.
- Look for missing colons, brackets, or quotation marks.
- Make sure your indentation is correct.
- Test your code one step at a time.
Remember, even experienced developers spend a lot of time fixing errors. The difference is that they have learned how to use errors as a tool for learning rather than seeing them as failures.
Keep practising, stay curious, and don’t be afraid to make mistakes. Every bug you fix is another step towards becoming a confident Python programmer.If you’re looking to take your Python further, a Northcoders is the perfect way to grow your skills in a supported environment and become a junior developer. Find out more about our courses here.