Python is a straightforward and simple to get the hang of programming language. Numerous fledgling software engineers select python as their most memorable programming language because of its straightforward grammar, effectiveness of purpose and applications.
While python is a basic and easy to understand programming language, there are a few normal errors that python developers make while composing the code. Such normal slip-ups could at times decipher your code, subsequently it's smarter to find out about those mix-ups in advance.
In this article, I've concocted a few normal slip-ups that numerous python developers make. How about we get everything rolling!
Read Also: Is web scraping in Python difficult?
5 Common Python Mistakes and How to Fix Them
1. Modifying a list while iterating over it
It is an exceptionally poorly conceived notion to change a rundown/assortment while repeating over it. Eliminating a thing from a rundown while emphasis is an exceptionally normal error that numerous software engineers make.
Here is an illustration of it:
#Program to remove odd numbers from list
odd = lambda x : bool(x % 2)numbers = [i for i in range(10)]
for i in range(len(numbers)):
if odd(numbers[i]):
del numbers[i] #deleting odd number while iteration
Here’s the error:
Traceback (most recent call last):
File "<string>", line 7, in <module>
IndexError: list index out of range
Solution:
odd = lambda x : bool(x % 2)nums = [i for i in range(10)]
nums[:] = [i for i in nums if not odd(i)]
print(nums)
2. Module Name Clash
Python is exceptionally wealthy concerning bundles and libraries. Thus, you might run into a name conflict between the name of your python module and the module with a similar name accessible in the Python Standard Library.
For instance, you could have a module named math.py or email.py in your code, that could conflict with the standard library module of a similar name.
It could prompt a few testing issues, for example, assume you are bringing in a library that thusly attempts to import the Python Standard Library rendition of the module. Presently, since you likewise have a module with a similar name, the bundle may erroneously import your variant of the module rather than the one that is inside the Python Standard Library.
Hence, you generally ought to try not to utilize the module names as those present in the Python Standard Library to stay away from any vagueness and struggle.
3. Not closing a previously opened file
In python, it is generally a decent practice to close the opened record once every one of the specific tasks have been performed on that document and presently you never again need to deal with it.
Continuously recall, opened records utilize your framework assets and might be locked, on the off chance that you don't close the documents subsequent to utilizing them, they will continue to utilize the framework assets and could keep different projects from utilizing those assets.
To keep away from such circumstances, it is dependably a decent practice to use with while perusing the documents. It generally deals with shutting the document for you.
Model:
Rather than:
file = open('filename.txt', 'w')file.write('new_data')file.close()
Use this:
with open('filename.txt', 'w') as file:
file.write('new_data')
4. Misunderstanding Python Functions
Python has countless underlying capabilities. Some of them could accomplish a similar work yet have an alternate method for executing it.
We, as a developer, need to comprehend how a particular capability fills in as utilizing some unacceptable capability probably won't give us the normal outcomes.
For instance, in python, for arranging an assortment we have two capabilities sort() and arranged(). Both these capabilities have a similar usefulness for example arranging an assortment. Yet, these two capabilities work in an unexpected way.
Example:
list1 = [6, 1, 7, 2, 9, 5]
print(list1.sort())list2 = [6, 2, 8, 5, 3, 11]
print(sorted(list2))
Output:
None[2, 3, 5, 6, 8, 11]
5. Misusing the _init_ method
In Python, _init_ technique is a saved strategy that is utilized as constructors in the python classes. It gets summoned when python designates memory to another class article and it permits the item to introduce the properties/qualities of the class.
The goal of this technique is to introduce the upsides of the information individuals from the class when an object of a class is made. Yet, software engineers at times utilize this strategy to return a worth, which isn't the reason for this _init_ technique and strays it from its genuine reason.
Python is a straightforward and simple to get the hang of programming language. Numerous fledgling software engineers select python as their most memorable programming language because of its straightforward grammar, effectiveness of purpose and applications.
While python is a basic and easy to understand programming language, there are a few normal errors that python developers make while composing the code. Such normal slip-ups could at times decipher your code, subsequently it's smarter to find out about those mix-ups in advance.
In this article, I've concocted a few normal slip-ups that numerous python developers make. How about we get everything rolling!
Read Also: Is web scraping in Python difficult?
5 Common Python Mistakes and How to Fix Them
1. Modifying a list while iterating over it
It is an exceptionally poorly conceived notion to change a rundown/assortment while repeating over it. Eliminating a thing from a rundown while emphasis is an exceptionally normal error that numerous software engineers make.
Here is an illustration of it:
Here’s the error:
Traceback (most recent call last):
File "<string>", line 7, in <module>
IndexError: list index out of range
Solution:
2. Module Name Clash
Python is exceptionally wealthy concerning bundles and libraries. Thus, you might run into a name conflict between the name of your python module and the module with a similar name accessible in the Python Standard Library.
For instance, you could have a module named math.py or email.py in your code, that could conflict with the standard library module of a similar name.
It could prompt a few testing issues, for example, assume you are bringing in a library that thusly attempts to import the Python Standard Library rendition of the module. Presently, since you likewise have a module with a similar name, the bundle may erroneously import your variant of the module rather than the one that is inside the Python Standard Library.
Hence, you generally ought to try not to utilize the module names as those present in the Python Standard Library to stay away from any vagueness and struggle.
3. Not closing a previously opened file
In python, it is generally a decent practice to close the opened record once every one of the specific tasks have been performed on that document and presently you never again need to deal with it.
Continuously recall, opened records utilize your framework assets and might be locked, on the off chance that you don't close the documents subsequent to utilizing them, they will continue to utilize the framework assets and could keep different projects from utilizing those assets.
To keep away from such circumstances, it is dependably a decent practice to use with while perusing the documents. It generally deals with shutting the document for you.
Model:
Rather than:
Use this:
4. Misunderstanding Python Functions
Python has countless underlying capabilities. Some of them could accomplish a similar work yet have an alternate method for executing it.
We, as a developer, need to comprehend how a particular capability fills in as utilizing some unacceptable capability probably won't give us the normal outcomes.
For instance, in python, for arranging an assortment we have two capabilities sort() and arranged(). Both these capabilities have a similar usefulness for example arranging an assortment. Yet, these two capabilities work in an unexpected way.
Example:
Output:
5. Misusing the _init_ method
In Python, _init_ technique is a saved strategy that is utilized as constructors in the python classes. It gets summoned when python designates memory to another class article and it permits the item to introduce the properties/qualities of the class.
The goal of this technique is to introduce the upsides of the information individuals from the class when an object of a class is made. Yet, software engineers at times utilize this strategy to return a worth, which isn't the reason for this _init_ technique and strays it from its genuine reason.