

{"id":6924,"date":"2018-02-01T07:18:34","date_gmt":"2018-02-01T07:18:34","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=6924"},"modified":"2026-04-13T11:59:23","modified_gmt":"2026-04-13T06:29:23","slug":"python-variable-scope","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/","title":{"rendered":"Python Variable Scope &#8211; Local, Global, Built-in, Enclosed"},"content":{"rendered":"<p>Today, we will discuss Python Variable Scope. Here, we will learn different types of variable scope in Python, the Python Global Keyword, and Python Non-local keywords.<\/p>\n<p>So, let&#8217;s start our learning.<\/p>\n<h3>What is Python Variable Scope?<\/h3>\n<p>Scope means where a variable can be seen in your code. A variable created inside a function lives only inside that function; this is called local scope. Once the function finishes, the local variable disappears.<\/p>\n<p>Variables written outside all functions sit in the global scope. Any function can read them, but changing them inside a function needs the global keyword. Using too many globals can confuse larger programs.<\/p>\n<p>Let\u2019s take an example, but before, let&#8217;s revise <b>Python syntax<\/b>.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; b=8\r\n&gt;&gt;&gt; def func():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a=7\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 print(b)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\r\n&gt;&gt;&gt; func()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">7<br \/>\n8<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\nFile &#8220;&lt;pyshell#6&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\na<br \/>\nNameError: name &#8216;a&#8217; is not defined<\/div>\n<p>Also, the duration for which a variable is alive is called its \u2018lifetime\u2019.<\/p>\n<h3>Types of Python Variable Scope<\/h3>\n<p>There are 4 types of Variable Scope in <strong>Python<\/strong>, let&#8217;s discuss them one by one:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Scopes-01.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-6944 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Scopes-01.jpg\" alt=\"types of Python Variable Scope\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Scopes-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Scopes-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Scopes-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Scopes-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Scopes-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a> Python Variable Scope &#8211; Types<\/p>\n<h4>1. Local Scope in Python<\/h4>\n<p>In the above code, we define a variable \u2018a\u2019 in a function \u2018func\u2019. So, \u2018a\u2019 is local to \u2018func\u2019. Hence, we can read\/write it in func, but not outside it.<\/p>\n<p>When we try to do so, it raises a NameError. Look at this code.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=0\r\n&gt;&gt;&gt; def func():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a=1\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\r\n&gt;&gt;&gt; func()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\nFile &#8220;&lt;pyshell#79&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\nfunc()<br \/>\nFile &#8220;&lt;pyshell#78&gt;&#8221;, line 2, in func<br \/>\nprint(a)<br \/>\nUnboundLocalError: local variable &#8216;a&#8217; referenced before assignment<\/div>\n<p>Here, we could\u2019ve accessed the global Scope \u2018a\u2019 inside \u2018func\u2019, but since we also define a local \u2018a\u2019 in it, it no longer accesses the global \u2018a\u2019.<\/p>\n<p>Then, the first print statement inside \u2018func\u2019 throws an<strong> error in Python<\/strong>, because we\u2019re trying to access the local scope \u2018a\u2019 before assigning it.<\/p>\n<p>However, it is a bad practice to try to manipulate global values from inside local scopes. Try to pass it as a parameter to the function.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def func(a=0):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a+=1\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\r\n&gt;&gt;&gt; func()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<h4>2. Global Scope in Python<\/h4>\n<p>We also declare a variable \u2018b\u2019 outside any other Python variable scope, which makes it a global scope.<\/p>\n<p>Consequently, we can read it anywhere in the program. Later in this article, we will see how to write it inside func.<\/p>\n<h4>3. Enclosing Scope in Python<\/h4>\n<p>Let\u2019s take another example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def red():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a=1\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 def blue():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 b=2\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(b)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 blue()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\r\n&gt;&gt;&gt; red()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n1<\/div>\n<p>In this code, \u2018b\u2019 has local scope in the <strong>Python function<\/strong> \u2018blue\u2019, and \u2018a\u2019 has nonlocal scope in \u2018blue\u2019.<\/p>\n<p>Of course, a Python variable scope that isn\u2019t global or local is nonlocal. This is also called the enclosing scope.<\/p>\n<h4>4. Built-in Scope in Python<\/h4>\n<p>Finally, we talk about the widest scope. The built-in scope has all the names that are loaded into the scope of Python variables\u00a0when we start the interpreter.<\/p>\n<p><strong>Characteristics of the built-in scope in Python:<\/strong><\/p>\n<ul>\n<li><strong>Available Everywhere:<\/strong> Python Functions like print(), len(), and range () can be used anywhere without importing modules.<\/li>\n<li><strong>Always Active:<\/strong> Built-in names always stay available as long as the program is running.<\/li>\n<li><strong>Checked Last:<\/strong> Python searches this scope only after checking local, enclosing, and global scopes.<\/li>\n<li><strong>Global Access:<\/strong> No need to import or define these functions.<\/li>\n<\/ul>\n<h3>Global Keyword in Python<\/h3>\n<p>So far, we haven\u2019t had any kind of problem with global scope. So let\u2019s take an example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=1\r\n&gt;&gt;&gt; def counter():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a=2\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\u00a0\u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u00a0\u00a0\r\n&gt;&gt;&gt; counter()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<p>Now, when we make a reference to \u2018a\u2019 outside this function, we get 1 instead of 2.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<p>Why does this happen? Well, this is because when we set \u2018a\u2019 to 2, it created a local variable \u2018a\u2019 in the local scope of \u2018counter\u2019.<\/p>\n<p>This didn\u2019t change anything for the global \u2018a\u2019. Now, what if you wanted to change the global version of \u2018a\u2019? We use the \u2018global\u2019 keyword in Python for this.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=1\r\n&gt;&gt;&gt; def counter():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 global a\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a=2\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\u00a0\u00a0 \u00a0\r\n&gt;&gt;&gt; counter()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<p>What we do here is, we declare that the \u2018a\u2019 we\u2019re going to use in this function is from the global scope.<\/p>\n<p>After this, whenever we refer to \u2018a\u2019 inside \u2018counter\u2019, the interpreter knows we\u2019re talking about the global \u2018a\u2019.<\/p>\n<p>In this example, it changed the value of the global \u2018a\u2019 to 2.<\/p>\n<h3>Nonlocal Keyword in Python<\/h3>\n<p>Like the \u2018global\u2019 keyword, you want to make a change to a nonlocal variable, you must use the \u2018nonlocal\u2019 keyword. Let\u2019s first try this without the keyword.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def red():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a=1\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 def blue():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a=2\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 b=2\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(b)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 blue()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\u00a0 \u00a0  \u00a0 \u00a0 \u00a0\u00a0\r\n&gt;&gt;&gt; red()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<br \/>\n2<br \/>\n1<\/div>\n<p>As you can see, this did not change the value of \u2018a\u2019 outside the function \u2018blue\u2019. To be able to do that, we use \u2018nonlocal\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def red():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a=1\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 def blue():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nonlocal a\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a=2\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 b=2\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(b)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 blue()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(a)\u00a0 \u00a0  \u00a0 \u00a0 \u00a0 \u00a0\u00a0\r\n&gt;&gt;&gt; red()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<br \/>\n2<br \/>\n2<\/div>\n<p>See? This works perfectly fine.<\/p>\n<h3>Python Interview Question on Python Variable Scope<\/h3>\n<p>1. What do you mean by scope?<\/p>\n<p>2. What are the types of variable scope in Python?<\/p>\n<p>3. What is the scope of a variable in Python with an example?<\/p>\n<p>4. What are Python variables?<\/p>\n<p>5. How do you declare a variable in Python 3?<\/p>\n<h3>Conclusion<\/h3>\n<p>We hope that the variable scope of Python is clearer to you. Here, we saw four types of variable scope in Python: local scope, enclosed scope, global scope, and built-in scope.<\/p>\n<p>We also saw two keywords in Python &#8211; \u2018global\u2019 and \u2018nonlocal\u2019. Hope you had fun, see you again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, we will discuss Python Variable Scope. Here, we will learn different types of variable scope in Python, the Python Global Keyword, and Python Non-local keywords. So, let&#8217;s start our learning. What is Python&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":6955,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[2225,4146,5105,5109,8362,9113,10674,15100,15339],"class_list":["post-6924","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-built-in-scope","tag-enclosed-scope","tag-global-keyword-in-python","tag-global-vs-local-variable-in-python","tag-local-scope-defination","tag-non-local-keyword","tag-python-main-variable-scope","tag-types-of-python-variable-scope","tag-variable-scope-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Variable Scope - Local, Global, Built-in, Enclosed - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn Python Variable Scope-types of Variable scope: local scope, global scope, enclosed scope, built-in scope; Global keyword in python etc\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-variable-scope\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Variable Scope - Local, Global, Built-in, Enclosed - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn Python Variable Scope-types of Variable scope: local scope, global scope, enclosed scope, built-in scope; Global keyword in python etc\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-variable-scope\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-01T07:18:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-13T06:29:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Variable-Scope-01.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=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Variable Scope - Local, Global, Built-in, Enclosed - DataFlair","description":"Learn Python Variable Scope-types of Variable scope: local scope, global scope, enclosed scope, built-in scope; Global keyword in python etc","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:\/\/data-flair.training\/blogs\/python-variable-scope\/","og_locale":"en_US","og_type":"article","og_title":"Python Variable Scope - Local, Global, Built-in, Enclosed - DataFlair","og_description":"Learn Python Variable Scope-types of Variable scope: local scope, global scope, enclosed scope, built-in scope; Global keyword in python etc","og_url":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-02-01T07:18:34+00:00","article_modified_time":"2026-04-13T06:29:23+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Variable-Scope-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Variable Scope &#8211; Local, Global, Built-in, Enclosed","datePublished":"2018-02-01T07:18:34+00:00","dateModified":"2026-04-13T06:29:23+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/"},"wordCount":845,"commentCount":23,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Variable-Scope-01.jpg","keywords":["built-in scope","enclosed scope","Global Keyword in Python","global vs local variable in python","local scope defination","non-local keyword","python main variable scope","types of python Variable scope","Variable Scope in Python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-variable-scope\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/","url":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/","name":"Python Variable Scope - Local, Global, Built-in, Enclosed - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Variable-Scope-01.jpg","datePublished":"2018-02-01T07:18:34+00:00","dateModified":"2026-04-13T06:29:23+00:00","description":"Learn Python Variable Scope-types of Variable scope: local scope, global scope, enclosed scope, built-in scope; Global keyword in python etc","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-variable-scope\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Variable-Scope-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Variable-Scope-01.jpg","width":1200,"height":628,"caption":"Python Variable Scope - Introduction"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-variable-scope\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Variable Scope &#8211; Local, Global, Built-in, Enclosed"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=6924"}],"version-history":[{"count":13,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6924\/revisions"}],"predecessor-version":[{"id":147572,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6924\/revisions\/147572"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/6955"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=6924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=6924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=6924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}