How-to articles, tricks, and solutions about PYTHON

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 get the filename without the extension from a path in Python?

To get the filename without the extension from a file path in Python, you can use the os.path.splitext() function.

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 get the last element of a list?

To get the last element of a list, you can use negative indexing.

How do I get the number of elements in a list (length of a list) in Python?

To get the number of elements in a list in Python, you can use the len() function.

How do I get the row count of a Pandas DataFrame?

You can use the shape property of the DataFrame to get the number of rows and columns.

How do I get time of a Python program's execution?

To get the time it takes for a Python program to execute, you can use the time module.

How do I install a Python package with a .whl file?

To install a Python package with a .whl file, you can use the pip command.

How do I install pip on macOS or OS X?

To install pip on macOS or OS X, you need to use the Terminal.

How do I iterate through two lists in parallel?

You can use the zip() function to iterate through two lists in parallel.

How do I list all files of a directory?

In Python, you can use the os module to list all files in a directory.

How do I lowercase a string in Python?

You can use the lower() method to lowercase a string in Python.

How do I make a flat list out of a list of lists?

To create a flat list out of a list of lists, you can use the itertools.chain function from the itertools module in the Python standard library.

How do I make a time delay?

There are several ways to add a delay to your Python program.

How do I measure elapsed time in Python?

There are several ways to measure elapsed time in Python.

How do I merge two dictionaries in a single expression?

You can use the update() method of one dictionary to merge the key-value pairs from another dictionary into it.

How do I pad a string with zeroes?

You can use the zfill() method to pad a string with zeros on the left side.

How do I parse a string to a float or int?

To parse a string to a float or int in Python, you can use the float() and int() functions, respectively.

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 do I pass a variable by reference?

In Python, you can pass a variable by reference by using the & operator.

How do I print an exception in Python?

You can print an exception in Python by using the print() function and passing the exc variable which is the default variable name for an exception.

How do I print colored text to the terminal?

You can use the termcolor module to print colored text to the terminal in Python.

How do I print curly-brace characters in a string while using .format?

To print a curly brace character in a string that is being formatted with the .format() method, you will need to use double curly braces to escape the character.