How-to articles, tricks, and solutions about PYTHON

How do I split a string into a list of words?

Here is an example of how to split a string into a list of words in Python:

How to comment out a block of code in Python

In Python, you can comment out a block of code by using the "#" symbol at the beginning of each line.

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)

This error is raised when trying to encode a Unicode string using the ASCII codec, and the string contains a character that is not within the ASCII range (0-127).

Creating an empty Pandas DataFrame, and then filling it

Note that in above example, the DataFrame is created empty first, and then columns are added one by one using the assignment operator (=).

What is the Python equivalent for a case/switch statement?

The Python equivalent for a case/switch statement is the if-elif-else structure.

How can I get a value from a cell of a dataframe?

You can use the .at or .iat methods to get the value of a specific cell in a DataFrame.

Sum a list of numbers in Python

Here is a code snippet that demonstrates how to sum a list of numbers in Python:

'pip' is not recognized as an internal or external command

This error message typically occurs when the command prompt or terminal is not able to find the pip executable.

How do I append one string to another in Python?

You can use the "+" operator to concatenate two strings in Python.

ValueError: invalid literal for int() with base 10:

In this code snippet, the variable "value" is being set to the integer representation of the string "invalid." However, "invalid" is not a valid integer, so the int() function will raise a ValueError.

How to convert index of a pandas dataframe into a column

To convert the index of a pandas DataFrame into a column, you can use the reset_index() function, and specify that you want to move the index to a new column with the inplace=True and name parameter.

Which exception should I raise on bad/illegal argument combinations in Python?

You can raise the ValueError exception when you encounter bad or illegal argument combinations in Python.

What is the difference between dict.items() and dict.iteritems() in Python2?

In Python 2, dict.items() returns a list of the dictionary's key-value pairs, whereas dict.iteritems() returns an iterator over the dictionary's key-value pairs.

IndentationError: unindent does not match any outer indentation level

This error occurs when there is a mismatch in the indentation level of a block of code in Python.

How does collections.defaultdict work?

collections.defaultdict is a subclass of the built-in dict class in Python.

How can I delete all local Docker images?

To delete all local Docker images, you can use the following command:

How do I generate all permutations of a list?

You can use the itertools.permutations() function to generate all permutations of a list in Python.

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.

python exception message capturing

To capture an exception message in Python, you can use a try-except block and the as keyword to assign the exception message to a variable.

Getting today's date in YYYY-MM-DD in Python?

You can use the datetime module in Python to get the current date in the YYYY-MM-DD format.

Equivalent of shell 'cd' command to change the working directory?

In Python, you can use the os module to change the current working directory using the chdir() function.

How to test multiple variables for equality against a single value?

In Python, you can test multiple variables for equality against a single value using the == operator.

How do I print the full NumPy array, without truncation?

To print the full NumPy array without truncation, you can use the numpy.set_printoptions() function and set the threshold parameter to np.inf.