More

    5 Python Mistakes You Should Avoid

    Some of the most common bugs are hard to find

    Photo by Javier Allegue Barros on Unsplash

    Being so intuitive, Python is a favourite among those just starting with programming. While the syntax is straightforward and the scripts are brief, a few fine points need to be attended to. Overlooking them may lead to your code being broken and giving you a headache.

    In this piece, we’ll look at five common Python programming rookie errors and how to prevent them.

    It is not a good idea to make changes to a collection or list while iterating over it. During iteration, many programmers accidentally remove items from lists. Here’s a case in point:

    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]

    Specifically, the mistake is as follows:

    IndexError: list index out of range

    Solution: The use of list comprehension can help us here.

    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)

    The number and variety of available Python packages and libraries are impressive. If you give your Python module the same name as one already present in the Python Standard Library, you may have a name conflict.

    You should be aware of any name collisions between modules in your code and those in the standard library, such as math.py and email.py.

    You might run into some tricky issues if you import a library and that library attempts to import the module from the Python Standard Library. Because of this, the package may try to import your duplicate module instead of the official one from Python’s standard library.

    For this reason, you should never use the same module names as those found in the Python Standard Library.

    Python recommends closing an opened file after the last of its actions has been completed and the file is no longer in use.

    It’s important to remember that files you open may use system resources and get locked if you don’t shut them after you’re done with them.

    Always using with while reading the files will help you prevent these problems. It will automatically save your changes and close the file when you are done.

    Rather than:

    file_1 = open(‘filename_demo.txt’, ‘w’)
    file_1.write(‘new_data’)
    file_1.close()

    Here’s how:

    with open(‘filename_demo.txt’, ‘w’) as file_1:
    file_1.write(‘new_data’)

    Python comes preloaded with several useful tools. Some of them may do similar tasks; however, they may do so differently. If we, as programmers, don’t fully grasp how a certain function operates, we run the risk of getting unexpected consequences if we use it.

    In Python, we have two distinct functions — sort() and sorted — for arranging items in a set in a certain sorted(). They both serve the same purpose — arranging a set in a certain order. But how these two features operate is distinct.

    list1 = [6, 5, 7, 2, 9, 3]
    print(list1.sort())
    list2 = [6, 2, 8, 5, 3, 11]
    print(sorted(list2))
    None[2, 3, 5, 6, 8, 11]

    What on earth just took place? Whereas both sort() and sorted() are useful, the sorted list is printed by the latter while sort() returns None.

    In this case, sort() modifies the original sequence while sorting (in-place sorting) and returns nothing. Also, the sorted() function always produces a sorted list without altering the input sequence.

    The _init_ function is a special, reserved Python method used for creating objects. It’s called whenever Python creates an instance of a class, letting that instance set its values for the class’s properties and methods.

    When an object of a class is created, this method’s job is to fill in the values of the class’s data members. However, programmers often stray from the intended usage of this _init_ function by having it return a value.

    5 Python Mistakes You Should Avoid Republished from Source https://towardsdatascience.com/5-python-mistakes-you-should-avoid-12104e350030?source=rss----7f60cf5620c9---4 via https://towardsdatascience.com/feed

    Recent Articles

    spot_img

    Related Stories

    Stay on op - Ge the daily news in your inbox