Python

Python Menu

Python

Python exec() Function - Definition, Syntax, Parameters, Examples

Definition

The exec() function executes the specified Python code.

The exec() function accepts large blocks of code, unlike the eval() function which only accepts a single expression

Syntax

exec(object, globals, locals)

Parameters

Parameter Description
object A String or a code object
globals Optional. A dictionary containing global parameters
locals Optional. A dictionary containing local parameters

Examples

myCode = 'fruits = {"apple","orange","mango"}\nprint(fruits)' exec(myCode)

Introduction

Python Basics

Python Advance

Data Science Python Tutorials

Python Functions and Methods