How-to articles, tricks, and solutions about PYTHON

How to extract the substring between two markers?

In Python, you can use the str.find() method to find the index of the first marker, and the str.rfind() method to find the index of the second marker.

How to find which version of TensorFlow is installed in my system?

You can use the following code snippet to check which version of TensorFlow is installed in your system:

How to fix "Attempted relative import in non-package" even with __init__.py

The "Attempted relative import in non-package" error occurs when attempting to use a relative import within a script that is not part of a package.

How to fix: "UnicodeDecodeError: 'ascii' codec can't decode byte"

The "UnicodeDecodeError: 'ascii' codec can't decode byte" error occurs when trying to decode non-ASCII bytes using the ASCII codec.

How to get a function name as a string?

In Python, you can use the built-in function attribute __name__ to get the name of a function as a string.

How to get all possible combinations of a list’s elements?

You can use the itertools library in Python to get all possible combinations of a list's elements.

How to get an absolute file path in Python

You can use the os.path.abspath() function to get the absolute file path of a file in Python.

How to get line count of a large file cheaply in Python?

You can use the line_count() function from the itertools module to get the line count of a large file cheaply in Python.

How to get the ASCII value of a character

To get the ASCII value of a character in Python, you can use the built-in ord() function.

How to get the last day of the month?

You can use the calendar.monthrange() function from the calendar module in Python to get the last day of a month.

How to get the position of a character in Python?

You can use the index() method to get the position of a specific character in a string.

How to identify which OS Python is running on?

You can use the platform module in Python to identify which operating system the code is running on.

How to import the class within the same directory or sub directory?

In Python, you can use the import statement to import a module or class from a file within the same directory or a subdirectory.

How to initialize a two-dimensional array in Python?

You can use the built-in list function to create a 2D array (also known as a list of lists) in Python.

How to install PIL with pip on Mac OS?

To install PIL (Python Imaging Library) using pip on Mac OS, you can use the following command in your terminal:

How to install pip with Python 3?

To install pip with Python 3, you can use the following command:

How to iterate over rows in a DataFrame in Pandas

You can use the iterrows() method to iterate over rows in a Pandas DataFrame.

How to leave/exit/deactivate a Python virtualenv

To leave a Python virtual environment, you can use the deactivate command.

How to list all functions in a module?

To list all functions in a module, you can use the dir() function to get a list of all the names defined in the module, and then use the inspect module to check if each name is a function.

How to make a class JSON serializable

In order to make a class JSON serializable in Python, you need to define two methods: __init__ and to_json.

How to make IPython notebook matplotlib plot inline

To make matplotlib plots show up inline in an IPython notebook, you can use the following code snippet:

How to move a file in Python?

In Python, you can use the shutil module to move a file.

How to normalize a NumPy array to a unit vector?

To normalize a NumPy array to a unit vector, you can use the numpy.linalg.norm function to calculate the magnitude of the vector, and then divide the array by this magnitude.

How to overcome "datetime.datetime not JSON serializable"?

One way to overcome "datetime.datetime not JSON serializable" in Python is to use the json.dumps() method with the default argument, default=str, which converts the datetime object to a string before serializing.