Python is a relatively easy language to get started in where there’s plenty of room for the beginner to find their programming feet. However, as with any other programming language, it can be easy to make common mistakes that’ll stop your code from running.
Here are ten common Python programming mistakes most beginners find themselves making. Being able to identify these mistakes will save you headaches in the future.
1 Versions – To add to the confusion that most beginners already face when coming into programming, Python has two live versions of its language available to download and use. There is Python version 2.7.x and Python 3.6.x. The 3.6.x version is the most recent, and the one we’d recommend starting. But, version 2.7.x code doesn’t always work with 3.6.x code and vice versa.
2 The Internet – Every programmer has and does at some point go on the Internet and copy some code to insert into their own routines. There’s nothing wrong with using others’ code, but you need to know how the code works and what it does before you go blindly running it on your own computer.
3 Indents, Tabs and Spaces – Python uses precise indentations when displaying its code. The indents mean that the code in that section is a part of the previous statement, and not something linked with another part of the code. Use four spaces to create an indent, not the Tab key.
4 Commenting – Again we mention commenting. It’s a hugely important factor in programming, even if you’re the only one who is ever going to view the code, you need to add comments as to what’s going on. Is this function where you lose a life? Write a comment and help you, or anyone else, see what’s going on.
5 Counting Loops – Remember that in Python a loop doesn’t count the last number you specify in a range. So if you wanted the loop to count from 1 to 10, then you will need to use:
n = list(range(1, 11))
Which will return 1 to 10.
6 Case Sensitive – Python is a case sensitive programming language, so you will need to check any variables you assign. For example, Lives=10 is a different variable to lives=10, calling the wrong variable in your code can have unexpected results.
7 Brackets – Everyone forgets to include that extra bracket they should have added to the end of the statement. Python relies on the routine having an equal amount of closed brackets to open brackets, so any errors in your code could be due to you forgetting to count your brackets; including square brackets.
8 Colons – It’s common for beginners to forget to add a colon to the end of a structural statement, such as:
def guess(self, letter):
And so on. The colon is what separates the code, and creates the indents to which the following code belongs to.
9 Operators – Using the wrong operator is also a common mistake to make. When you’re performing a comparison between two values, for example, you need to use the equality operator (a double equals, ==). Using a single equal (=) is an assignment operator that places a value to a variable (such as, lives=10).
10 Operating Systems – Writing code for multiple platforms is difficult, especially when you start to utilise the external commands of the operating system. For example, if your code calls for the screen to be cleared, then for Windows you would use cls. Whereas, for Linux you need to use clear. You need to solve this by capturing the error and issuing it with an alternative command.
This guide, and many more like it, can be found in Coding Tips, Tricks and Fixes Vol 27 – On sale now!