Month: November 2020

list.clear() vs New List — Why Clearing a List Rather Than Creating a New One?

Problem: You’ve just learned about the list.clear() method in Python. You wonder, what’s its purpose? Why not creating a new list and overwriting the variable instead of clearing an existing list? Example: Say, you have the following list. If you clear the list, it becomes empty: However, you could have accomplished the same thing by …

list.clear() vs New List — Why Clearing a List Rather Than Creating a New One? Read More »

Python abs() Function

Python’s built-in abs(x) function returns the absolute value of the argument x that can be an integer, float, or object implementing the __abs__() function. For a complex number, the function returns its magnitude. The absolute value of any numerical input argument -x or +x is the corresponding positive value +x. Argument x int, float, complex, …

Python abs() Function Read More »

Python Regex – How to Count the Number of Matches?

To count a regex pattern multiple times in a given string, use the method len(re.findall(pattern, string)) that returns the number of matching substrings or len([*re.finditer(pattern, text)]) that unpacks all matching substrings into a list and returns the length of it as well. A few hours ago, I wrote a regular expression in Python that matched …

Python Regex – How to Count the Number of Matches? Read More »

Python Built-In Functions

Python comes with many built-in functions you can use without importing any library. Here are they in alphabetical order: Built-in Functions abs() delattr() hash() memoryview() set() all() dict() help() min() setattr() any() dir() hex() next() slice() ascii() divmod() id() object() sorted() bin() enumerate() input() oct() staticmethod() bool() eval() int() open() str() breakpoint() exec() isinstance() ord() …

Python Built-In Functions Read More »

Exponential Fit with SciPy’s curve_fit()

In this article, you’ll explore how to generate exponential fits by exploiting the curve_fit() function from the Scipy library. SciPy’s curve_fit() allows building custom fit functions with which we can describe data points that follow an exponential trend. In the first part of the article, the curve_fit() function is used to fit the exponential trend …

Exponential Fit with SciPy’s curve_fit() Read More »

Christian Mayer

Chris is the founder of the programming education company FINXTER, author of the Coffee Break Python series of self-published books, the popular programming book Python One-Liners (NoStarch 2020), a doctorate computer scientist, and owner of one of the top 10 Python blogs worldwide. His research interests include graph theory and distributed systems. You can join …

Christian Mayer Read More »

Law of Demeter

This tutorial gives you a short explanation of the law of Demeter. It’s based on a rough chapter draft for my upcoming book “From One to Zero” to appear with NoStarch in 2021. You’ve already learned that one of the most important reasons for code complexity is interdependency. To write clean code, you must minimize …

Law of Demeter 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 …

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