# Scripting

## Calculating Entropy with SciPy

Problem: How to calculate the entropy with the SciPy library? Solution: Import the entropy() function from the scipy.stats module and pass the probability and the base of the logarithm into it. Try It Yourself: Run this code in the interactive code shell! Exercise: Change the probabilities. How does the entropy change? Let’s start slowly! You’ll …

## Regex to Match Dollar Amounts with Optional Cents

Problem: How to match dollar amounts in a given string? If cent amounts are given, those should match as well. Solution: You can use regular expression pattern ‘(\\$[0-9]+(.[0-9]+)?)’ to find all numbers that start with a dollar symbol, followed by an arbitrary number of numerical digits, followed by an optional decimal point and an arbitrary …

## Python Global in One Line

To update a global variable in one line of Python, retrieve the global variable dictionary with the globals() function, and access the variable by passing the variable name as a string key such as globals()[‘variable’]. Then overwrite the global variable using the equal symbol, for example in globals()[‘variable’] = 42 to overwrite the variable with …

## How To Split A String And Keep The Separators?

Summary: To split a string and keep the delimiters/separators you can use one of the following methods: Use a regex module and the split() method along with \W special character. Use a regex module and the split() method along with a negative character set [^a-zA-Z0-9]. Use a regex module and the split() method along with …

## How to Fix “ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()”

If you run the following code, you’ll experience a special ValueError: The output will be this error message: Try It Yourself in our interactive browser shell: Solution: Using the Numpy functions called logical_and() and logical_or() instead of Python’s logical operators (“and” and “or”) is the solution. Master the basics and join the “Python Built-in Functions” …

## Python Comments — 2-Minute Guide with Exercise

Wouldn’t reading code be much easier if the author constantly shared their thoughts with you? Commenting is good practice in Python because it helps others (and your future self) understanding your code much better. Writing commented code makes you more productive in the long term! There are two types of comments: one-line comments and multi-line …

## Python Keyboard Errors

When you’re learning and working with Python, you’re going to encounter errors and there’s no getting around that fact. So how do you get around them? It helps to learn about the errors. We’re going to take the opportunity to learn one of them today. What is “Keyboard Interrupt”? This is an error that you’re …

## Python One Line Class

Do you hate those lengthy class definitions with __init__ and too many whitespaces and newlines? Python One-Liners to the rescue! Luckily, you can create classes in a single line—and it can even be Pythonic to do so! Sounds too good to be true? Let’s dive right into it! Problem: How to create a Python class …

## Python Define Multiple Variables in One Line

In this article, you’ll learn about two variants of this problem. Assign multiple values to multiple variables Assign the same value to multiple variables Let’s have a quick overview of both in our interactive code shell: Exercise: Increase the number of variables to 3 and create a new one-liner! Let’s dive into the two subtopics …

## Python One Line Map

The single line of Python code is more powerful than you may expect. In fact, you can compress whole algorithms in a single line of Python code. In this tutorial, you’ll learn how to use the built-in map() function in Python to one-linerize a critical step that would otherwise take multiple lines of Python code: …

## What Is Python Output Buffering and How to Disable It?

Summary: Python output buffering is the process of storing the output of your code in buffer memory. Once the buffer is full, the output gets displayed on the standard output screen. Buffering is enabled by default and you can use one of the following methods to disable it: Execute the Python code with the -u …

## How To Get A Cron Like Scheduler in Python?

Summary: To get a cron like scheduler in Python you can use one of the following methods: Use schedule module Use APScheduler Use timeloop library Use CronTab module Cron (also called a cron job) is a software utility that helps a user to schedule tasks in Unix-like systems. The tasks in cron are present in …