Python is a fairly popular language among beginners as it is very easy to use. But behind the easy to understand syntax and shorter codes there are some minute details that everyone must take care of. Because ignoring these details may cause the breaking of your code and can be a reason for your headache.
This article will talk about 6 Mistakes every beginner should avoid while coding in Python.
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 ofmin()
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.
Conclusion
These were a few mistakes that a beginner Python programmer should be aware of and avoid at any cost. Although some of these mistakes might seem very trivial – they can cause big problems in your code if they remain unnoticed.
But the good thing is that if you practice keeping these points in your mind – you can get rid of these mistakes in a very short time and become a better developer.
Note: This article contains an affiliate link. This means that if you click on it and choose to buy the resource I linked above, a small portion of your subscription fee will go to me.
However, the recommended resource is experienced by me and helped me in my data science career journey.
Before you go…
If you liked this article and want to stay tuned with more exciting articles on Python & Data Science – do consider becoming a medium member by clicking here https://pranjalai.medium.com/membership.
Please do consider signing up using my referral link. In this way, the portion of the membership fee goes to me, which motivates me to write more exciting stuff on Python and Data Science.
Also, feel free to subscribe to my free newsletter: Pranjal’s Newsletter.