Not as elegant as it should be

5 Ways To Break Out of Nested Loops in Python
5 Ways To Break Out of Nested Loops in Python
Photo by Johannes Plenio on Unsplash

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

Unfortunately, there is no built-in support for this operation.

As an old saying goes, “comparison is…


To write bug-free programs

4 Facts To Know Before You Use Boolean Operators in Python
4 Facts To Know Before You Use Boolean Operators in Python
Photo by Jari Hytönen on Unsplash

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
  • The variable du will be assigned to the duration if it’s a normal number.
  • Setting the duration to 3600 if the variable du was None.

Not really.

After testing it…


Practise, make mistakes, learn from them and move on.

Top 5 Mistakes I Made When I Was a Newbie in Programming
Top 5 Mistakes I Made When I Was a Newbie in Programming

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…


Small tricks for a big difference

Image for post
Image for post

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]

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.


Know them all and choose your favourite one

Merge Dictionaries in Python
Merge Dictionaries in Python

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 ULTIMATE PYTHON TUTORIAL

Master the most magical feature of Python

7 Levels of Using Decorators in Python
7 Levels of Using Decorators in Python
Photo by YoungScum on Wallhaven

Introduction

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 ULTIMATE PYTHON TUTORIAL

Make the appropriate choice for different situations

3 Ways to Create a Dictionary in Python
3 Ways to Create a Dictionary in Python

Introduction

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…


The Ultimate Python Tutorial

Do more by less code

The Zip Function in Python
The Zip Function in Python

Introduction

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]]

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…


Traditional media is dead and we are in the Influencer Age.

How to Be an Influencer — The 3 C’s
How to Be an Influencer — The 3 C’s
Image: Influencer Yang Zhou standing on the Tower Bridge

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


5 Levels of Understanding the Mutability of Python Objects
5 Levels of Understanding the Mutability of Python Objects
Photo from Ghost of Tsushima

Introduction

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:

  • What is the difference between mutable and immutable objects?
  • Is Python call-by-value or call-by-reference?
  • Can a mutable object contained in an immutable object be modified?

To help you upgrade to a senior programmer, this article will explain the relative concepts through 5…

Yang Zhou

Software Engineer | Investor | Entrepreneur yangzhou1993.medium.com/follow

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