How to Use exec() Function in PythonPython Basics


The exec() function executes the specified Python code.

Example

#!/usr/bin/python3

exec('print("ABCD")')

exec('print("Hello")nprint("World")')

Output:

ABCD
Hello
World

Syntax

exec(object, globals, locals)

Parameters

Name Description
object A String, or a code object
globals A dictionary containing global parameters
locals A dictionary containing local parameters

Return Value

The method doesn’t return any values.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us