5 Levels of Using Sets in Python
5 Levels of Using Sets in Python

Introduction

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. …


5 Uses of Lambda Functions in Python
5 Uses of Lambda Functions in Python

Introduction

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:

  • A lambda keyword
  • Arguments that the function will receive
  • An expression whose result is the return value of the function

Because of its simplicity, the lambda functions can make our Python code more elegant in some using scenarios. …


4 Types of Comprehensions in Python
4 Types of Comprehensions in Python

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:

  • list comprehension
  • generator comprehension
  • set comprehension
  • dictionary comprehension

This article will explain them by simple and readable examples. Now, it’s time to feel their power and master them. 🆒

Type 1: List Comprehension

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:

Image for post
Image for post
The template of list comprehension

As the template demonstrated, we can just use one line code to generate a Python list from an iterable.

An example of list…


5 Levels of Understanding Closures in Python
5 Levels of Understanding Closures in Python

Introduction

Functions in Python are first-class citizens. This means we can operate functions like other objects:

  • Pass functions as arguments
  • Assign a function to a variable
  • Return a function from another function (nested functions)

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. ☕️

Level 0: Understand What the Closure Is

The closure is a concept in the context of nested functions. …


4 Built-In Functions to Display Python Namespaces
4 Built-In Functions to Display Python Namespaces
Your Name by Makoto Shinkai

Introduction

When a program becomes large, reading the code becomes a difficult job. Even some simple tasks will be hard. For example:

  • How to quick look all variable names and values for the current script?
  • How to check all variable names and values of a large function or class?
  • How to get a list of valid attributes of a specific object?

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. …


Master Python Scope by the LEGB Rule
Master Python Scope by the LEGB Rule
Photo by Angela Compagnone on Unsplash

Introduction

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:

  • Why Python needs the nonlocal keywords?
  • What is the LEGB rule?
  • Differences between global and nonlocal variables in Python?
  • How many types of variables in Python?

This post will explain the Python scope from elementary to profound. After reading, you will totally master this significant concept.

Four Types of Variables and the LEGB Rule

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…


5 Writing Principles That Help Me Enter the $100 Club on Medium
5 Writing Principles That Help Me Enter the $100 Club on Medium
Photo by Marvel Comics On Wallpaperflare

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.

Principle 1: Content is King

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. …


5 Uses of Asterisks in Python
5 Uses of Asterisks in Python
Starry Night by Vincent van Gogh

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.

Use 1: Multiplication or Exponentiation Operator

The simplest use is to exploit asterisks as infix operators:

  • Single * for the multiplication operation.
  • Double ** for the exponentiation operation.
>>> 2*3
>>> 6
>>> 2**3
>>>…


Image for post
Image for post
Photo by Jason Cooper on Unsplash

Introduction

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. …


3 Ways to Explore a Python Object
3 Ways to Explore a Python Object
Photo by James Harrison on Unsplash

Introduction

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.

1. Get the Type Information of Objects

First of all, when we get an object, we can check which type it belongs to by type() method.

Since the type() method returns an object’s type, it can be used to compared two types. …

About

Yang Zhou

Software Engineer | Investor | Entrepreneur

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store