How to declare an array in Python?

[toc] Have you been wondering – “How to declare an array in Python?” Well, if your answer is yes, then you are at the right place to find your answers; as in this article, we are going to learn about the different ways of declaring an array in Python. Video Walkthrough A Quick Introduction to … Read more

List Head and Tail in One Line Python

❗ Problem Formulation: How to assign the first list element to the variable head and the remaining elements to the variable tail? Let’s have a look at the two most Pythonic solutions to this one-liner programming challenge! πŸ™‚ Method 1: Unpacking and Multiple Assignment Given a list. The most Pythonic way to unpack the first … Read more

Iterative vs. Recursive Binary Search Algorithms in Python

In this article, you’ll learn about a basic algorithm, every computer scientist must know: the binary search algorithm. I have drawn the code from my NoStarch programming introductory book Python One-Liners: Applications Binary Search The algorithm has important practical applications in many basic data structures such as sets, trees, dictionaries, bags, bag trees, bag dictionaries, … Read more

Python __contains__() Magic Method

Syntax and Definition The Python __contains__() magic method implements the membership operation, i.e., the in keyword. Semantically, the method returns True if the argument object exists in the sequence on which it is called, and False otherwise. For example, 3 in [1, 2, 3] returns True as defined by the list method [1, 2, 3].__contains__(3). … Read more

The Shortest Quicksort Algorithm in Python

Quicksort is not only a popular question in many code interviews – asked by Google, Facebook, and Amazon – but also a practical sorting algorithm that is fast, concise, and readable. Because of its beauty, you won’t find many introductions to algorithms that don’t discuss the Quicksort algorithm. In this one-liner tutorial, you’ll learn about … Read more