

{"id":6867,"date":"2018-01-31T12:09:57","date_gmt":"2018-01-31T06:39:57","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=6867"},"modified":"2026-04-13T14:53:59","modified_gmt":"2026-04-13T09:23:59","slug":"python-namespace-and-variable-scope","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/","title":{"rendered":"Python Namespace and Variable Scope &#8211; Local and Global Variables"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1278,&quot;href&quot;:&quot;https:\\\/\\\/techvidvan.com\\\/courses\\\/python-course-english&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250713145243\\\/https:\\\/\\\/techvidvan.com\\\/courses\\\/python-course-english\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 04:35:05&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2025-12-12 05:42:20&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2025-12-15 05:49:39&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2025-12-18 06:05:40&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2025-12-21 06:38:49&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-24 07:58:25&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2025-12-27 08:14:01&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2025-12-30 09:23:27&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-02 13:50:09&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-05 14:45:45&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-08 15:40:20&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-11 17:38:53&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-14 20:22:22&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-18 03:46:37&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-21 05:38:02&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-24 07:35:25&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-27 08:15:24&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-30 15:09:25&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-03 06:31:54&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-06 07:30:29&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-09 08:26:08&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-12 11:17:37&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-15 14:03:28&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-18 14:13:43&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-21 15:29:57&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-24 16:17:25&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-28 07:00:57&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-03 07:20:03&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-06 11:10:42&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-09 13:00:48&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-12 13:03:00&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-15 19:32:01&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-18 23:50:01&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-22 02:44:11&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-25 06:28:30&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-28 06:54:17&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-31 06:59:56&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-03 08:00:46&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-06 12:20:22&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-09 14:25:59&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-12 15:18:36&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-16 02:03:12&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-19 07:06:10&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-22 07:12:30&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-25 09:23:03&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-28 10:36:26&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-01 11:53:00&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-04 12:01:26&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-07 12:36:14&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-10 15:18:07&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-13 16:14:13&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-16 17:13:30&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-20 03:50:51&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-23 04:11:07&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-26 04:55:32&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-29 07:41:02&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-06-01 11:38:39&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-06-04 17:19:39&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-04 17:19:39&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>In this <a href=\"https:\/\/data-flair.training\/blogs\/python-tutorial-for-beginners\/\"><strong>Python Tutorial<\/strong><\/a>, we discuss Python Namespace, Types of Namespace in Python, and Python Variable Scope, with their examples and Python Syntax.<\/p>\n<p>Try this: type \u2018import this\u2019 in the interpreter.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import this<\/pre>\n<p><strong>The Zen of Python, by Tim Peters<\/strong><\/p>\n<p>Beautiful is better than ugly.<br \/>\nExplicit is better than implicit.<br \/>\nSimple is better than complex.<br \/>\nComplex is better than complicated.<br \/>\nFlat is better than nested.<br \/>\nSparse is better than dense.<br \/>\nReadability counts.<br \/>\nSpecial cases aren&#8217;t special enough to break the rules.<br \/>\nAlthough practicality beats purity.<br \/>\nErrors should never pass silently.<br \/>\nUnless explicitly silenced.<br \/>\nIn the face of ambiguity, refuse the temptation to guess.<br \/>\nThere should be one&#8211; and preferably only one &#8211;obvious way to do it.<br \/>\nAlthough that way may not be obvious at first unless you&#8217;re Dutch.<br \/>\nNow is better than never.<br \/>\nAlthough never is often better than *right* now.<br \/>\nIf the implementation is hard to explain, it&#8217;s a bad idea.<br \/>\nIf the implementation is easy to explain, it may be a good idea.<br \/>\nNamespaces are one honking great idea &#8212; let&#8217;s do more of those!<\/p>\n<h3>What is Python Name?<\/h3>\n<p>Before we move on to namespaces in <a href=\"https:\/\/techvidvan.com\/courses\/python-course-english\/\">python<\/a>, let\u2019s talk about names in Python.<\/p>\n<p>A Python name is an identifier- something we use to access a Python object, and in Python, everything\u2019s an object.<\/p>\n<p>We\u2019ll take an example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; rank=1<\/pre>\n<p>Here, \u2018rank\u2019 is the name associated with the Python object 1. To get this object\u2019s address in RAM, we use the id() function.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; id(rank)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">492979856<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; id(1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">492979856<\/div>\n<p>To take a slightly more complex example, we store 2 in a name \u2018a\u2019. Then, we increment it by 1 and associate the name \u2018b\u2019 to the object 2. We keep checking the id as we go.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=2\r\n&gt;&gt;&gt; id(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">492979872<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a+=1\r\n&gt;&gt;&gt; id(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">492979888<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; b=2\r\n&gt;&gt;&gt; id(b)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">492979872<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt;\r\n&gt;&gt;&gt; id(2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">492979872<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; id(3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">492979888<\/div>\n<p>So what\u2019s actually happening? We\u2019ll illustrate.<\/p>\n<div id=\"attachment_6877\" style=\"width: 4374px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Efficient.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6877\" class=\"wp-image-6877 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Efficient.png\" alt=\"Java Namespace - Python Name\" width=\"4364\" height=\"1678\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Efficient.png 4364w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Efficient-150x58.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Efficient-300x115.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Efficient-768x295.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Efficient-1024x394.png 1024w\" sizes=\"auto, (max-width: 4364px) 100vw, 4364px\" \/><\/a><p id=\"caption-attachment-6877\" class=\"wp-caption-text\">Java Namespace &#8211; Python Name<\/p><\/div>\n<p>As you can see, when we set \u2018a\u2019 to 3 and set \u2018b\u2019 to 2, \u2018b\u2019 starts pointing to the object \u2018a\u2019 once pointed to.<\/p>\n<p>Isn\u2019t that quite efficient? It does not have to create another object to hold 2 for b. This dynamic name binding is powerful.<\/p>\n<p>Also, a name can hold any kind of value.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=1\r\n&gt;&gt;&gt; a='one'<\/pre>\n<p>Finally, since everything is an object, so are Python functions. Consequently, you can associate them with names.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; identity=id\r\n&gt;&gt;&gt; identity(2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">492979872<\/div>\n<p>Here, we associate the name \u2018identity\u2019 with the built-in function id().<\/p>\n<p><strong>Bonus Question- Check the following code and figure out what\u2019s happening.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def sayhello(): print('Hello')\r\n&gt;&gt;&gt; hi=sayhello()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Hello<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; hi\r\n&gt;&gt;&gt; type(hi)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;NoneType&#8217;&gt;<\/div>\n<p>Well, since the function does not return anything, we get an object of class \u2018NoneType\u2019.<\/p>\n<p>Of course, None is an object that indicates no value. Did function sayhello() return a value, things would be different.<\/p>\n<p>Let\u2019s take another example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def func1():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(\"Hi\")\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return 1\r\n&gt;&gt;&gt; func2=func1()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Hi<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; func2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; type(func2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;int&#8217;&gt;<\/div>\n<h4>What are Python Namespaces?<\/h4>\n<p>A namespace in Python is a mapping of names to objects. Python has many namespaces: one for built-in names (print, len), one for each module, and one inside every function. They stop names from clashing with each other.<\/p>\n<p>At any instant, different Python namespaces can coexist completely isolated- the isolation ensures that there are no name collisions.<\/p>\n<p>Simply speaking, two namespaces in Python can have the same name without facing any problem. A namespace is implemented as a Python dictionary.<\/p>\n<p>When we start the interpreter, a Python namespace is created for as long as we exist. This holds all built-in names.<\/p>\n<p>It is due to this that Python functions like print() and id() are always available. Also, each module creates its own global namespace in Python.<\/p>\n<p>When you call a function, a local Python namespace is created for all the names in it.<\/p>\n<p>A module has a global namespace. The built-in namespace encloses this.<\/p>\n<p>You can explore the current namespace using dir(), globals(), and locals(). Knowing what names exist and where keeps your programs predictable and free of hidden bugs.<\/p>\n<p>Take a look at the following figure to get a clearer understanding.<\/p>\n<div id=\"attachment_6912\" style=\"width: 1090px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Namespaces-1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6912\" class=\"wp-image-6912 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Namespaces-1.png\" alt=\"Types of Python Namespace\" width=\"1080\" height=\"1080\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Namespaces-1.png 1080w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Namespaces-1-150x150.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Namespaces-1-300x300.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Namespaces-1-768x768.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Namespaces-1-1024x1024.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Namespaces-1-100x100.png 100w\" sizes=\"auto, (max-width: 1080px) 100vw, 1080px\" \/><\/a><p id=\"caption-attachment-6912\" class=\"wp-caption-text\">Types of Python Namespace<\/p><\/div>\n<h4>What is Python Variable Scope?<\/h4>\n<p>Through various Python namespaces, not each can be accessed from every part of the program.<\/p>\n<p>A namespace is in variable scope in a part of a program if it lets you access the Python namespace without having to use a prefix.<\/p>\n<p>At any instant, we have at least three nested Python scopes:<\/p>\n<ol>\n<li>Current function\u2019s\u00a0variable scope- has local names<\/li>\n<li>Module\u2019s variable scope- has global names<\/li>\n<li>The outermost variable scope- has built-in names<\/li>\n<\/ol>\n<p>This is in accordance with the three kinds of namespaces in Python, we just discussed. This also decides the order of searching when a reference is made.<\/p>\n<p><strong>The LEGB rule in Python:<\/strong><\/p>\n<p>Python uses the LEGB rule to find variable names; it searches for them step by step in a specific order.<\/p>\n<ul>\n<li><strong>Local:<\/strong> It consists of names defined inside the current function.<\/li>\n<li><strong>Enclosing:<\/strong> Variables from outer functions are used in inner functions.<\/li>\n<li><strong>Global:<\/strong> Variables that are defined outside all the functions can be used anywhere in the program.<\/li>\n<li><strong>Built-in:<\/strong> It consists of pre-defined names that are always available in Python.<\/li>\n<\/ul>\n<p>The order is- the local Python namespace, the global namespace, the built-in namespace. Also, a nested function creates a nested variable scope inside the outer function\u2019s scope.<\/p>\n<h4>Few Python Namespace Examples<\/h4>\n<p>To further what we said, let\u2019s take an example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=1\r\n&gt;&gt;&gt; def func1():\r\n\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 def func2():\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 c=3<\/pre>\n<p>In this code, \u2018a\u2019 is in the global namespace in python. \u2018b\u2019 is in the local namespace of func1, and \u2018c\u2019 is in the nested local python namespace of func2.<\/p>\n<p>To func2, \u2018c\u2019 is local, \u2018b\u2019 is nonlocal, and \u2018a\u2019 is global. By nonlocal, we mean it isn\u2019t global, but isn\u2019t local either.<\/p>\n<p>Of course, here, you can write \u2018c\u2019, and read both \u2018b\u2019 and \u2018c\u2019. But you can\u2019t access \u2018a\u2019, that would create a new local variable \u2018a\u2019. See this example,<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=1\r\n&gt;&gt;&gt; def func1():\r\n\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 def func3():\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=3\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 c=3\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(f\"a={a}, b={b}, c={c}\")\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 func3()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(f\"b={b}\")\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n&gt;&gt;&gt; func1()\r\na=2, b=3, c=3\r\nb=2\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<p>To deal with this situation, we can use the \u2018global\u2019 and \u2018nonlocal\u2019 keywords.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=1\r\n&gt;&gt;&gt; def func1():\r\n\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 def func3():\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 global 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 nonlocal b\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=3\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 c=3\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(f\"a={a}, b={b}, c={c}\")\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 func3()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(f\"b={b}\")\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n&gt;&gt;&gt; func1()\r\na=2, b=3, c=3\r\nb=3\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<h3>Python Interview Questions on Namespaces and Scopes<\/h3>\n<p>1. What are namespaces and scope in Python?<\/p>\n<p>2. Why are namespace and scope used in Python?<\/p>\n<p>3. Give an example of Python namespace and scope?<\/p>\n<p>4. What is class namespace in Python?<\/p>\n<p>5. What is global namespace in Python?<\/p>\n<h3>Conclusion<\/h3>\n<p>There are three types of Python namespaces- global, local, and built-in. It\u2019s the same with a variable scope in Python.<\/p>\n<p>Also, the \u2018global\u2019 keyword lets us refer to a name in a global scope. Likewise, the \u2018nonlocal\u2019 keyword lets us refer to a name in a nonlocal scope.<\/p>\n<p>If you don\u2019t get something, ask us in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python Tutorial, we discuss Python Namespace, Types of Namespace in Python, and Python Variable Scope, with their examples and Python Syntax. Try this: type \u2018import this\u2019 in the interpreter. &gt;&gt;&gt; import this&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":42111,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[1784,10714,10716,10726,15339,15828],"class_list":["post-6867","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-benefits-of-python-iterator","tag-python-namespace-example","tag-python-namespace","tag-python-numberspaces","tag-variable-scope-in-python","tag-what-is-namespace-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 Namespace and Variable Scope - Local and Global Variables - DataFlair<\/title>\n<meta name=\"description\" content=\"Let&#039;s discover what Python Namespace &amp; Variable Scope are, along with their different types, syntax, and examples.\" \/>\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-namespace-and-variable-scope\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Namespace and Variable Scope - Local and Global Variables - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Let&#039;s discover what Python Namespace &amp; Variable Scope are, along with their different types, syntax, and examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-namespace-and-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-01-31T06:39:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-13T09:23:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Namespace-and-Scope-01-1024x536-1.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=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Namespace and Variable Scope - Local and Global Variables - DataFlair","description":"Let's discover what Python Namespace & Variable Scope are, along with their different types, syntax, and examples.","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-namespace-and-variable-scope\/","og_locale":"en_US","og_type":"article","og_title":"Python Namespace and Variable Scope - Local and Global Variables - DataFlair","og_description":"Let's discover what Python Namespace & Variable Scope are, along with their different types, syntax, and examples.","og_url":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-01-31T06:39:57+00:00","article_modified_time":"2026-04-13T09:23:59+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Namespace-and-Scope-01-1024x536-1.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":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Namespace and Variable Scope &#8211; Local and Global Variables","datePublished":"2018-01-31T06:39:57+00:00","dateModified":"2026-04-13T09:23:59+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/"},"wordCount":1100,"commentCount":9,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Namespace-and-Scope-01-1024x536-1.jpg","keywords":["Benefits of Python Iterator","python Namespace example","python namespace()","python numberspaces","Variable Scope in Python","what is Namespace in Python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/","url":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/","name":"Python Namespace and Variable Scope - Local and Global Variables - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Namespace-and-Scope-01-1024x536-1.jpg","datePublished":"2018-01-31T06:39:57+00:00","dateModified":"2026-04-13T09:23:59+00:00","description":"Let's discover what Python Namespace & Variable Scope are, along with their different types, syntax, and examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-namespace-and-variable-scope\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Namespace-and-Scope-01-1024x536-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Namespace-and-Scope-01-1024x536-1.jpg","width":1200,"height":628,"caption":"Python Namespace and Variable Scope - Local and Global Variables"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-namespace-and-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 Namespace and Variable Scope &#8211; Local and Global Variables"}]},{"@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\/6867","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=6867"}],"version-history":[{"count":17,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6867\/revisions"}],"predecessor-version":[{"id":147576,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6867\/revisions\/147576"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/42111"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=6867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=6867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=6867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}