{"id":80024,"date":"2020-10-21T09:00:35","date_gmt":"2020-10-21T03:30:35","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=80024"},"modified":"2020-10-21T09:00:35","modified_gmt":"2020-10-21T03:30:35","slug":"python-eval-function","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/","title":{"rendered":"Python Eval function &#8211; Example and Uses"},"content":{"rendered":"<p>Do you ever dream of putting a queen robe in an old fairy basket and suddenly wishing it to become a trendy gold studded robe? This is, of course, a dream coming true, but what if I tell you this can actually happen.<\/p>\n<p>Surprised? This fairy basket if replaced by our buddy python, then I&#8217;m not afraid to tell you all that it can actually do this. How? Here is an article where our buddy Python just suddenly turns out to be a fairy for it&#8217;s coder with a magic wand spelling out a magic rhyme. Let us learn about Python Eval Function.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/09\/Python-Eval-function.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80038\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/09\/Python-Eval-function.jpg\" alt=\"Python Eval function\" width=\"1200\" height=\"628\" \/><\/a><\/p>\n<h2>Python Eval Function<\/h2>\n<p>Speak after me<\/p>\n<p><strong>&#8216; Dear coder, after this nobody will be your rival<\/strong><br \/>\n<strong>Lemme tell you a secret of eval. \u2018<\/strong><\/p>\n<p>As I had stated earlier, Python uses Eval as a magic spell, but for what? So technically &#8216;eval function parses the expression argument and evaluates it as a python expression&#8217;. But in a straight and simple language, it evaluates the simple old robe and generates a completely different yet required gold studded one. This simple robe is STRING and this new robe is INTEGER.<\/p>\n<p><strong>The syntax of the Eval function is:<\/strong><\/p>\n<ul>\n<li>eval(expression, [globals[, locals]])<\/li>\n<\/ul>\n<p><strong>For Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def drivingage(age):\n    if age&gt;18:\n        print(\"yes\")\n    else:\n        print(\"no\")\nage=int(input(\"age\"))\ndrivingage(age)\n\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">age 6<br \/>\nno<\/div>\n<h3>Argument\u00a0 and Parameters of Eval Function in Python<\/h3>\n<p>The arguments or parameters of eval function are STRING (global and locals can be used as an argument inside eval function), but the globals must be represented as a dictionary and the locals as a mapped object. This is very same as the robe can be of silk or cotton but silk should be ____ while cotton should be ____<\/p>\n<p><strong>Return Value of Python Eval Function<\/strong><\/p>\n<p>The result\/return value will mostly be integer. Its like &#8220;Use any (silk\/cotton) you&#8217;ll get a gold studded robe only&#8221;.<\/p>\n<p>Let&#8217;s have a look at the basic code :<\/p>\n<p><strong>Example of Python Eval Function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">a=int(input(\u201center a number of choice\u201d))\nif:\n   a = 1\n   print(eval('a + 1'))\nElse:\n   break()\nprint(\u201cthe program is executed\u201d)\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<p><strong>Python Eval Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">a=eval(\u201c2** 5\u201d))\nprint(a)\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">32<\/div>\n<p><strong>Python Eval() Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">a=eval(\"sum([9,7])\")\nprint(a)\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">16<\/div>\n<p>Python making a cool robe has an easy code though \ud83d\ude42<\/p>\n<h4>Where do we use EVAL()?<\/h4>\n<p>To answer this question, the basic answer is Python, but the technical answer is in that code of Python where the coder wants to evaluate mathematical expression or evaluation of string.<\/p>\n<h4>WHEN should a coder use Eval?<\/h4>\n<p>Not every spell is useful everywhere similarly, eval should be used in scripting in a Python program for real-time evaluation.<\/p>\n<h3>Python Eval() Parameters<\/h3>\n<p>The eval() function takes three parameters:<\/p>\n<ul>\n<li>expression &#8211; string parsed and evaluated as a Python expression<\/li>\n<li>globals (optional) &#8211; is a dictionary<\/li>\n<li>locals (optional)- mapping object. Dictionary is the standard and is a commonly used mapping type in Python.<\/li>\n<\/ul>\n<h4>Exploiting Eval in Python<\/h4>\n<p>Having a function in your code that returns some kind of password or other confidential data can be serious. A user can make a call to this function through Python eval() and make their way to sensitive data. How would you like a user to be able to read and write your files, or worse, delete them or even redo them? This is possible using the command os.system(\u2018rm -rf *\u2019). This poses higher risks when working with applications like web apps and kiosk computers.<\/p>\n<h3>Warnings while using eval() in Python<\/h3>\n<p>A situation where you are using a Unix system (macOS, Linux etc) and you have imported the os module. The os module provides a portable way to use operating system functionalities such as reading or writing to a file.<\/p>\n<h2>Summary<\/h2>\n<p>This fairy basket has so much more to offer, and Eval was just one magical spell. We&#8217;ll learn from others too. Having learned so much about Eval that the need to have a fairy wand is so much less now. Haha, so let&#8217;s end this topic with a concentrated read, practicing a code, learning while relaxing and enjoying coding. Hope this article proved to be beneficial for everyone searching for the usage of eval.<\/p>\n<p>Thank you for a good read, keep coding, keep learning, and also keep understanding the fairy basket. Happy Pythoning \ud83d\ude42<\/p>\n<p><strong>Do share your <a href=\"https:\/\/g.page\/techvidvan?gm\">feedback for TechVidvan<\/a> if you liked the article.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you ever dream of putting a queen robe in an old fairy basket and suddenly wishing it to become a trendy gold studded robe? This is, of course, a dream coming true, but&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":80038,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[3310,3311,3312,3313],"class_list":["post-80024","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-eval-function-in-python","tag-eval-in-python","tag-python-eval","tag-python-eval-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Eval function - Example and Uses - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn What is eval() in python &amp; what is its syntax with Examples.Learn where and how to use Python Eval Function for easy python programming\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Eval function - Example and Uses - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn What is eval() in python &amp; what is its syntax with Examples.Learn where and how to use Python Eval Function for easy python programming\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-21T03:30:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Python-Eval-function.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Eval function - Example and Uses - TechVidvan","description":"Learn What is eval() in python & what is its syntax with Examples.Learn where and how to use Python Eval Function for easy python programming","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:\/\/techvidvan.com\/tutorials\/python-eval-function\/","og_locale":"en_US","og_type":"article","og_title":"Python Eval function - Example and Uses - TechVidvan","og_description":"Learn What is eval() in python & what is its syntax with Examples.Learn where and how to use Python Eval Function for easy python programming","og_url":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-10-21T03:30:35+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Python-Eval-function.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Eval function &#8211; Example and Uses","datePublished":"2020-10-21T03:30:35+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/"},"wordCount":687,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Python-Eval-function.jpg","keywords":["Eval Function in Python","Eval() in python","Python eval","python eval function"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-eval-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/","url":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/","name":"Python Eval function - Example and Uses - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Python-Eval-function.jpg","datePublished":"2020-10-21T03:30:35+00:00","description":"Learn What is eval() in python & what is its syntax with Examples.Learn where and how to use Python Eval Function for easy python programming","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-eval-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Python-Eval-function.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Python-Eval-function.jpg","width":1200,"height":628,"caption":"Python Eval function"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-eval-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Eval function &#8211; Example and Uses"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=80024"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80024\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/80038"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=80024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=80024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=80024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}