How-to articles, tricks, and solutions about PYTHON

How do I determine the size of an object in Python?

You can use the sys.getsizeof() function to determine the size of an object in bytes.

Why use pip over easy_install?

Pip is generally preferred over easy_install because it offers a number of features that easy_install does not, including better package management and more user-friendly command options.

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.

Combine two columns of text in pandas dataframe

In pandas, you can use the str.cat() function to combine the values of two columns of text into a single column.

How to print a date in a regular format?

In Python, you can use the datetime module to work with dates and times.

How to extract numbers from a string in Python?

In Python, you can use regular expressions to extract numbers from a string.

How can I parse a YAML file in Python

To parse a YAML file in Python, you can use the yaml library.

How are lambdas useful?

Lambdas, also known as anonymous functions, are useful in Python because they allow you to create small, one-time use functions without having to define them with a full function statement.

Parsing boolean values with argparse

Here is an example of how to parse boolean values with argparse in Python:

How to check if type of a variable is string?

You can use the type() function in Python to check the type of a variable.

Peak detection in a 2D array

Here is an example of a Python function that can be used to detect peaks in a 2D array:

What is __pycache__?

__pycache__ is a directory that is created by the Python interpreter when it imports a module.

How to get an absolute file path in Python

You can use the os.path.abspath() function to get the absolute file path of a file in Python.

mkdir -p functionality in Python

The os.makedirs() function in Python can be used to create a directory with the -p option, which will create the entire directory path, including any missing parent directories.

How can I make a Python script standalone executable to run without ANY dependency?

One way to make a Python script standalone executable is by using the package pyinstaller.

Importing modules from parent folder

In Python, you can use the sys.path.append() method to add the parent directory to the list of paths where Python looks for modules.

Traverse a list in reverse order in Python

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

What is the naming convention in Python for variable and function?

In Python, variable and function names should be lowercase, with words separated by underscores.

How do I remove the first item from a list?

You can use the list method pop(index) to remove the first item in a list.

How to POST JSON data with Python Requests?

You can use the requests library in Python to send a POST request with JSON data.

How to get a function name as a string?

In Python, you can use the built-in function attribute __name__ to get the name of a function as a string.

Behaviour of increment and decrement operators in Python

In Python, the increment operator (++) and decrement operator (--) do not exist.