Common Mistakes Python Beginners should Avoid
Python is arguably the rising programming language of the decade and is proven to be a very powerful language. I have built so many applications using Python from interactive maps to blockchains. There are so many features in Python, and it is very difficult for beginners to grasp everything at first.
Even if you are a programmer switching from other languages such as C or MATLAB, coding in Python with a higher level of abstraction is definitely a different experience. I wish I had known some Python features earlier, and have highlighted five of the most important ones.
1. List comprehensions — compact codes
Many people would mention the lambda, map, and filter as Python ‘tricks’ that every beginner should learn. While I believe they are functions that we should be aware of, I find them not particularly useful most of the time as they lack flexibility.
Lambda
is a method to compose a function in one line for one-time use. Performance suffers if the functions are called multiple times. On the other hand, map
applies a function to all elements in a list, whereas filter
gets a subset of elements in a set that meets a user-defined condition.
List comprehension is a concise and flexible method to create lists from other lists with flexible expressions and conditions. It is constructed by a square bracket, with an expression or a function that is applied to every element in a list only if the element satisfies a certain condition. It can also be nested to handle nested lists, and is far more flexible than using map and filter.
# Syntax of list comprehension
[ expression(x) for x in aList if optional_condition(x) ]
2. List manipulation — circular lists
Python allows negative indexing where
aList[-1] == aList[len(aList)-1]
. Therefore, we can get the second last element in a list by calling aList[-2]
and so on.
We can also slice lists using the syntax
aList[start:end:step]
, where the starting element is included but the ending element is not. Therefore, calling aList[2:5]
gives [2, 3, 4]
. We can also reverse a list simply by calling aList[::-1]
, and I find this technique very elegant.
A list can be also unpacked into separate elements, or a mix of elements and a sub-list using an asterisk.
3. Zipping and enumerate — for-loops
Zip
function creates an iterator that aggregates elements from multiple lists. It allows traversing lists in parallel in a for-loop and sorting in parallel. It can be unzipped using an asterisk.Enumerate
might look a bit intimidating at first, but becomes very handy in many scenarios. It is an automatic counter that is often used in a for-loop, such that there is no need to create and initialise a counter variable anymore in a for-loop by counter = 0
and counter += 1
. Enumerate and zip are two of the most powerful tools when constructing a for-loop.4. Generator — memory efficiency
Generators are utilized when we intend to calculate a large set of results but would like to avoid allocating the memory needed for all results at the same time. In other words, they generate values on the fly and do not store previous values in memory, and thus we can only iterate over them once.
They are often used when reading large files or generating an infinite sequence using keyword
yield
. I often find it useful in most of my data science projects.5. Virtual environment — isolation
If you could only remember one thing in this article, then it should be the use of virtual environments.
Python applications often use many different packages from various developers with complex dependencies. Different applications are developed using a specific library setting, where results cannot be reproduced using other library versions. There does not exist a single installation that satisfies the requirements of all applications.
conda create -n venv pip python=3.7 # select python version
source activate venv
...
source deactivate
Therefore, it is vital to create separate self-contained virtual environments
venv
for each application, which can be done using pip
or conda
.
Comments