{"id":1744,"date":"2024-08-03T13:18:24","date_gmt":"2024-08-03T07:48:24","guid":{"rendered":"https:\/\/geekpython.in\/?p=1744"},"modified":"2024-08-03T13:18:25","modified_gmt":"2024-08-03T07:48:25","slug":"exec-and-eval","status":"publish","type":"post","link":"https:\/\/geekpython.in\/exec-and-eval","title":{"rendered":"Difference between exec() and eval() with Examples"},"content":{"rendered":"\n<p>Both functions have a common objective: <strong>to execute Python code from the string input or code object<\/strong>. Even though they both have the same objective, <code>exec()<\/code> and <code>eval()<\/code> are not the same.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Return Values<\/h3>\n\n\n\n<p>The <code>exec()<\/code> function doesn&#8217;t return any value whereas the <code>eval()<\/code> function returns a value computed from the expression.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >expression = \"3 + 5\"\n\nresult_eval = eval(expression)\nprint(result_eval)\n\nresult_exec = exec(expression)\nprint(result_exec)<\/pre><\/div>\n\n\n\n<p>When we run this code, we&#8217;ll get <code>8<\/code> and <code>None<\/code>. This means that <code>eval(expression)<\/code> evaluated the result and stored it inside <code>result_eval<\/code> whereas <code>exec(expression)<\/code> returned nothing, so the value <code>None<\/code> gets stored into <code>result_exec<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \" >8\nNone<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Execution<\/h3>\n\n\n\n<p>The <code>exec()<\/code> function is capable of executing multi-line and multi-statment code, it doesn&#8217;t matter whether the code is simple, complex, has loops and conditions, classes, and functions.<\/p>\n\n\n\n<p>On the other hand, the <code>eval()<\/code> function is restricted to executing the single-line code which might be a simple or complex expression.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >expression = \"\"\"\nfor x in range(5):\n    print(x, end=\" \")\n\"\"\"\n\nexec(expression)\neval(expression)<\/pre><\/div>\n\n\n\n<p>Look at this code, we have a multi-line code that prints numbers up to the given range. The <code>exec(expression)<\/code> will execute it and display the result but the <code>eval(expression)<\/code> will display the error.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \" >0 1 2 3 4\nSyntaxError: invalid syntax<\/pre><\/div>\n\n\n\n<p>However, if we convert the same expression into a single line as the following, the <code>eval(expression)<\/code> will not throw any error.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >expression = \"[print(x, end=' ') for x in range(5)]\"\neval(expression)\n\n--------------------\n0 1 2 3 4<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-center\" data-align=\"center\"><code>eval()<\/code><\/th><th class=\"has-text-align-center\" data-align=\"center\"><code>exec()<\/code><\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">Returns a value<\/td><td class=\"has-text-align-center\" data-align=\"center\">Doesn&#8217;t return any value<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Executes a single expression, it might be simple or complex<\/td><td class=\"has-text-align-center\" data-align=\"center\">Capable of executing multi-statement and multi-line expressions containing loops, conditions, functions, and classes<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Use <code>eval()<\/code> when you need to evaluate a single expression and use its result.<\/td><td class=\"has-text-align-center\" data-align=\"center\">Use <code>exec()<\/code> when you need to execute complex code blocks or multiple statements.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/exec-function-in-python\">Execute complex code blocks from string input using exec() function<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/template-inheritance-in-flask\">Template inheritance in Flask<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/type-hinting-in-python\">Type hints in Python &#8211; Callable objects, Return values, and more<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/positional-and-keyword-arguments-in-python\">Best Practices: Positional and Keyword Arguments in Python<\/a><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/yield-keyword-in-python\">Yield Keyword in Python with Examples<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/build-websocket-server-and-client-using-python\">Create a WebSocket Server and Client in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Both functions have a common objective: to execute Python code from the string input or code object. Even though they both have the same objective, exec() and eval() are not the same. Return Values The exec() function doesn&#8217;t return any value whereas the eval() function returns a value computed from the expression. When we run [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1750,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[41,2],"tags":[40,31],"class_list":["post-1744","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-function","category-python","tag-functions","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1744"}],"version-history":[{"count":4,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1744\/revisions"}],"predecessor-version":[{"id":1748,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1744\/revisions\/1748"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1750"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}