## How to Remove Specific Elements in a Numpy Array

Summary: The most straightforward way to remove an element at a given index from a NumPy array is to call the function np.delete(array, index) that returns a new array with the element removed. Problem: Given a Numpy Array; how to remove specific elements from the Numpy array? Example: Consider the following Numpy array as shown below: Challenge: How will you … Read more

## How to Change Strings to Lowercase in Pandas DataFrame

Problem Formulation Problem: Given a Pandas DataFrame; how to change the strings in the DataFrame to lowercase? Example: Consider the following Pandas DataFrame: Output: Expected Output: When you change a pandas DataFrame string column to lowercase, then the column is returned such that every string in the column is converted and displayed in lowercase while any non-alphabetical … Read more

## How to Convert a String to Binary in Python

Summary: You can convert a string to binary in Python using: bytearray() + bin() map() + bin() + bytearray() join() + format() + bytearray() join() + format() + ord() binascii.hexlify() Problem: How to convert a given string to its binary equivalent in Python? Example: When you convert a string to binary, it either results in … Read more

## How to Add Two Lists Element-wise in Python

Summary: The most pythonic approach to add two lists element-wise is to use zip() to pair the elements at the same positions in both lists and then add the two elements. Here’s a quick look at the solution: [x + y for x, y in zip(li_1, li_2)]. An alternate proposition to this without using zip: … Read more

## How to Calculate a Logistic Sigmoid Function in Python?

Summary: You can caculate the logistic sigmoid function in Python using: The Math Module: 1 / (1 + math.exp(-x)) The Numpy Library: 1 / (1 + np.exp(-x)) The Scipy Library: scipy.special.expit(x) Problem: Given a logistic sigmoid function: If the value of x is given, how will you calculate F(x) in Python? Let’s say x=0.458. Note: … Read more

## How to Overwrite the Previous Print to Stdout in Python?

Summary: The most straightforward way to overwrite the previous print to stdout is to set the carriage return (‘\r’) character within the print statement as print(string, end = “\r”). This returns the next stdout line to the beginning of the line without proceeding to the next line. Problem Formulation Problem Definition: How will you overwrite … Read more

## 8 Best Ways to Update Non-Existing Key in Python Dict

To update a key in a dictionary if it doesn’t exist, you can check if it is present in the dictionary using the in keyword along with the if statement, and then update the key-value pair using subscript notation or update() method or the asterisk * operator. This approach will be examined in Section 1 … Read more

## How to Print Colored Text in Python?

Summary: To print colored text in Python, you can use: The simple_color package, The ‘\033[1m’ ANSI escape-sequence, The termcolor module, The coloroma package, The colored library, The prompt_toolkit package. A simple, no-library way to print bolded text in Python is to enclose a given string s in the special escape sequence like so: print(“\033[38;5;4m”). We’ll … Read more

## How to Get the Size of an Image with PIL in Python

Problem Formulation Given an image, how to get the size of the image with PIL or any other Python library? Getting the size of an image with Python PIL(Python Image Library) basically implies that you want to get its height and width in pixels. Example: Consider the following image: 🔥Challenge: Get the size of the … Read more

## How to Add Time Onto a Datetime Object in Python

Problem: Given a datetime.datetime object in Python, is there a way to add time to the given datetime object? Related Question on StackOverflow: Discussion: Adding time to a datetime object in Python should lead to a new datetime object with the updated time information. For example, if you add 5 hours to a datetime object … Read more

## How to Print Bold Text in Python?

Summary: To print bold text in Python, you can use: The simple_color package, The ‘\033[1m’ ANSI escape-sequence, The termcolor module, The coloroma package, The prompt_toolkit package. A simple, no-library way to print bolded text in Python is to enclose a given string s in the special escape sequence like so: print(“\033[1m” + s + “\033[0m”). … Read more

## How to Suppress Scientific Notation in Python

Summary: Use the string literal syntax f”{number:.nf}” to suppress the scientific notation of a number to its floating-point representation. Problem Formulation: How will you suppress a number represented in scientific notation by default to a floating-point value? Note: Generally, Python represents huge floating-point numbers or very small floating-point numbers in their scientific form. Scientific notation … Read more