{"id":7863,"date":"2025-04-24T07:15:48","date_gmt":"2025-04-24T07:15:48","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=7863"},"modified":"2026-01-19T06:35:07","modified_gmt":"2026-01-19T06:35:07","slug":"how-to-check-if-variable-is-defined-in-python","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/","title":{"rendered":"How to Check if Variable is Defined in Python"},"content":{"rendered":"\n<p>Every Python program heavily relies on variables to store data. If these variables have not been defined when you access them, then your program will give error and may even stop running. In order to avoid such situations, it is important to check if variable is defined in Python, before you use it. Then you can define ways to gracefully deal with situations where undefined variables are accessed.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#How_to_Check_if_Variable_is_Defined_in_Python\" >How to Check if Variable is Defined in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#1_Using_try-except\" >1. Using try-except<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#2_Using_globals\" >2. Using globals()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#3_Using_locals\" >3. Using locals()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#4_Using_hasattr\" >4. Using hasattr<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#5_Using_None\" >5. Using None<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Check_if_Variable_is_Defined_in_Python\"><\/span>How to Check if Variable is Defined in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are the different ways to check if a variable exists in Python program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_try-except\"><\/span>1. Using try-except<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>By default, when you try to access a variable that has not been defined, such as variable &#8216;a&#8217;, you will get a NameError as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">NameError: name 'a' is not defined<\/pre>\n\n\n\n<p>In Python, you can catch all such errors and exceptions by wrapping the code in a try-except block.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    print(a)<br>except NameError:<br>    print(\"a is not defined\")<br>else:<br>    print(\"a is defined\")<\/pre>\n\n\n\n<p>In the above code, we have written the print() function inside try block. If the variable a is not defined, then this statement will throw an exception. In this case, the except block will catch it and display &#8216;a is not defined&#8217;. On the other hand, if the variable is indeed defined then it will directly execute the last statement and display &#8216;a is defined&#8217;.<\/p>\n\n\n\n<p>Using try-except block is a very good and recommended way to catch all kinds of errors and handle them effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_globals\"><\/span>2. Using globals()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Every Python program or script supports a built-in function called globals() that returns a dictionary of all the global variables defined in the program. You can use it to check if a variable is defined globally or not.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = 24<br>if 'a' in globals():<br>    print(\"'a' is defined globally\")<br>else:<br>    print(\"'a' is not defined globally\")<br><br>## output <br>'a' is defined globally<\/pre>\n\n\n\n<p>Here is what the output of globals() looks like.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{'__name__': '__main__', ..., 'a': 24}<\/pre>\n\n\n\n<p>In the above code, we use &#8216;in&#8217; clause to check if variable &#8216;a&#8217; is present in the output of globals(). If the variable is defined globally in your code, then it will be present as a key-value pair in the output. Please note, we have enclosed the variable name in single quotes (&#8216;a&#8217;). If you do not use quotes, then it will not work properly.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = 24<br>if a in globals():<br>    print(\"'a' is defined globally\")<br>else:<br>    print(\"'a' is not defined globally\")<br><br>## output <br>'a' is not defined globally<\/pre>\n\n\n\n<p>Also, the output of globals() will not contain any local variables. So if you check for the existence of a local variable here, it will fail.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = 24<br><br>def abc():<br>    b=10<br>    if 'b' in globals():<br>        print(\"'a' is defined globally\")<br>    else:<br>        print(\"'a' is not defined globally\")<br>        <br>abc() <br><br>## output <br><br>'a' is not defined globally<\/pre>\n\n\n\n<p>If you need to check if a variable is locally defined or not, you need to use locals() function as described below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_locals\"><\/span>3. Using locals()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you want to check for the presence of a local variable, then you can use locals() function. It returns an dictionary of all local variables. We use the same logic we used earlier to check for global variables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = 24<br><br>def abc():<br>    b=10<br>    if 'b' in locals():<br>        print(\"'b' is defined locally\")<br>    else:<br>        print(\"'b' is not defined locally\")<br>        <br>abc()        <\/pre>\n\n\n\n<p>In the above function, we define local variable &#8216;b&#8217; and check if it is present in the result of locals() function, using &#8216;in&#8217; clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Using_hasattr\"><\/span>4. Using hasattr<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So far, all the examples we have seen are about simple variables in Python. What if you want to check if a specific class attribute is defined or not? In this case, you can use <a href=\"https:\/\/www.programiz.com\/python-programming\/methods\/built-in\/hasattr\">hasattr()<\/a> function. The hasattr() function is used to check if an object has a specific attribute or not in Python. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">hasattr(object, attribute)<\/pre>\n\n\n\n<p>hasattr() function accepts two arguments &#8211; name of object and the name of attribute. It returns true if the attribute is present in object, else it returns false. Here is an example to demonstrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class my_class:<br>    def __init__(self):<br>        self.test = \"value\"<br><br>obj = my_class()<br><br>if hasattr(obj, 'test'):<br>    print(\"test is defined in obj\")<br>else:<br>    print(\"test is not defined in obj\")<br><br>## output<br><br>attribute is defined in obj<\/pre>\n\n\n\n<p>In the above code, we have defined a class my_class. This class has an attribute test. We create an object obj using this class. Then we call hasattr() function, that accepts two arguments. The first argument is the name of object and the second attribute is the name of the attribute.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Using_None\"><\/span>5. Using None<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this solution, you can set a variable to None beforehand and then check to see if the variable is None or not. This is useful if you want to differentiate between a variable that has been explicitly set to None, and one that has not been defined at all.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a=None<br><br>if a is None:<br>    print(\"a is None\")<br>else:<br>    print(\"a is defined\")<br><br>## output <br>a is None<\/pre>\n\n\n\n<p>This solution will help you prevent errors and exceptions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt how to check if a variable is defined or not. You can use try-except block to catch errors in any block of code, including &#8216;variable not defined&#8217; ones. If you want to check if a variable is defined as a global variable or not, use globals() function. If you want to check if a variable is defined as a local variable or not, use locals() function.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-reverse-string-in-python\/\">How to Reverse String in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-compare-strings-in-python\/\">How to Compare Strings in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-remove-characters-from-string-in-python\/\">How to Remove Characters from String<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes Python developers need to check if a variable is defined or not in Python. Here are different ways to do this.<\/p>\n","protected":false},"author":1,"featured_media":7877,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[414],"class_list":["post-7863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-variable-check"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Check if Variable is Defined in Python - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Sometimes Python developers need to check if a variable is defined or not in Python. Here are different ways to do this.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Check if Variable is Defined in Python - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Sometimes Python developers need to check if a variable is defined or not in Python. Here are different ways to do this.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-24T07:15:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-01-19T06:35:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/check-if-variable-is-defined-in-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\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:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Check if Variable is Defined in Python\",\"datePublished\":\"2025-04-24T07:15:48+00:00\",\"dateModified\":\"2026-01-19T06:35:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/\"},\"wordCount\":760,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/check-if-variable-is-defined-in-python.jpg?fit=300%2C200&ssl=1\",\"keywords\":[\"variable check\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/\",\"name\":\"How to Check if Variable is Defined in Python - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/check-if-variable-is-defined-in-python.jpg?fit=300%2C200&ssl=1\",\"datePublished\":\"2025-04-24T07:15:48+00:00\",\"dateModified\":\"2026-01-19T06:35:07+00:00\",\"author\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Sometimes Python developers need to check if a variable is defined or not in Python. Here are different ways to do this.\",\"breadcrumb\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/check-if-variable-is-defined-in-python.jpg?fit=300%2C200&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/check-if-variable-is-defined-in-python.jpg?fit=300%2C200&ssl=1\",\"width\":300,\"height\":200,\"caption\":\"check if variable is defined in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ubiq.co\/tech-blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Check if Variable is Defined in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/\"],\"url\":\"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Check if Variable is Defined in Python - Ubiq BI","description":"Sometimes Python developers need to check if a variable is defined or not in Python. Here are different ways to do this.","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:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Check if Variable is Defined in Python - Ubiq BI","og_description":"Sometimes Python developers need to check if a variable is defined or not in Python. Here are different ways to do this.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2025-04-24T07:15:48+00:00","article_modified_time":"2026-01-19T06:35:07+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/check-if-variable-is-defined-in-python.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Check if Variable is Defined in Python","datePublished":"2025-04-24T07:15:48+00:00","dateModified":"2026-01-19T06:35:07+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/"},"wordCount":760,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/check-if-variable-is-defined-in-python.jpg?fit=300%2C200&ssl=1","keywords":["variable check"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/","name":"How to Check if Variable is Defined in Python - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/check-if-variable-is-defined-in-python.jpg?fit=300%2C200&ssl=1","datePublished":"2025-04-24T07:15:48+00:00","dateModified":"2026-01-19T06:35:07+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Sometimes Python developers need to check if a variable is defined or not in Python. Here are different ways to do this.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/check-if-variable-is-defined-in-python.jpg?fit=300%2C200&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/check-if-variable-is-defined-in-python.jpg?fit=300%2C200&ssl=1","width":300,"height":200,"caption":"check if variable is defined in python"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-variable-is-defined-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Check if Variable is Defined in Python"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/check-if-variable-is-defined-in-python.jpg?fit=300%2C200&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-22P","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/7863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=7863"}],"version-history":[{"count":19,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/7863\/revisions"}],"predecessor-version":[{"id":10309,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/7863\/revisions\/10309"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/7877"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=7863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=7863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=7863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}