/  Exec() method in Python Built in Function

Exec() method in Python Built in Function

 

Exec method in built in function executes a program which is either a string or a code object.

 

Syntax:

exec(object[, globals[, locals]])

 

Parameter

exec has three parameters.

  1. Expression:the parsed or the resolved string is entered and evaluated as a python expression.
  2. Locals:this parameter is used a mapping object. Dictionary is an commonly used mapping object.
  3. Globals: It a dictionary.

 

Example-1:

x = 'name = "Tulip season in Ottawa"\nprint(name)'
exec(x)

 

Output:

Exec () method in Python Built in Function

 

Example-2:

x = 'a = 8\nb=10\nprint("add of =", a+b)'
exec(x)

 

Output:

Exec () method in Python Built in Function

 

Example-3:

program = input('Enter a program:')
exec(program)

 

Output:

Exec () method in Python Built in Function