Useful Python Tips
This is a collection of some useful Python tips since I first started learning the language. I found out about them through different projects, online courses, and code challenges. The caveat is that they are a bit all over the place. They range from setting up a virtual environment to more general Python tips. As I learn more about the language, I will update this post.
Before we start, I do want to say ahead of time that I will be using Python 3 and my OS is Ubuntu. When I use the terminal to type in command lines, it will be written for Linux.
deque is a container datatype alternative to
list. The main difference between them is that deque has O(1) time complexity for both
pop from both sides of the queue. While list is O(1) for the end but O(n) for beginning. You can read more about deque here: Deque.
Now let’s see a simple example of deque in action. Suppose you want to remove the first element of a list and move the second element to the end. Then repeat the process until only one element is left and return it.
Merge Two Dictionaries
In Python 3.5 and above, you can merge two dictionaries together in a single expression.
What’s cool about this expression is that you can perform dictionary comprehension on each of those dictionaries before merging them together. For a problem set of a MOOC I took, I had to create a dictionary of a Caesar Cipher program that applies cipher to a message. The amount of shift is how much the letter is moved down the alphabet. A shift of one means that letter key
"a" will be mapped to another letter,
"b" in this case.
The function returns a dictionary of 52 keys, 26 for each lowercase and uppercase letters. What I did was to use dictionary comprehension for each dictionary’s shift while merging both of them together to a new dictionary:
pip is a command line tool for managing your Python packages. If you have Python 3.4 and above, pip should already come pre-installed. If you don’t have it, just open up the terminal and type in:
Then to install packages you can simply type this into the terminal:
You can also install a package with
sudo apt install python3-<package> but pip is a cleaner way to install packages. Pip also install any dependency that a package needs when you run it. Of course you can also install a package manually by downloading the
tar.gz file but it’s a hassle because you have to unzip it and move the files to the intended directory. It’s better to use pip and save yourself the headache.
If you want to see other pip commands, type
virtualenv allows you to create a copy of Python installation on a specific directory. To install it, you just use pip:
The good thing about virtualenv is that packages you installed in the virtual environment will not affect the global packages on your system. For example, you are starting a new project and you need to install packages specific to the project, you can
cd into the project directory and create a virtual environment inside:
When you check inside the project folder, there should be a new directory called
dir_name. It is an isolated Python environment for locally installed packages. To make matter simple, I will use
venv as the name of your virtual environment. You can then use the virtual environment by activating it:
(venv) in the terminal shows that the virtual environment has been successfully activated. Now if you want to install
matplotlib to plot data points for this project, you just install it normally with pip:
And when you are done with installing local packages, you deactivate the virtual environment:
Remember that the virtual environment is simply directory on your system. If you don’t need it anymore, you can remove it: