The Secrets of Python You Didn't Know | String Concatenation

String concatenation is the process of merging two or more strings into one. While it may seem like a basic problem, in Python, there are multiple ways to achieve string concatenation. A poor choice can lead to performance issues. Method 1: Plus Operator Many languages support using the plus operator for string concatenation, and Python is no exception. Simply add two or more strings together to concatenate them. a = 'Python' b = 'Cuisine' r = a + b # Outputs 'PythonCuisine' Method 2: Using the % Operator Before Python 2.6, the % operator was the only way to format strings, and it can also be used for string concatenation. ...

June 28, 2018 · 3 min · Zhiya

The Evolution of Python String Formatting You Didn't Know

String formatting is a fundamental and frequently used feature in every programming language. Those learning Python are likely familiar with using the % syntax for string formatting. However, to make this common feature more convenient, the language itself has iterated on string formatting methods. Before Python 2.6: % Operator Before Python 2.6, there was only one method for string formatting: the % (also known as the modulo) operator. The % operator supports both unicode and str types of Python strings, similar to the sprintf() method in C. Here’s an example of using % to format a string: ...

June 11, 2018 · 4 min · Zhiya

Essential for Beginners | Python Cheat Sheet Chinese Version

I have compiled a quick reference sheet for Python3’s built-in methods, which includes: Built-in methods List processing methods Dictionary processing methods Tuple processing methods Set processing methods Slicing methods for sequence types A total of over 100 methods. Click on the image—view original—download.

May 31, 2018 · 1 min · Zhiya

A Brief Analysis of Four Types of Queues in Python

A queue is a linear data structure that only allows insertion operations at one end and deletion operations at the other end. Searching for “queue” in the Python documentation reveals that the Python standard library includes four types of queues: queue.Queue, asyncio.Queue, multiprocessing.Queue, and collections.deque. collections.deque Deque is short for double-ended queue. Since both ends can be edited, deque can be used to implement both stacks and queues. Deque supports a rich set of operations, with the main methods illustrated below: ...

May 22, 2018 · 4 min · Zhiya

Do You Really Know How to Use Assertions Correctly?

What is an Assertion Assertions were invented as a debugging tool to check conditions that “should always be true if the code is correct.” For example, if we want to assert that a variable a must be greater than 2, we can write: assert a > 2 When the condition is not met, an AssertionError exception is raised, equivalent to the following code: if not assert_condition: raise AssertionError Since assertions are a debugging tool, Python’s implementation aligns with this philosophy. In Python, the execution of the assert statement depends on the __debug__ variable. The assert statement will only be executed if __debug__ is true. ...

May 7, 2018 · 3 min · Zhiya