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…
Python has some built-in functions that can make our code very elegant. The zip function is one of them. However, uses of the zip function are not very intuitive for beginners and sometimes they are error-prone.
For example, we have a 2*3 matrix represent by a nested list as following:
matrix = [[1, 2, 3], [1, 2, 3]]
Here comes a popular Python interview question:
How to get the transpose of the above matrix?
A junior developer may write some for-loops to implement it, but a senior developer just needs one line of code:
matrix_T = [list(i) for i in…
Nowadays, every individual can show him or herself to the whole world and becomes an influencer. On the one hand, our opinions are much more influenced by influencers on social media instead of professional journalists on traditional media. On the other hand, the market size of influencer marketing is growing rapidly. It has never been easier to become a millionaire in the Influencer Age.
Ryan Kaji, who is a 9-year-old boy, generated millions of dollars in revenue in a single year from his YouTube channel (reported from CNBC). This was impossible 10 years ago. …
Every Python programmer may have been confused about the mutability of objects. This concept is indeed a little difficult to understand in Python.
Unfortunately, questions related to the mutability of objects are the most popular questions in technical interviews for Python developer. Such as:
The above questions are enough to distinguish between junior and senior Python developers.
To help you upgrade to a senior programmer, this article will explain the relative concepts through 5…