A context manager is a Python object that defines the methods __enter__() and __exit__(). It is used to manage resources such as files, sockets, and database connections, ensuring that they are properly initialized and cleaned up.