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 in Python is a good example. We have many choices to do this job and every way is not too bad. We only need to choose one to use, but we have to know all of them to make sure we can understand others’ code. …
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 you understand half of them, reading Python programs containing decorators will be easy. If you understand all of them, designing and writing decorators in Python will be just a piece of cake. …
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 and discuss their pros and cons. After reading, it will be easy for you to choose the best method for a specific usage scenario. …
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 levels from the appearance to the essence. After reading, answering the above questions will become very easy for you. …
Handling dates and time is a common requirement for software developments. However, since dates and time are special data types, it’s could be confusing and error-prone for some operations, such as switching between different date formats or time zones.
Believe it or not, many programmers, no matter how many years they have been working, keep encountering problems about dates or time operations. This is why this article is a must-read one.
This article will explain 5 important operations of dates and time from easy to hard. After reading, handling dates and time in Python will be just a piece of cake for you. …
A set is an unordered collection of unique elements. It is one of the most core data structures in computer science. Like other programming languages, Python has built-in implementations of the set and its operation functions.
Since the features and manipulations of sets are based on the set theory, it’s different from other data structures and may be confusing for beginners. Error-prone programs will be produced easily by a developer who is not familiar with sets.
This article will indicate 5 levels of using the sets in Python and explain it from elementary to profound. After mastering the 5 levels, bugs will leave you away. …
The lambda function, which is also called the anonymous function, is one of the core concepts in functional programming.
Python, which supports multi programming paradigms, also gives us a simple way to define a lambda function.
The template of writing the lambda function in Python is:
lambda arguments : expression
It contains three parts:
lambda
keywordBecause of its simplicity, the lambda functions can make our Python code more elegant in some using scenarios. …
Python has many awesome features that make programs elegant and programming enjoyable. The comprehension is one of them. There are four types of comprehensions in Python:
This article will explain them by simple and readable examples. Now, it’s time to feel their power and master them. 🆒
As its name implies, the list comprehension helps us build a list easily and elegantly. The template of writing a list comprehension is as following: