Type to search

5 most common mistakes students make while coding on Python

Introduction

Nowadays, computer programming is a popular field in which many college students are interested. And many of them choose this specialty, even in a form of online studying from courses, although what is important to know is that choosing this path of education has many obstacles.

Most of the time, the ones interested in this field end up using Python, a very powerful computer programming language that has an easily understandable syntax and structure. Although, that can be sometimes misleading, especially to beginners. Learning how to code is way more demanding than your other university subjects. However, if you only know how to program, you will not graduate from university. Therefore, it is important not to forget about other subjects and the importance of grades in these subjects in your diploma. If you have a catastrophic lack of time to perform other tasks, you can seek online help. One example of such a sites is essay writing company Edubirdie, where you can get online help from professional tutors to finish your essay or assignments on time.

You need to fully understand the coding language and the different approaches to overcoming the different challenges. Basically, there is no cheating on your exams because you will constantly have to access different resources. So, the only assistance that you can get is from your teachers on campus.

Meaning that while in school, you will have to deal with numerous errors while coding on Python, and if you want to become a professional programmer, you will need to learn how to avoid making them. Therefore, to help you, we highlighted the 5 common mistakes you need to avoid while coding. So, let’s see what they are.

computer programmingModifying lists while iterating over them

Almost every person that has tried coding has been faced with this problem at least once.

Take a look at the following code:

odd = lambda x : bool(x % 2)

numbers = [n for n in range(20)]

for i in range(len(numbers)):

if odd(numbers[i]):

del numbers[i] # Deleting item from a list while iterating over it

The error happens right here:

Traceback (most recent call last):

File “<stdin>”, line 2, in <module>

IndexError: list index out of range

This problem may look obvious to you, but not to everyone. And the solution to it would be to use comprehensions on the list. By using them, you will end up with:

odd = lambda x : bool(x % 2)

numbers = [n for n in range(20)]

numbers[:] = [n for n in numbers if not odd(n)]

print(numbers)

Output:

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Misusing The __init__ Method

The init is a method used in construction. It is invoked when you allocate memory to a new class object or when a class object is created. It is used for things like setting the values of the members of the class objects. The beginners will often return the value from the init method, but that is not the purpose of it. So, you need to pay more attention to that.

Wrong use of the expression as defaults for arguments

These mistakes are hard to notice because they won’t give you any errors and will work just fine with simple tasks but can cause problems with more complex works. The error happens when you specify a mutable optional function argument. Take a look at the following example:

def myFunc(myArr=[]):

myArr.append(“Hi”)

return myArr

it is just a simple function that is going to add”Hi” at the end of the list. Otherwise, it will return [“Hi”] whenever it is requested without myArr argument. But also take a look at this:

>>>myFunc()

[“Hi”]

>>>myFunc()

[“Hi”, “Hi”]

>>>myFunc()

[“Hi”, “Hi”, “Hi”]

Your output should not be this, but it won’t give you any errors. The default arguments in Python are just evaluated once, and they will work the first time, but on the following call, it will use your existing myArr and won’t initialize it again. To solve it you should do the following:

def myFunc(myArr=None):

if myArr is None:

myArr=[]

myArr.append(“Hi”)

return myArr

and your output should be:

>>>myFunc()

[“Hi”]

>>>myFunc()

[“Hi”]

Using assert statements for validity checks

Assert allows you to easily check any failed execution or condition if needed. So, you can see developers using it to check validity. Although, it should be used just in tests. When the python interpreter is requested with the -o, the assert statements will get deleted from the bytecode, meaning that if you use it in production code to validate it, you won’t execute the block. So, follow the recommendation and use it just for tests.

Mistake:

assert re.match(VALID_EMAIL_REGEXP, email) is not None

Correct:

if not re.match(VALID_EMAIL_REGEXP, email):

raise AssertionError

codingNot needed lambda expression

The python functions are the aggregation of related statements created to perform a logical task. And those functions can be assigned to any variable. But for newbies or programmers that used other languages, this may be unknown. They may end up doing it, for example like this:

def request(self, method, **kwargs):

if method not in (“put”, “post”):

req.get_method = lambda: method.upper()

but the better way would be:

def request(self, method, **kwargs):

if method not in (“put”, “post”):

req.get_method = method.upper

Conclusion:

Almost every student will stumble with those mistakes as soon as they start programming. However, knowing them can make things easier than simply being unaware of them. So, the only thing you can do now is to try and avoid them, and even if they still occur, don’t give up, code as much as possible, and spend as much time as you can on it. All the hard work will pay up in the end.