How-to articles, tricks, and solutions about PYTHON

Replacements for switch statement in Python?

Here are a few alternatives to using a switch statement in Python:

Reverse / invert a dictionary mapping

Here's an example of how you can reverse or invert a dictionary mapping in Python:

Running shell command and capturing the output

In Python, you can use the subprocess module to run shell commands and capture their output.

Running unittest with typical test directory structure

Here is an example of how you can run unittests in Python using the typical test directory structure:

Save plot to image file instead of displaying it using Matplotlib

To save a plot to an image file using Matplotlib, you can use the savefig function.

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.

Selecting multiple columns in a Pandas dataframe

To select multiple columns in a pandas DataFrame, you can pass a list of column names to the indexing operator [].

Selenium using Python - Geckodriver executable needs to be in PATH

If you are using Selenium with the Firefox web browser and you see the error message "Geckodriver executable needs to be in PATH," it means that the Selenium Python library cannot find the geckodriver executable on your system.

Set value for particular cell in pandas DataFrame using index

In pandas, you can set the value of a specific cell in a DataFrame using the at method.

Should I put #! (shebang) in Python scripts, and what form should it take?

You should include a shebang (#!) in Python scripts if you want the script to be directly executable from the command line.

Should I use 'has_key()' or 'in' on Python dicts?

It is recommended to use the in keyword to check if a key exists in a Python dict, rather than the has_key() method.

Shuffling a list of objects

The random module in Python provides a function called shuffle() which can be used to shuffle the elements of a list.

Split string on whitespace in Python

You can use the split() method to split a string on whitespace in Python.

Split string with multiple delimiters in Python

You can use the re.split() function from the re module in Python to split a string using multiple delimiters.

Static methods in Python?

In Python, a static method is a method that belongs to a class rather than an instance of the class.

Styling multi-line conditions in 'if' statements?

In Python, there are a few different ways to style multi-line conditions in if statements, depending on the complexity of the condition and personal preference.

Sum a list of numbers in Python

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

SyntaxError: unexpected EOF while parsing

The SyntaxError: unexpected EOF while parsing error is raised when the Python interpreter reaches the end of the file (EOF) while it is still parsing the file, and it is unable to complete the parsing process because of an error in the syntax of the code.

Traverse a list in reverse order in Python

You can use the reversed() function to iterate through a list in reverse order.

TypeError: 'module' object is not callable

This error message typically occurs when you are trying to call a module as if it were a function.

TypeError: 'NoneType' object is not iterable in Python

The TypeError: 'NoneType' object is not iterable error message is raised when you are trying to iterate over an object that has a value of None, which is not iterable.

TypeError: a bytes-like object is required, not 'str' when writing to a file in Python 3

This error occurs when you are trying to write a string to a file using the write() method in Python 3, but the file is opened in binary mode (using the 'b' flag when opening the file).

TypeError: list indices must be integers or slices, not str

This error is usually caused when you try to access an element in a list using a string as the index, rather than an integer.