Years ago, when I started to learn Python, I had no idea how flexible and elegant it is. After reading and writing lots of code, I like Python more and more. Because even a common operation can have many different implementations. Merging lists is a good example, and there are at least 5 ways to do this. This article will introduce them and show the tricks under the hood.
The simplest way to merge lists in Python is just using the +
operator directly, as the following example:
Furthermore, the +=
operator also supports lists. But, things become interesting…
In Python, a module is a file containing Python code and its name has the suffix — .py
. Generally speaking, putting related classes or functions into a module and separating the whole program into different modules are good programming practice.
However, when a project becomes larger, handling more and more modules would become difficult.
Fortunately, Python is very flexible and there are many good tricks and practice that can help us manage and use modules elegantly. This article will summary them into three key skills. After reading, handling modules would just be a piece of cake for you. 🍰
When…
The singleton is a simple but controversial design pattern. For some classes that only one instance of them is needed, such as logging, settings, caches or device drivers related objects, instantiating more than one instances could introduce many unexpected bugs. Therefore, the idea of singleton, which is restricting the special classes’ abilities to only generate at most one instance, is useful for some scenarios.
On the other side, the singleton pattern is considered an anti-pattern by some developers. Because it will introduce unnecessary restrictions and global variables. Not to mention it reduces the readability of code.
Although controversial, it is…
We all know that Python is an elegant programming language. But everything has weaknesses. Sometimes Python is not as elegant as it should be.
For example, when we need to break out of nested loops as follows:
for a in list_a:
for b in list_b:
if condition(a,b):
break
The break
keyword can only help us break out of the inner-most loop. Can we directly break out of the two nested loops at once? Are there some built-in keywords or tricks for this in Python?
Unfortunately, there is no built-in support for this operation.
As an old saying goes, “comparison is…
Python has three boolean or logical operators: and
, or
, and not
. They are easy-to-use and flexible. However, a coin has two sides. They could be bug-prone operators if we can’t totally understand their features.
For example, I once encountered an annoying bug in my project and it was from one line of code like the following:
duration = du or 3600
My idea was clear:
du
will be assigned to the duration
if it’s a normal number.duration
to 3600 if the variable du
was None
.It looks perfect. Isn’t it?
Not really.
After testing it…
Time flies so fast. My first line of code was written in 2013 when I started my college life. You probably guessed it, I printed “Hello, world” by the C programming language and felt so excited when the code worked. 😄
Now, I’m working as a professional software engineer and have implemented several projects already. Many younger friends asked me about how to learn programming fast or how to write high-quality code.
To be honest, I think I’m not successful and qualified enough to give suggestions to them. Because programming is a life-long journey and there are always lots of…
In the Python world, there are many small tricks that can make a big difference to our code. Especially for a large program, these tricks can keep everything neat and elegant.
The slice operator is one of them. It helps us get items from iterables easily and elegantly. For example, we have a list as follows:
>>> a = [1,2,3,4,5,6]
If we need to get the first, third and fifth items, how to write the code?
A Python newbie could write a for-loop including an if-else statement to filter the items. It works, but there is too much unnecessary code.
…
Last month, a friend, who just started learning Python, asked me a question:
Why do I need to know many ways to do the same task? How about just learning one method and using it every time?
To be honest, I have the same idea as him when I was a beginner. But I changed my mind later. Because in actual work, I need to spend a lot of time reading other developers’ code instead of writing code. Especially for Python projects, everyone may choose a different way to do the same task since Python is so flexible.
Merging dictionaries…
The simplest and fastest way to distinguish junior and senior Python programmers in a technical interview is letting him or her write a decorator. Because mastering the decorator, which is the most magical Python feature of all time, is a milestone for a Python developer.
There are many tips and tricks worth to mention about decorators, but they are scattered around different books or tutorials and some of them may make beginners more confused. This is why I wrote this article.
This article will dive into all the core concepts, techniques and usages of Python decorators by 7 levels. If…
The dictionary (or dict in short) is a core data structure in Python. It stores key-value pairs and handles data efficiently. Creating dictionaries is the first step to take advantage of the powerful data structure.
Creating a dictionary looks like a simple task. However, the real data could be very complicated. One method may not be appropriate for all situations.
Fortunately, Python gives us enough flexibility to create dictionaries in different ways under different scenarios. Being familiar with all the approaches is significant for writing elegant and Pythonic code.
This article will dive into the three methods for creating dictionaries…