Scripting

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 …

Show Don’t Tell: Commenting Python Best Practices Read More »

Matplotlib Line Plot – A Helpful Illustrated Guide

The line plot is the most iconic of all the plots. To draw one in matplotlib, use the plt.plot() function and pass it a list of numbers used as the y-axis values. Per default, the x-axis values are the list indexes of the passed line. Matplotlib automatically connects the points with a blue line per …

Matplotlib Line Plot – A Helpful Illustrated Guide Read More »

How to Catch and Print Exception Messages in Python

Python comes with an extensive support of exceptions and exception handling. An exception event interrupts and, if uncaught, immediately terminates a running program. The most popular examples are the IndexError, ValueError, and TypeError. An exception will immediately terminate your program. To avoid this, you can catch the exception with a try/except block around the code …

How to Catch and Print Exception Messages in Python 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 …

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

np.polyfit() — Curve Fitting with NumPy Polyfit

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

np.polyfit() — Curve Fitting with NumPy Polyfit Read More »

Python Int to String with Leading Zeros

To convert an integer i to a string with leading zeros so that it consists of 5 characters, use the format string f'{i:05d}’. The d flag in this expression defines that the result is a decimal value. The str(i).zfill(5) accomplishes the same string conversion of an integer with leading zeros. Challenge: Given an integer number. …

Python Int to String with Leading Zeros 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 …

How to Calculate the Column Variance of a DataFrame in Python Pandas? 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. …

Python Function Call Inside List Comprehension 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 …

Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) 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 …

How to Convert a Float List to an Integer List in Python Read More »