How-to articles, tricks, and solutions about PYTHON

Is there a "not equal" operator in Python?

Yes, in Python there is an operator for not equal (!=) .

Cannot find module cv2 when using OpenCV

The error message "Cannot find module cv2" usually indicates that the OpenCV library is not installed on your system, or that Python is unable to find the library.

Why am I seeing "TypeError: string indices must be integers"?

The "TypeError: string indices must be integers" error is raised when you try to access an element of a DataFrame or Series using a string index instead of an integer index.

How to deal with SettingWithCopyWarning in Pandas

The "SettingWithCopyWarning" in pandas is raised when you try to modify a copy of a DataFrame or Series rather than the original.

How to return dictionary keys as a list in Python?

In Python, you can use the dict.keys() method to return a view object that contains the keys of a dictionary.

Checking whether a variable is an integer or not

In Python, you can check if a variable is an integer using the isinstance() function.

How to change the font size on a matplotlib plot

There are a couple of ways to change the font size on a matplotlib plot.

Remove specific characters from a string in Python

In Python, you can remove specific characters from a string by using the replace() method or using string slicing and concatenation.

How to install pip with Python 3?

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

How to add to the PYTHONPATH in Windows, so it finds my modules/packages?

In Windows, you can add to the PYTHONPATH environment variable to make sure that Python can find your modules and packages.

Check if something is (not) in a list in Python

In Python, you can check if an item is in a list using the in keyword.

How do I reverse a list or loop over it backwards?

There are a few ways to reverse a list in Python, depending on what you want to do with the reversed list.

How to drop rows of Pandas DataFrame whose value in a certain column is NaN

You can drop rows of a Pandas DataFrame that have a NaN value in a certain column using the dropna() function.

How do I terminate a script?

In Python, you can terminate a script by using the exit() function from the sys module.

How to copy a dictionary and only edit the copy

There are a few ways to copy a dictionary in Python, depending on the level of copying that you need.

How do I get the full path of the current file's directory?

You can use the os module to get the full path of the current file's directory.

How do I trim whitespace from a string?

There are a few ways to trim whitespace from a string in Python.

How to move a file in Python?

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

In Python, how do I determine if an object is iterable?

In Python, an object is considered iterable if it has an __iter__() method defined or if it has a __getitem__() method with defined indices (i.e., it can be indexed, like a list or a string).

How do I create multiline comments in Python?

In Python, you can create a multiline comment using triple quotes (either single or double) at the beginning and end of the comment.

Removing duplicates in lists

There are a few ways to remove duplicates from a list in Python.

Best way to convert string to bytes in Python 3?

In Python 3, you can convert a string to bytes using the bytes function.

Change column type in pandas

In pandas, you can change the data type of a column using the astype() function.

What is the use of "assert" in Python?

In Python, the assert statement is used to check if a certain condition is true, and if it is not true, raise an exception.