6 Mistakes Every Python Beginner Should Avoid While Coding
By: Pranjal Saxena
Tags: Productivity, Programming, Python, Software Development, Mistakes

1. Version of Python:

This should be one of the main concerns for python programmers as a large number of Python versions are used by programmers across the globe. Two main versions that are used today are Python 2.X and Python 3.X which have some differences in between them, like:

10 / 4

output in python 2.X is 2 output in python 3.X is 2.5

Also while printing a statement:

Print ‘hello world’ #python 2.X Print (“hello world”) #python 3.X

Even some differences are noticed among the sub-versions of Python 3 also like the Python 3.8 supports the Walrus operator(:=) whereas Python 3.7 doesn’t. Also the f-string in Python 3.8 supports “=”.

2. Operating System

Another point to keep in mind while writing python code is the operating system where the code is executed. This is especially important if you are writing code for multiple platforms. One example can be the command for clearing screen on Windows and Linux:

For Windows:

os.system(‘cls’)

For Linux:

os.system(“clear”)

3. Misunderstanding Python Functions

Python has a large number of functions that might do the same work but execute them in a different way. So every programmer needs to understand how a specific function works as using the wrong function might not give you the expected result. For example, in Python, both sort() and sorted() function does the same thing i.e. sorting an array. But they work differently:

list1 = [6,3,8,1,5]

print(list1.sort()) list2 = [9,2,7,6,8]

list2 = sorted(list2)

print(list2)

OUTPUT

None [2, 6, 7, 8, 9]

Here although both the list are sorted, list1 returns none as the sorting is done in-place whereas in the case of sorted(list2) a new list is created and it prints the list properly.

Same goes with the function reverse() and reversed() and also for append() and extend() functions while working with lists.

4. Mistakes in Indentation

Python is very strict about the indentation in the code as it does not rely on brackets to separate the code blocks. That is why even one indentation mistake can break your code and give you unexpected results.

This becomes more serious when you are using multiple functions in the same code file as sometimes it might not give you an indentation error but can become a serious bug in the code.

5. Modifying List Items While Iterating Over

It is a bad idea to modify any list/collection while iterating over it. Let me first show you with an example:

a = [1,2,3,4,5,6,7,8,9] #here I want to remove all numbers those are >= 5 from the list

for b in a:

if b >= 5:

a.remove(b)

print(a)

OUTPUT

[1, 2, 3, 4, 6, 8]

This is because in the case of iterative statements the machine goes through the list a number of times and the original list might give you an expected result when the machine goes through the list the next time.

This mistake can be avoided with list comprehension:

a = [1,2,3,4,5,6,7,8,9]

a = [b for b in a if b<5]

print(a)

OUTPUT

[1, 2, 3, 4]

6. Inappropriate Naming

A Python programmer should make a habit to follow some conventions while coding to make the code more readable and error-free.

While naming a variable one must be very careful as even a single difference can make the variables different. For example, Apples and apples are two different variables.

Many beginner programmers name their functions carelessly which becomes a nightmare for other programmers to understand. That is why naming a function that returns the minimum value among the numbers given returnMinimumValue() instead of min() is advisable.

instead of is advisable. Sometimes names of the models written by a programmer clash with the Python standard library modules. This might cause some serious problems when you are trying to import the standard library module with the same name.

Here is a book on Python programming that I would definitely recommend for all beginners.