Scripting

Python One Line Dictionary

Python’s dictionary data structure is one of the most powerful, most underutilized data structures in Python. Why? Because checking membership is more efficient for dictionaries than for lists, while accessing elements is easier for dictionaries than for sets. In this tutorial, you’ll learn how to perform four common dictionary operations in one line of Python …

Python One Line Dictionary Read More »

Python Import Error (ModuleNotFoundError)

Python’s ImportError (ModuleNotFoundError) indicates that you tried to import a module that Python doesn’t find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH. If this file (__init__.py) is in the folder, change the position of the import in the file that is …

Python Import Error (ModuleNotFoundError) Read More »

Python Unicode Encode Error

Summary: The UnicodeEncodeError generally occurs while encoding a Unicode string into a certain coding. Only a limited number of Unicode characters are mapped to strings. Thus, any character that is not-represented / mapped will cause the encoding to fail and raise UnicodeEncodeError. To avoid this error use the encode(utf-8) and decode(utf-8) functions accordingly in your …

Python Unicode Encode Error Read More »

[Dash + Flask] How to Deploy a Python Dash App on Pythonanywhere.com

Here’s the step-by-step approach of how to deploy your Dash app on Pythonanywhere.com using Flask and pip: Create an account on Pythonanywhere.com. Create a Flask application. Create a Dash application. Copy the Dash app into the Flask app. Connect the Flask server with the Dash app. Modify the WSGI configuration file. Install Dash with pip …

[Dash + Flask] How to Deploy a Python Dash App on Pythonanywhere.com Read More »

Python One-Line Password Generator

Can you believe it? People use unknown and potentially insecure websites to generate their random passwords! This works as follows: A website generates a “random” password for them and they copy&paste it and assume this is a safe password because of the randomness of the characters. What a security flaw! Why? Because the website could …

Python One-Line Password Generator Read More »

How to Solve Python “TypeError: ‘int’ object is not iterable”?

It’s quite common for your code to throw a typeerror, especially if you’re just starting out with Python. The reason for this is that the interpreter expects variables of certain types in certain places in the code. We’ll look at a specific example of such an error: “typeerror: ‘int’ object is not iterable”. Exercise: Run …

How to Solve Python “TypeError: ‘int’ object is not iterable”? Read More »

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 …

Calculating Entropy with SciPy Read More »

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 …

Regex to Match Dollar Amounts with Optional Cents Read More »

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 …

Python Global in One Line Read More »

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 Split A String And Keep The Separators? Read More »

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. Why does the ValueError occur at all? Many programmers …

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