{"id":20141,"date":"2020-09-29T09:38:02","date_gmt":"2020-09-29T04:08:02","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/"},"modified":"2025-06-12T03:57:10","modified_gmt":"2025-06-11T22:27:10","slug":"eval-in-python","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/","title":{"rendered":"eval() Function in Python"},"content":{"rendered":"\n<p>Python\u2019s <code>eval()<\/code> function runs Python code stored as a string. It takes a string, treats it as a Python expression, and then calculates its value. This lets you run code that is not directly written in your program.<\/p>\n\n\n\n<p>It helps you execute dynamic code. You can use it to evaluate mathematical expressions or even call functions from strings. This helps when you need to run user-provided input as code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-eval\">What is <code>eval()<\/code>?<\/h2>\n\n\n\n<p>A Python <code>eval()<\/code> function runs a string as a Python expression and returns the result. You provide a string, and <code>eval()<\/code> processes it as if it were a piece of live Python code.<\/p>\n\n\n\n<p>It takes three arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>expression:<\/strong> This is the string you want to evaluate. It must be a valid Python expression.<\/li>\n\n\n\n<li><strong>globals (optional):<\/strong> This is a dictionary. It specifies the global namespace that <code>eval()<\/code> will use.<\/li>\n\n\n\n<li><strong>locals (optional):<\/strong> This is another dictionary. It specifies the local namespace that <code>eval()<\/code> will use.<\/li>\n<\/ul>\n\n\n\n<p>You can use <code>eval()<\/code> for simple calculations or more complex tasks.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-title-link\">Python Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>11.5 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>51 Coding Exercises<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-ways-to-use-eval-in-python\">3 Ways to Use <code>eval()<\/code> in Python<\/h2>\n\n\n\n<p>You can use <code>eval()<\/code> for various tasks:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Evaluate Math Expressions: Run calculations from strings.<\/li>\n\n\n\n<li>Run Dynamic Code: Execute simple Python statements from strings.<\/li>\n\n\n\n<li>Work with Scope: Control which variables <code>eval()<\/code> can see.<\/li>\n<\/ul>\n\n\n\n<p>Let\u2019s look at each one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-evaluate-math-expressions\">1. Evaluate Math Expressions<\/h3>\n\n\n\n<p>The most common use of <code>eval()<\/code> is to calculate mathematical expressions. You give it a string containing numbers and operators, and it returns the answer.<\/p>\n\n\n\n<p>Here is an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Evaluate a simple math expression\nresult = eval(&quot;10 + 5&quot;)\nprint(result)\n\n# Evaluate a more complex expression\nresult_complex = eval(&quot;2 * (3 + 4) \/ 7&quot;)\nprint(result_complex)\n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n15\n2.0\n<\/pre><\/div>\n\n\n<p>You pass the math expressions as strings. <code>eval()<\/code> handles the calculation. This is useful for building calculators or processing formulas from text.<\/p>\n\n\n\n<p>You can also evaluate expressions that include variables, if those variables are defined in the current scope:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nx = 10\ny = 20\n\n# Evaluate an expression with variables\nresult_vars = eval(&quot;x * y + 5&quot;)\nprint(result_vars)\n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n205\n<\/pre><\/div>\n\n\n<p>The <code>eval()<\/code> function sees <code>x<\/code> and <code>y<\/code> from the surrounding code and uses their values.<\/p>\n\n\n\n<p>Using <code>eval()<\/code> to calculate mathematical formulas from text is a great way to build tools that work like Excel calculators. However, even simple math in Python relies on a solid grasp of how numbers and strings interact. To help you build those foundational skills through hands-on practice, we recommend trying our Free Python Course. It is tailored for beginners who want to learn how to write functional scripts and perform data tasks at their own pace.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Free Course<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-fundamentals-for-beginners\" class=\"courses-cta-title-link\">Python Fundamentals for Beginners Free Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Master Python basics, from variables to data structures and control flow. Solve real-time problems and build practical skills using Jupyter Notebook.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>13.5 hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>4.55<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-fundamentals-for-beginners\" class=\"courses-cta-button\">\n                Enroll for Free\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-run-dynamic-code\">2. Run Dynamic Code<\/h3>\n\n\n\n<p>You can use <code>eval()<\/code> to run more than just math. It can execute any valid Python expression, including function calls or data structure creations.<\/p>\n\n\n\n<p>Here is an example that creates a list from a string:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Create a list from a string\nlist_str = &quot;&#x5B;1, 2, 3, 4, 5]&quot;\nmy_list = eval(list_str)\nprint(my_list)\nprint(type(my_list))\n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;1, 2, 3, 4, 5]\n&amp;lt;class &#039;list&#039;&gt;\n<\/pre><\/div>\n\n\n<p><code>eval()<\/code> takes the string representation of a list and turns it into an actual Python list. This is useful for converting string data into Python objects.<\/p>\n\n\n\n<p>You can also call functions defined in your code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef greet(name):\n    return &quot;Hello, &quot; + name + &quot;!&quot;\n\n# Call a function using eval()\nmessage = eval(&quot;greet(&#039;Alice&#039;)&quot;)\nprint(message)\n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nHello, Alice!\n<\/pre><\/div>\n\n\n<p><code>eval()<\/code> runs the <code>greet()<\/code> function using the string. This shows its power in running dynamic code based on strings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-work-with-scope-globals-and-locals\">3. Work with Scope (<code>globals<\/code> and <code>locals<\/code>)<\/h3>\n\n\n\n<p>The <code>eval()<\/code> function lets you control the environment in which the expression runs. You can use the optional <code>globals<\/code> and <code>locals<\/code> dictionaries to define which variables and functions <code>eval()<\/code> can access.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>globals:<\/strong> This dictionary provides the global variables for the evaluation.<\/li>\n\n\n\n<li><strong>locals:<\/strong> This dictionary provides the local variables for the evaluation.<\/li>\n<\/ul>\n\n\n\n<p>If you do not provide <code>globals<\/code> and <code>locals<\/code>, <code>eval()<\/code> uses the current environment.<\/p>\n\n\n\n<p>Here is an example using <code>globals<\/code> to restrict access:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Define some variables\nglobal_var = 100\nlocal_var = 200\n\n# Create a restricted global dictionary\n# Only &#039;global_var&#039; is available to eval()\nrestricted_globals = {&#039;global_var&#039;: global_var}\n\ntry:\n    # This works because &#039;global_var&#039; is in restricted_globals\n    result1 = eval(&quot;global_var + 50&quot;, restricted_globals)\n    print(f&quot;Result with restricted globals: {result1}&quot;)\n\n    # This will raise an error because &#039;local_var&#039; is not in restricted_globals\n    result2 = eval(&quot;local_var + 50&quot;, restricted_globals)\n    print(f&quot;Result with restricted globals (error expected): {result2}&quot;)\nexcept NameError as e:\n    print(f&quot;Error when accessing unauthorized variable: {e}&quot;)\n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nResult with restricted globals: 150\nError when accessing unauthorized variable: name &#039;local_var&#039; is not defined\n<\/pre><\/div>\n\n\n<p>By providing <code>restricted_globals<\/code>, you control what <code>eval()<\/code> can see. This helps with security.<\/p>\n\n\n\n<p>You can also use <code>locals<\/code> to provide specific variables:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Define local variables for eval()\nexec_locals = {&#039;a&#039;: 10, &#039;b&#039;: 20}\n\n# Evaluate using specific local variables\nresult = eval(&quot;a * b&quot;, {}, exec_locals) # {} means no special globals\nprint(result)\n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n200\n<\/pre><\/div>\n\n\n<p>This shows how you can isolate the <code>eval()<\/code> execution environment. This is important for managing dynamic code safely.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"risks-of-eval\">Risks of <code>eval()<\/code><\/h2>\n\n\n\n<p>While <code>eval()<\/code> is powerful, it carries risks. <strong>Never use <code>eval()<\/code> with untrusted input.<\/strong><\/p>\n\n\n\n<p>If you let a user provide the string for <code>eval()<\/code>, they could type malicious code. This code could:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Delete files from your system.<\/li>\n\n\n\n<li>Access sensitive information.<\/li>\n\n\n\n<li>Run other harmful commands.<\/li>\n<\/ul>\n\n\n\n<p>Consider this example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# RISKY: Do NOT use with untrusted input!\nuser_input = &quot;__import__(&#039;os&#039;).system(&#039;rm -rf \/&#039;)&quot; # Example: delete all files\n\ntry:\n    eval(user_input)\n    print(&quot;Code executed.&quot;)\nexcept Exception as e:\n    print(f&quot;An error occurred: {e}&quot;)\n<\/pre><\/div>\n\n\n<p>This example shows a harmful string. If executed, it would try to delete all files on your system. Always treat user input to <code>eval()<\/code> as a major security risk.<\/p>\n\n\n\n<p>Use <code>eval()<\/code> only when you fully control the input string.<\/p>\n\n\n\n<p>For simple mathematical expressions, <code>ast.literal_eval()<\/code> is a safer alternative. It only evaluates literal structures (strings, numbers, tuples, lists, dicts, booleans, None), preventing arbitrary code execution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u2019s eval() function runs Python code stored as a string. It takes a string, treats it as a Python expression, and then calculates its value. This lets you run code that is not directly written in your program. It helps you execute dynamic code. You can use it to evaluate mathematical expressions or even call [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":20168,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36796],"content_type":[],"class_list":["post-20141","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-python"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Learn eval() Function in Python<\/title>\n<meta name=\"description\" content=\"The eval() function parses the expression passed to it and evaluates it as a Python expression. In other words, it takes a string input, interprets it as a Python expression, and executes it within the program.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"eval() Function in Python\" \/>\n<meta property=\"og:description\" content=\"The eval() function parses the expression passed to it and evaluates it as a Python expression. In other words, it takes a string input, interprets it as a Python expression, and executes it within the program.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-29T04:08:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-11T22:27:10+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"661\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"eval() Function in Python\",\"datePublished\":\"2020-09-29T04:08:02+00:00\",\"dateModified\":\"2025-06-11T22:27:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/\"},\"wordCount\":686,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/shutterstock_1428562595.jpg\",\"keywords\":[\"python\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/\",\"name\":\"Learn eval() Function in Python\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/shutterstock_1428562595.jpg\",\"datePublished\":\"2020-09-29T04:08:02+00:00\",\"dateModified\":\"2025-06-11T22:27:10+00:00\",\"description\":\"The eval() function parses the expression passed to it and evaluates it as a Python expression. In other words, it takes a string input, interprets it as a Python expression, and executes it within the program.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/shutterstock_1428562595.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/shutterstock_1428562595.jpg\",\"width\":1000,\"height\":661,\"caption\":\"eval in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/eval-in-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"eval() Function in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Learn eval() Function in Python","description":"The eval() function parses the expression passed to it and evaluates it as a Python expression. In other words, it takes a string input, interprets it as a Python expression, and executes it within the program.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/","og_locale":"en_US","og_type":"article","og_title":"eval() Function in Python","og_description":"The eval() function parses the expression passed to it and evaluates it as a Python expression. In other words, it takes a string input, interprets it as a Python expression, and executes it within the program.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2020-09-29T04:08:02+00:00","article_modified_time":"2025-06-11T22:27:10+00:00","og_image":[{"width":1000,"height":661,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"eval() Function in Python","datePublished":"2020-09-29T04:08:02+00:00","dateModified":"2025-06-11T22:27:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/"},"wordCount":686,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg","keywords":["python"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/","url":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/","name":"Learn eval() Function in Python","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg","datePublished":"2020-09-29T04:08:02+00:00","dateModified":"2025-06-11T22:27:10+00:00","description":"The eval() function parses the expression passed to it and evaluates it as a Python expression. In other words, it takes a string input, interprets it as a Python expression, and executes it within the program.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg","width":1000,"height":661,"caption":"eval in python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/eval-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"eval() Function in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg",1000,661,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595-300x198.jpg",300,198,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595-768x508.jpg",768,508,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg",1000,661,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg",1000,661,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg",1000,661,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg",640,423,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg",96,63,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/shutterstock_1428562595.jpg",150,99,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"Python\u2019s eval() function runs Python code stored as a string. It takes a string, treats it as a Python expression, and then calculates its value. This lets you run code that is not directly written in your program. It helps you execute dynamic code. You can use it to evaluate mathematical expressions or even call&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/20141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=20141"}],"version-history":[{"count":15,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/20141\/revisions"}],"predecessor-version":[{"id":116964,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/20141\/revisions\/116964"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/20168"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=20141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=20141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=20141"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=20141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}