How-to articles, tricks, and solutions about PYTHON

How do I type hint a method with the type of the enclosing class?

In Python, you can use the self keyword to refer to the instance of the enclosing class within a method.

How do I unload (reload) a Python module?

To unload a Python module, you can use the del statement to remove it from memory.

How do I update/upgrade pip itself from inside my virtual environment?

To update pip itself from inside your virtual environment, you can use the following command:

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.

How do I use a decimal step value for range()?

You can use the numpy library's arange() function to specify a decimal step value for the range.

How do I use raw_input in Python 3?

In Python 3, the input() function can be used in place of raw_input() to read input from the user.

How do I wait for a pressed key?

Here is an example of how you might wait for a key press in Python:

How do I write JSON data to a file?

To write JSON data to a file in Python, you can use the json module.

How do you get the logical xor of two variables in Python?

You can use the ^ operator to get the logical XOR of two variables in Python.

How do you round UP a number?

In Python, you can use the ceil() function from the math module to round a number up.

How do you test that a Python function throws an exception?

You can use the pytest.raises function to test that a Python function throws an exception.

How does collections.defaultdict work?

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

How does Python's super() work with multiple inheritance?

In Python, super() is used to call a method from a parent class.

How does the @property decorator work in Python?

In Python, the @property decorator is used to define a method as a "getter" for a class property.

How to activate virtualenv in Linux?

To activate a virtual environment in Linux, you can use the source command and the path to the activate script that is located in the virtual environment's bin directory.

How to add a new column to an existing DataFrame?

You can add a new column to an existing pandas DataFrame by using the assign() method or the [] notation.

How to add an empty column to a dataframe?

In pandas, you can add an empty column to a DataFrame using the assign() method or the insert() method.

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.

How to apply a function to two columns of Pandas dataframe

To apply a function to two columns of a Pandas DataFrame, you can use the apply() method of the DataFrame and pass the function as an argument.

How to calculate number of days between two given dates

To calculate the number of days between two dates, you can use the timedelta class from the datetime module.

How to catch and print the full exception traceback without halting/exiting the program?

You can use the traceback module to catch and print the full exception traceback without halting/exiting the program.

How to change a string into uppercase?

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

How to change the figure size of a seaborn axes or figure level plot

You can change the figure size of a Seaborn plot by using the set_size_inches() method of the matplotlib.pyplot.figure object and passing in the desired width and height.

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.