Common Mistakes Python Beginners should Avoid


1. List comprehensions — compact codes

Many people would mention the lambdamap, 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.
Photo by Anastase Maragos on Unsplash
# 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.
Photo by Martin Shreder on Unsplash

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.
Photo by Erol Ahmed on Unsplash

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.

5. Virtual environment — isolation

If you could only remember one thing in this article, then it should be the use of virtual environments.
Photo by Matthew Kwong on Unsplash
conda create -n venv pip python=3.7  # select python version
source activate venv
...
source deactivate

Related Articles

Thank you for reading. If you are interested in data science, the following articles might be useful:

4 Common Mistakes Python Beginners should Avoid

I learned them the hard way, but you don’t need to

towardsdatascience.com

Comments

Popular posts from this blog

Flutter for Single-Page Scrollable Websites with Navigator 2.0

A Data Science Portfolio is More Valuable than a Resume

Better File Storage in Oracle Cloud