In this short article, I will discuss most common mistake python programmers do, or at least, I personally do.

While working with Tuples

Tuples are immutable and one can not change its values by using tuple ID. integer or string tuples are immutable in nature.

testtuple = (1,2,3)
testtuple[2]=4
print testtuple
Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
   
Traceback (most recent call last):
  File "python", line 1
SyntaxError: invalid syntax

When working with Discussions

Missing parts of an IF statement:
Make sure you are using proper and appropriate colons and indentations when working with IF statements because they result in Syntax and indentation errors.

val = 500
if val > 100
print("value is grater then 100")
Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
Traceback (most recent call last):
  File "/run_dir/repl.py", line 60, in 
    raise EOFError
EOFError
   
Traceback (most recent call last):
  File "python", line 2
    if val > 100
              ^
SyntaxError: invalid syntax

Have you recognised the error i the above code?

Even if the logic is appropriate we still get the error due to forgotten at the end of If statement condition.

val = 500
if val > 100:
print("value is grater then 100")
Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
Traceback (most recent call last):
  File "/run_dir/repl.py", line 60, in 
    raise EOFError
EOFError
   
Traceback (most recent call last):
  File "python", line 2
    if val > 100
              ^
SyntaxError: invalid syntax

Now you you will be able to make sure all the tab spaces provided, and it gives the results that we have expected.

val = 500
if val > 100:
 print("value is grater then 100")
value is grater then 100

Unnamed variables in a relational expression

In some cases due to hurry we forget to define the variables. It may looks like silly one but there are cases who commits such type of mistakes. Executing the values that are not given variables result in Name Error. Silly one but useful.

Mismatch names in a relational expression

Using existing code by copy pasting will be taken care of renaming variables. But it will land you in a new problems sometimes.

While using the malford expressions

While working with relational expressions, we need to be accurate to use appropriate operators like compare equality, double equals to greater than and less than.

Elif statement order of a relational expression

Order of if condition will show impact on the output while working with If statements. The following example of exam grades, will give you the incorrect results due to improper order of conditions.

marks = 93
print("student's Grade")
if marks >=50:
 print("Grade E")
elif marks >= 60:
 print("Grade c")
elif marks >= 80:
 print("Grade B")
elif marks >= 90:
 print("Grade A")
else:
 print ("Grade f")
student's Grade
Grade E

In the above code even though the logic is correct according to our perception, but that’s not the same while executing. It shows the 93 marks equal to grade E but it is not true. Now we can get to know the importance of conditions order.

Following is the conditional order which will work as you expected.

marks = 74
print("student's Grade")
if marks >=90:
 print("Grade A")
elif marks >= 80:
 print("Grade B")
elif marks >= 70:
 print("Grade C")
elif marks >= 60:
 print("Grade D")
elif marks >= 60:
 Print("Grade E")
else:
 print ("Grade f")
student's Grade
Grade C

working with function definitions

Indentation plays a crucial role in executing the python script. The commonly observed mistake is that the developers and beginners often start second instruction before closing the earlier definition.

Python best practices

The professional programmer is the one who use some innovative ways to code to stand out of the crowd. The professional programmer takes less possible instructions to complete the task. Here are the few tricks which help you to become a professional programmer.

Using enumerators while working with a list.
Here below we take an illustration to print fruits by using generic way of executions. The code absolutely works well and gives us the expected result.

Fruits = ['apple','banana','mango','grapes']
i = 0
for fruit in Fruits:
 print(i,fruit)
 i+=1
0 apple
1 banana
2 mango
3 grapes

To write the above code in an innovative way eliminate the unwanted variable ‘i’ to iterate through the list.

Fruits = ['apple','banana','mango','grapes']
for i,fruit in enumerate(Fruits):
 print(i,fruit)
0 apple
1 banana
2 mango
3 grapes

Even though there is no difference in the output but there are slight changes in code and the time takes to execute the code.

Use of ZIP while working with lists

Following illustration is to print values from two or more list with the same length. Let’s execute with the following generic way to get desired results.

students = ['john','vinod','vasu']
grades =['D','A','c']
for i in range(len(students)):
 student = students[i]
 grade = grades[i]
 print(student,grade)
john D
vinod A
vasu c

This is the simple way to solve the problem. But whoever knows how to use ZIP in this scenario will get merit. Here is the result using ZIP.

students = ['john','vinod','vasu']
grades =['D','A','c']
for student, grade in zip(students,grades):
  print(student,grade)
john D
vinod A
vasu c

Swapping variables made easy

We have different algorithms to swap two numbers, and the third variable is the most common used one to swap among all.

Valueone = 15
ValueTwo = 25
print('Before swap',Valueone,ValueTwo)
temp=Valueone
Valueone=ValueTwo
ValueTwo=temp
print('After Swap',Valueone,ValueTwo)
Before swap 15 25
After Swap 25 15

To swap variables in a single line The best available feature in python is using tuple and packing.

Valueone = 15
ValueTwo = 25
print('Before swap',Valueone,ValueTwo)
Valueone, ValueTwo = ValueTwo, Valueone
print('Afterswap',Valueone,ValueTwo)
Before swap 15 25
Afterswap 25 15

The above code has given the expected result but within a one line of code, we have given all the logic we knew to swap. Probably it would be the coolest among all tricks

employees = {
'vinod' : 'hyderabad',
'kiran' : 'vizag',
'harish': 'mumbai'
}
if 'harish' in employees:
 state = employees['harish']
else:
 state = 'Not Found'
print('Michael is from %s state' % state)
Michael is from mumbai state

The same operation can be done by using very few lines of code by using “get”. The following result will give the expected result with shortcodes.

employees = {
'vinod' : 'hyderabad',
'kiran' : 'vizag',
'harish': 'maharashtra'
}
state = employees.get('harish','Unknown')
print('harish is from %s state' % state)
harish is from maharashtra state

Get method within the associative array does the same logic as the previous code.it tries to map with the first key in the associative array and return the value of the mapped key. Here in this above case “state” is the value. If the key in the associative array is not found,the second parameter to the get method acts as default one.

Practice is the best way to master your skill and gives you boost to think in an innovative way. Do experiments to learn new ways. Always be ready to explore new ways what you gain is a priceless experience.

I hope you are clear with the topic now, and soon I will come up with a few more blogs on python. Practice is the best way to master your skill and gives you boost to think in an innovative way. Do experiments to learn other programming languages as well like Node.js from Mindmajix, Linux, and machine learning. Always be ready to explore new ways what you gain is a priceless experience. Happy learning.