How-to articles, tricks, and solutions about PYTHON

Getting the class name of an instance

To get the class name of an instance in Python, you can use the __class__ attribute of the object.

Getting the index of the returned max or min item using max()/min() on a list

You can use the enumerate() function along with max() or min() to get the index of the maximum or minimum item in a list in Python.

Getting today's date in YYYY-MM-DD in Python?

You can use the datetime module in Python to get the current date in the YYYY-MM-DD format.

Hidden features of Python

There are many hidden features of Python, some of which are not well-known even among experienced Python programmers.

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.

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.

How can I access environment variables in Python?

You can use the os module in Python to access environment variables.

How can I add new keys to a dictionary?

To add a new key-value pair to a dictionary in Python, you can use the update() method or simply assign a value to a new key using square brackets [].

How can I check for NaN values?

You can use the isnan() function provided by the math module to check if a value is NaN.

How can I compare two lists in python and return matches

To compare two lists and return the matches, you can use the built-in intersection() method of the set data type.

How can I delete a file or folder in Python?

To delete a file or folder in Python, you can use the os module and call the os.remove() function to delete a file, or the shutil.rmtree() function to delete a folder and all its contents.

How can I delete all local Docker images?

To delete all local Docker images, you can use the following command:

How can I do a line break (line continuation) in Python?

In Python, you can use the "" character to indicate a line continuation.

How can I find where Python is installed on Windows?

To find where Python is installed on Windows, you can follow these steps:

How can I flush the output of the print function?

To flush the output of the print function, you can use the flush parameter of the print function.

How can I get a value from a cell of a dataframe?

You can use the .at or .iat methods to get the value of a specific cell in a DataFrame.

How can I get list of values from dict?

You can use the values() method to get a list of values from a dictionary:

How can I get the concatenation of two lists in Python without modifying either one?

There are a few different ways to concatenate two lists in Python without modifying either one.

How can I import a module dynamically given the full path?

You can use the importlib.import_module function to import a module dynamically given the full path to the module.

How can I install packages using pip according to the requirements.txt file from a local directory?

To install packages using pip from a local directory, you can use the -r option to specify the path to the requirements.txt file.

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 can I make a dictionary (dict) from separate lists of keys and values?

You can use the zip function to create a dictionary from separate lists of keys and values like this:

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.

How can I make one python file run another?

To run one Python file from another, you can use the exec function or the subprocess module.

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.