We usually focus on all the amazing features and practicalities in Python. But it can also be helpful to know what not to do while programming with Python or learning coding Python. 🙂

Here is a list of major “Faux Pas” in other words “What not to dos” while working with Python. By paying attention to some of the aspects mentioned in this Python tutorial you can step up your coding game and avoid and eliminate bad habits that might keep you from excelling in future.

1- Not taking advantage of one liners:

If you’re new to Python, whether you’re learning everything from scratch or coming from a different language, it may not be obvious to take advantage of Python’s one liners. They are:

a– Lambda: One line user defined function
b– List comprehension: super useful and elegant way of creating lists from scratch in one line.
c– Dict comprehension: same as list comprehension but for dictionaries.
d– One line conditional statements
Bonus: Make use of .format() method: This is technically not a typical one-liner but it tidies up in a similar way.

One line if statement:

if a > 5: 
    v = True 
else: 
    v = False
v = True if a > 5 else False

You can check out detailed usage of Conditional Statements in this tutorial.

List Comprehension:

a=[]
for i in range(x):
    if i%2 == 0:
        a.append(i)
a = [i for i in range(x) if i%2 == 0]

You can check out detailed usage of List Comprehension in this tutorial.

.format() method

amount = 3
fuel = 'gasoline'
print('You have '+str(amount)+' of '+type+' left in your tank.')
print('You have {} of {} left in your tank.'.format(amount, type))

You can check out detailed usage of .format method in this tutorial.

2- Don't neglect using libraries unless it's a deliberate choice for practice or some other reason.

Python has fantastic default and 3rd party libraries. If you need something of a niche, there is likely a library for it. There is usually not a lot of good reasons reinventing the wheel so remember the libraries. If you’re onto something specific or practicing, then power to you!

3- Remember to take advantage of user-defined functions.

Don’t copy paste code over and over. This will make everything more elegant and much less messy.

4- Don’t forget to use tuples if the values of your sequence won't change:

Alex_birthday = [01,01,2020]
Alex_birthday = (01,01,2020)

You can check out detailed usage of tuples in this tutorial.

5- Mutable vs Immutable

Don’t use a mutable object as default parameter of a user defined function:

You may think, you’re going to get a fresh initialization of lst parameter each time you call the function but that’s not the case with Python.

Check this out:

  • f()
  • f()

You’ll get only one “watermelon” the first time you call the function but then you’ll get a “watermelon” “watermelon”.

This is because default parameter is initialized only once during function’s definition. 

def f(lst=[]):
    lst.append("watermelon")
    print(lst)

To get a default empty list each time:

def f(lst=None)
    lst= []
    lst.append("watermelon")
    print(lst)

6- Don't use short variable names 

This is generally a bad practice. If you’re conscious of that fact it might be okay to use low quality variable names for practice or education purposes but in a complex program it will very likely cause you and the readers lots of headache.

7- Avoid wildcard imports. A wildcard import is:

from itertools import *
from itertools import count

8- Make use of enumerate function:

lst = ["a","b","c"]

for i in range(len(lst)):
    j = lst[i]
    print(i,j)
for i, j in enumerate(lst):
    print(i,j)

9- Make use of set() function to get unique values: 

lstin = [1,1,2,3,3,3,4,5])
lstout = []
for i in lstin:
    if i not in lstout:
        lstout.append(i)
lstin = set(lstin)

10- Don't hesitate to ask for help.

It can be weird to ask total strangers for help. But our conscience has been evolving as humans and there are tons of absolutely amazing people on the internet (on platforms such as Stackoverflow, Reddit, Github, Quora, Google Groups etc.). If you can’t find an answer to your problem after enough research and thinking, maybe it’s time to post it online.

Keep in mind it might take some hours or even days before someone answers your question correctly. So you don’t want to wait way too much anyway. Be proactive.

Recommended Posts