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:
Because 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:
As the template demonstrated, we can just use one line code to generate a Python list from an iterable.
Functions in Python are first-class citizens. This means we can operate functions like other objects:
Based on these, Python supports a powerful technique: closure.
Most of us heard about the closure, but totally understand it and use it well are not easy.
Fortunately, this post will uncover the mystery of closures layer by layer. It’s time to make a cup of coffee and enjoy this technique. ☕️
The closure is a concept in the context of nested functions. …
When a program becomes large, reading the code becomes a difficult job. Even some simple tasks will be hard. For example:
Of course, we can look up the code line after line and keep the names in mind or write them in papers. However, it’s not Pythonic at all.
It’s harder to read code than to write it. — Joel Spolsky
To make our lives easier, Python provides four useful built-in functions to help us display variable names and values of a specific scope conveniently. …
The variables scope (or namespace) is a very fundamental concept in programming languages. Every developer, no matter which language he or she is using, knows the definition of local and global variables. However, things become a little complicated in Python. Following questions appeared lots of times in interviews for Python developers:
This post will explain the Python scope from elementary to profound. After reading, you will totally master this significant concept.
The LEGB rule defines an order in which the Python interpreter retrieves a variable’s name. The four letters represent four types of variables in…
This month is a milestone for my writing career on Medium. I hit the $100 earning goal first time. 🎉🎉🎉
It’s not too much for many top writers, but it makes sense for me since I’m a software engineer rather than a professional writer. I read many posts about writing on Medium and learned a lot during these months. Now, it’s time to return the favour to this amazing community. In this post, I will summarise my experience of success and failure into five principles to share with everyone who likes writing.
There are too much useless and boring contents on the Internet. I usually open a post having a very attractive title but containing garbage content and trash advertisements filling my screen. …
The asterisk, which is known as the multiplication operator, is a commonly used symbol in all programs. It could be enough for us to use it just as a multiplication operator. However, if you are serious about to become a Python expert. It’s time to know how useful and powerful the asterisk is in Python.
This post will explain 5 usage scenarios of asterisks with most understandable examples, from elementary to profound.
The simplest use is to exploit asterisks as infix operators:
*for the multiplication operation.
**for the exponentiation operation.
>>> 6>>> 2**3
The list comprehension is a very Pythonic technique and able to make your code very elegant. However, its syntax is a little confusing especially for new learners and programmers who come from other languages. I read lots of materials about it, but to be honest, none of them can perfectly show a whole picture and how powerful and beautiful the list comprehension is. Therefore, here comes this article. 😊
This post will demonstrate 8 levels of using the list comprehension from elementary to profound. After understanding all the 8 levels, mastering the list comprehension will be just a piece of cake. …
When we get a reference of a Python object, how can we know the information about it? Of course, we can read its source code. However, sometimes we don’t have enough time to read it or even we just get this object from an API and cannot access its source code.
This post will introduce three ways to have a quick check of a Python object.
First of all, when we get an object, we can check which type it belongs to by
type() method returns an object’s type, it can be used to compared two types. …