Python all() Function

Python’s built-in all(x) function takes one iterable as an argument x such as a list, tuple, or dictionary. It returns True if all iterable elements evaluate to True using implicit Boolean conversion, otherwise it returns False. If the iterable is empty, all() returns True because the condition is satisfied for all elements. Argument x -> … Read more

Python Built-In Functions

Python comes with many built-in functions you can use without importing a library. The following table goes over all built-in functions in alphabetical order. If you click on any of the provided links, you’ll find an in-depth article with a short explainer video on the Finxter blog to help you improve your skills. Just follow … Read more

Show Don’t Tell: Commenting Python Best Practices

This tutorial taken from my upcoming programming book “From One to Zero” (NoStarch, 2021) will show you how to write great comments. While most online tutorials focus on a bullet list of commenting tips, we dive deeper into the meat exploring the underlying reasons for the commonly recommended commenting principles. So, let’s get started! Code … Read more

Python Math Domain Error (How to Fix This Stupid Bug)

You may encounter a special ValueError when working with Python’s math module. Python raises this error when you try to do something that is not mathematically possible or mathematically defined. To understand this error, have a look at the definition of the domain: “The domain of a function is the complete set of possible values … Read more

np.polyfit() — Curve Fitting with NumPy Polyfit

The np.polyfit() function, accepts three different input values: x, y and the polynomial degree. Arguments x and y correspond to the values of the data points that we want to fit, on the x and y axes, respectively. The third parameter specifies the degree of our polynomial function. For example, to obtain a linear fit, … Read more

How to Calculate the Column Variance of a DataFrame in Python Pandas?

Want to calculate the variance of a column in your Pandas DataFrame? In case you’ve attended your last statistics course a few years ago, let’s quickly recap the definition of variance: it’s the average squared deviation of the list elements from the average value. You can calculate the variance of a Pandas DataFrame by using … Read more

Python Function Call Inside List Comprehension

Question: Is it possible to call a function inside a list comprehension statement? Background: List comprehension is a compact way of creating lists. The simple formula is [expression + context]. Expression: What to do with each list element? Context: What elements to select? The context consists of an arbitrary number of for and if statements. … Read more

Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug)

Do you encounter this stupid error? You’re not alone—thousands of coders like you generate this error in thousands of projects every single month. This short tutorial will show you exactly why this error occurs, how to fix it, and how to never make the same mistake again. So, let’s get started! Python throws the TypeError … Read more

How to Convert a Float List to an Integer List in Python

The most Pythonic way to convert a list of floats fs to a list of integers is to use the one-liner fs = [int(x) for x in fs]. It iterates over all elements in the list fs using list comprehension and converts each list element x to an integer value using the int(x) constructor. This … Read more