How-to articles, tricks, and solutions about PYTHON
In Python, __all__ is a list of strings that defines the names that should be imported when from <module> import * is used.
To get the filename without the extension from a file path in Python, you can use the os.path.splitext() function.
To use a different Python version with virtualenv, follow these steps:
To convert an integer to a string in Python, use the str() function.
In Python 3, you can use the http.server module to run a simple HTTP server.
To get the time it takes for a Python program to execute, you can use the time module.
To write JSON data to a file in Python, you can use the json module.
To reverse a string in Python, you can use the following method:
To save a plot to an image file using Matplotlib, you can use the savefig function.
To install packages using pip from a local directory, you can use the -r option to specify the path to the requirements.txt file.
There are several ways to profile a Python script:
In Python 3, all classes automatically inherit from the object class.
To select multiple columns in a pandas DataFrame, you can pass a list of column names to the indexing operator [].
You can use the zip function to create a dictionary from separate lists of keys and values like this:
You can use the importlib.import_module function to import a module dynamically given the full path to the module.
To install pip on macOS or OS X, you need to use the Terminal.
Here is a simple function that generates a random string of a given length using upper case letters and digits:
To print to stderr in Python, you can use the stderr attribute of the sys module:
It looks like you are trying to include the Python.h header file in a C or C++ program, but it is not found on your system.
To declare a custom exception in Python, you can create a new class that inherits from the built-in Exception class.
You can use the shape property of the DataFrame to get the number of rows and columns.
In Python, a single underscore "_" before an object name indicates that the object is meant to be private, meaning that it should not be directly accessed or modified outside of the class that it is defined in.
Here are a few alternatives to using a switch statement in Python:
You can extract the file extension from a file name in Python by using the os.path module.