How-to articles, tricks, and solutions about PYTHON

How do I print the key-value pairs of a dictionary in python

You can use the items() method to print the key-value pairs of a dictionary in Python.

How to read a text file into a list or an array with Python

One way to read a text file into a list or an array with Python is to use the split() method.

How do I upgrade the Python installation in Windows 10?

To upgrade the Python installation in Windows 10, you can use the pip package manager.

Python - TypeError: 'int' object is not iterable

This error message is indicating that you are trying to iterate over an object of type 'int', which is not iterable (i.e.

Filter pandas DataFrame by substring criteria

Here is an example of how you can filter a pandas DataFrame by substring criteria:

How to print instances of a class using print()?

To print instances of a class using the built-in print() function, you can define a __str__ method within the class.

How do I get the current time in milliseconds in Python?

You can use the time module in Python to get the current time in milliseconds.

Append integer to beginning of list in Python

To append an integer to the beginning of a list in Python, you can use the insert() method.

Does Python's time.time() return the local or UTC timestamp?

The time.time() function in Python returns the current timestamp in seconds since the epoch (January 1, 1970) in the local timezone.

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 check if any value is NaN in a Pandas DataFrame

You can use the isna() method to check for NaN values in a Pandas DataFrame.

How do I find the duplicates in a list and create another list with them?

In Python, one way to find duplicates in a list and create another list with them is to use a for loop and an if statement.

Selecting a row of pandas series/dataframe by integer index

You can use the .iloc[] property to select a row by its integer index in a pandas DataFrame or Series.

Plot logarithmic axes

Here is a code snippet that shows how to plot a graph with logarithmic scales for both the x and y axes in Python using the Matplotlib library:

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.

TypeError: method() takes 1 positional argument but 2 were given

This error message is indicating that a method or function called "method" is expecting one argument, but it was called with two arguments.

How to declare and add items to an array in Python?

To declare an array in Python, you can use the array module.

"Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3

This error occurs when trying to open a file that contains escape characters (such as \) in the file path, and the escape characters are not being properly interpreted by Python.

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:

error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Here is an example of how to handle a UnicodeDecodeError caused by an invalid start byte:

How to print a dictionary's key?

To print the keys of a dictionary in Python, you can use the built-in keys() method.

python: SyntaxError: EOL while scanning string literal

This error message is indicating that there is a problem with a string in your code.

What should I do with "Unexpected indent" in Python?

"Unexpected indent" in Python means that the indentation level of a line of code is not what the interpreter was expecting.

How to change a string into uppercase?

In Python, you can use the built-in upper() method to change a string to uppercase.

How can the Euclidean distance be calculated with NumPy?

Here is a code snippet that shows how to calculate the Euclidean distance using NumPy: