How to Find Number of Digits in an Integer?

To find the number of digits in an integer you can use one of the following methods: (1) Use Iteration (2) Use str()+len() functions (3) Use int(math.log10(x)) +1 (4) Use Recursion Problem Formulation Given: An integer value. Question: Find the number of digits in the integer/number given. Test Cases: Input: num = 123 Output: 3 … Read more

How to Initialize a Dictionary with Keys in Python?

Summary: The most Pythonic approach to initialize a dictionary with keys is to use the dict.fromkeys(key, None) dictionary method, which accepts each item from the given list as a key and associates None to each key as a value. Problem Formulation: Given a list containing employee IDs as items within it. How will you create … Read more

How to Save a Dictionary to a File in Python

Summary: You can use Python’s pickle library to save dictionary data to a file. Another efficient approach to save dictionary data to a file is to use Python’s built-in JSON package. You can also use simple file handling functions to store dictionary data in a text file directly. Problem: Given a Python dictionary. How will … Read more

How to Extend a NumPy Array in Python

Summary: Call the append function of the Numpy library as: numpy.append(given_array, elements_to_be_appended, axis) to extend the given array along a specific axis. Other ways of extending the array include using: (i) the vstack and column_stack helper functions. (ii) the numpy.insert function. Problem Formulation Given a Numpy array; How will you extend the given array with … Read more

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