Indirect Recursion in Python

What is the output of this code snippet? Recursion is a powerful tool in your coding toolbox. Understanding it is a key skill on your path to mastery. Stephen Hawking used a concise explanation: “to understand recursion, one must first understand recursion.” This puzzle uses indirect recursion where a function f calls a function g … Read more

Python One Line Conditional Assignment

Problem: How to perform one-line if conditional assignments in Python? Example: Say, you start with the following code. You want to set the value of x to 42 if boo is True, and do nothing otherwise. Let’s dive into the different ways to accomplish this in Python. We start with an overview: Exercise: Run the … Read more

Python One Line Append

Do you want to one-linerize the append() method in Python? I feel you—writing short and concise one-liners can be an addiction! πŸ™‚ This article will teach you all the ways to append one or more elements to a list in a single line of Python code! Python List Append Let’s quickly recap the append method … Read more

How to Parse JSON in a Python One-Liner?

Problem: How to Parse a JSON object as a Python One-Liner? Example: Say, you have pulled a JSON object from a server using the curl command: Source How to parse the resulting JSON object and extract, say, the value “Netherlands” associated to the “country” key? Solution: The following one-liner accomplishes this: The command consists of … Read more

How to Create Inline Objects With Properties? [Python One-Liner]

Problem: How to create a Python object in a single line of code? And how can you associate custom properties to this object in the same line? Example: Say, you want to do something like this pseudocode snippet: However, you cannot do the same in Python because this would create a dictionary: This raises an … Read more

What is the Asterisk / Star Operator (*) in Python?

Asterisk Operator

Many Python coders—even at intermediate skill levels—are often puzzled when it comes to the asterisk character in Python. What does it mean? How does it work? What’s it purpose? This article answers all of those questions and more. After studying this article, you will have a solid understanding of the asterisk operator * in Python—and … Read more

Python One Line And/Or

How do the Boolean and and or operators work in the context of Python one-liners? You may know the standard use of the logical operators applied to Boolean values: But there’s more to these operators that only experts in the art of writing concise Python one-liners know. For instance, the following use of the or … Read more