Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Python Tricks - Introduction

Nov 18, 2019 • 7 Minute Read

Introduction

As Bruce Eckel famously said:

Life is short—you need Python.

Python is a popular programming language. As the quote alludes to, one of Python's greatest advantages is its ability to improve developers' productivity. This series of guides, Python Tricks, summarizes all kinds of cool skills with enlightening examples. It will help you write code in a more "pythonic-style" way, which means more concise, elegant, and high-quality, without losing any efficiency.

Python tricks are advanced techniques, patterns or philosophies of Python. Some are indeed hackish and have side effects. Although they may not be suitable for your particular scenario, they can improve your thinking and creativity.

As an introduction to this series, I'll explain some prior knowledge and precautions before jumping in. At the end of this guide, you'll find links to the six other guides containing all the tricks.

More About Python

Python is a high-level, interpreted, and dynamic programming language. Python offers a great compromise between development and execution time. It improves development efficiency within acceptable performance loss. With its simplicity of grammar, high-level language features, and rich extensive support libraries, Python helps you to focus more on your own logic.

Python used to be a glue or scripting language to increase productivity. Now it has a complete technology stack, community ecology, and supporting tools and frameworks. With its strengths in conciseness, readability and interactivity, Python also plays an important role in the field of data science and artificial intelligence.

Python Tricks

Python tricks are the summary of advanced techniques, patterns, and philosophy of Python. What's more, they collect many inspiring examples to enlighten you on how and where to use.

This series of guides contains three parts: basic tricks, iterable tricks, and black magic.

  • Basic tricks cover most common advanced python tricks, such as advanced boolean usages, chained operations, and multiple assignment.
  • Iterable tricks cover skills related to iterables, such as generator/iterator, comprehension, map/reduce, lambda, and pack/unpack.
  • Black magic tricks cover uncommon tricks, many of which have side effects. But they are cool and useful for specific scenarios, such as EAFP, sentinel, fractions index to integrate, complex number in 2d index, and dynamic property.

Why Should I Use Tricks?

Tricks make code concise, elegant, and high-quality, without losing any efficiency. In my opinion, tricks obey Python's philosophy: trading maybe a little bit of performance for readability, usability, coding efficiency, and a reduced possibility of bugs.

Or maybe you simply want to try some cool stuff with Python.

Examples

Many code examples in these guides come from LeetCode and Stack Overflow. I have tried my best to includebrief problem descriptions for most cases, but in reality you can focus only on the trick. If you are also interested in the problem and want to go deeper, you can visit LeetCode and Stack Overflow for more information. (For the LeetCode examples, I have marked the problem ID.)

The most valuable section of each guide is Inspiring Examples. Even though you may have already known this trick, you rarely use it because you don't know where to use it. These examples will enlighten you.

In order to keep the code concise, some code snippets may not be complete and runnable.

Some Caveats

As I said here in this post from Stack Overflow regarding whether we should use python tricks like ~:

In my opinion, if it is a code maintained by yourself, you can use any trick to avoid potential bugs or achieve goals easier, because of maybe high readability and usability. But in teamwork, avoid using 'too clever' code, as it may bring troubles to your co-workers.

In Python Tricks, not all tricks are tricky, and not all tricks may be suitable in your scenario. So it's up to you to decide which tricks to use. I have added extra comments about the side effects of tricks to help you decide. After you learn how to use a trick, you should also be able to determine whether to use it.

Who is This For?

If you have mastered basic python skills or transferring from another language, and you want to enhance your Python skills and write a more Pythonic-style code, this guide is for you. It will help you learn how to and where to use these advanced Python tricks. If you want to write more cool or hackish Python code, or just have fun with Python, here you will be enlightened and enjoy it.

Who Isn't This For?

This series of guides is not for Python beginners. I try to make it short and refined, so I only pick the most valuable tricks.

These guides also don't include all the advanced Python features. I won't give a complete tutorial, either. You may need to refer to other tutorials to help you understand.

Thanks

Special thanks to:

  • Stefan Pochmann. Many enlightening codes come from Stefan. I really learned a lot from him.
  • Stack Overflow for the great questions and answers.
  • LeetCode for the algorithm problems. Moreover, LeetCode has a great OJ platform. It's great for functional and performance tests. Many templates and tricks are enlightened by discussions in LeetCode. Thanks to all the code masters.
  • The many other articles and code snippets that I have referred to.

Environment

Version

Python 3.6 or above. But some examples have no version restrictions.

Development Environment

In the learning and debugging stage, it is recommended to use an interactive development environment such as Jupyter Notebook.

Conclusion

We are now ready to start our Python tricks learning journey. You can start with the basic tricks.

This guide is the first in the series of Python tricks guides:

I hope you enjoy these guides. If you have any questions, you're welcome to contact me at recnac@foxmail.com.