The compile() function is used to compile a source string into a code object. A source string refers to a regular string that contains valid Python code.
The function takes a source string and returns a code object that can be executed by using either exec() or eval() functions.
The compile function takes 3 required arguments: source , filename and mode.
compile(source, filename, mode)
The source argument is a string containing the Python source code to be compiled.
The filename argument is a string representing the filename (if any) from which the code was read, if not provided, it defaults to '<string>'.
Lastly, the mode is a string specifying what kind of code object should be created, it is one of 'exec', 'eval' or 'single'.
The difference between the 'exec' and the 'eval' mode is that, the exec mode is used with the exec() function to execute a source string which can be having more than one statements while the 'eval' mode is used with the eval() function which is only capable of evaluating a single expression.
The 'single' mode is used to compile a single interactive statement into a code object, this object can then be executed using either exec() or eval() functions.