How-to articles, tricks, and solutions about PYTHON

How to convert string representation of list to a list

You can use the ast.literal_eval() function from the ast module to safely evaluate a string and convert it to a list.

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:

Create an empty list with certain size in Python

You can create an empty list of a certain size in Python using the following methods:

Why is python setup.py saying invalid command 'bdist_wheel' on Travis CI?

The error message "invalid command 'bdist_wheel'" occurs when the "bdist_wheel" module is not installed.

What is the purpose and use of **kwargs?

In Python, kwargs is a way to pass a keyworded, variable-length argument list.

Argparse optional positional arguments?

In Python, the argparse module can be used to specify optional positional arguments by setting the nargs parameter to '?'.

Pandas Merging 101

Here is an example of how to use the pd.merge() function to merge two DataFrames in pandas:

ImportError: No module named requests

This error message indicates that the "requests" module, which is used for making HTTP requests in Python, is not installed on your system or is not in the Python path.

How can I iterate over files in a given directory?

You can use the os module in Python to iterate over files in a given directory.

How to get the last day of the month?

You can use the calendar.monthrange() function from the calendar module in Python to get the last day of a month.

How are iloc and loc different?

iloc and loc are both used to select rows and columns from a Pandas DataFrame, but they work differently.

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).

How do I set the figure title and axes labels font size?

In matplotlib, you can set the font size of the figure title and axes labels using the pyplot module.

Purpose of "%matplotlib inline"

The purpose of "%matplotlib inline" in Python is to display matplotlib plots within the Jupyter notebook.

How do I get the day of week given a date?

You can use the datetime module in Python to get the day of the week for a given date.

How do I parse an ISO 8601-formatted date?

You can use the datetime.fromisoformat() method to parse an ISO 8601-formatted date in Python.

How to sort a list/tuple of lists/tuples by the element at a given index?

You can use the sorted() function with the key parameter to specify a function that extracts the element at the desired index.

Using Python 3 in virtualenv

Here is an example of how you can use Python 3 in a virtual environment using the virtualenv package:

What does the "at" (@) symbol do in Python?

In Python, the "at" (@) symbol is used to decorate a function.

Python: Find in list

Here is a code snippet that demonstrates how to find an element in a list in Python:

How can I open multiple files using "with open" in Python?

You can open multiple files using "with open" in Python by using multiple with open statements, one for each file you wish to open.

How to define a two-dimensional array?

A two-dimensional array in Python can be defined using the numpy library.

not None test in Python

In Python, you can check if a variable is not equal to None using the is not operator.

How can I use Python to get the system hostname?

You can use the socket module in Python to get the hostname of the system.