{"id":68,"date":"2026-03-01T16:11:59","date_gmt":"2026-03-01T11:11:59","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=68"},"modified":"2026-03-13T10:46:05","modified_gmt":"2026-03-13T05:46:05","slug":"scope-and-lifetime-of-variables","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/scope-and-lifetime-of-variables\/","title":{"rendered":"Scope and Lifetime of Variables"},"content":{"rendered":"\n<p>Understanding <strong>scope<\/strong> and <strong>lifetime<\/strong> of variables is very important in Python. It helps you control where variables can be accessed and how long they exist in memory.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHAT IS SCOPE?<\/strong><\/h1>\n\n\n\n<p>Scope refers to the region of a program where a variable is accessible.<\/p>\n\n\n\n<p>There are mainly four types of scope in Python:<\/p>\n\n\n\n<p>\u2022 Local Scope<br>\u2022 Enclosing Scope<br>\u2022 Global Scope<br>\u2022 Built-in Scope<\/p>\n\n\n\n<p>This is also known as the <strong>LEGB rule<\/strong>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>1. LOCAL SCOPE<\/strong><\/h1>\n\n\n\n<p>A variable defined inside a function has local scope. It can only be accessed inside that function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def greet():<br>    message = \"Hello\"<br>    print(message)greet()<\/pre>\n\n\n\n<p>If you try to access <code>message<\/code> outside the function, it will give an error.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>2. GLOBAL SCOPE<\/strong><\/h1>\n\n\n\n<p>A variable defined outside all functions has global scope. It can be accessed anywhere in the program.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">name = \"Hira\"def display():<br>    print(name)display()<\/pre>\n\n\n\n<p>Global variables exist throughout the program.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>MODIFYING GLOBAL VARIABLES<\/strong><\/h1>\n\n\n\n<p>To modify a global variable inside a function, use the <code>global<\/code> keyword.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">count = 0def update():<br>    global count<br>    count += 1update()<br>print(count)<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>3. ENCLOSING SCOPE<\/strong><\/h1>\n\n\n\n<p>Enclosing scope appears in nested functions. It refers to variables in the outer function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def outer():<br>    message = \"Hello\"    def inner():<br>        print(message)    inner()outer()<\/pre>\n\n\n\n<p>The <code>inner()<\/code> function can access variables from <code>outer()<\/code>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>4. BUILT-IN SCOPE<\/strong><\/h1>\n\n\n\n<p>Built-in scope includes predefined names in Python like <code>print<\/code>, <code>len<\/code>, and <code>type<\/code>. These are always available.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(len(\"Python\"))<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHAT IS LIFETIME OF A VARIABLE?<\/strong><\/h1>\n\n\n\n<p>Lifetime refers to how long a variable exists in memory.<\/p>\n\n\n\n<p>\u2022 Local variables exist only while the function is running<br>\u2022 Global variables exist until the program ends<br>\u2022 Variables are destroyed when they go out of scope<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def test():<br>    x = 10<br>    print(x)test()<\/pre>\n\n\n\n<p>After the function finishes, <code>x<\/code> no longer exists.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>IMPORTANT POINTS<\/strong><\/h1>\n\n\n\n<p>\u2022 Python follows the LEGB rule to search for variables<br>\u2022 Avoid using too many global variables<br>\u2022 Local variables are safer and better practice<br>\u2022 Use <code>nonlocal<\/code> keyword to modify enclosing variables<\/p>\n\n\n\n<p>Example of nonlocal:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def outer():<br>    x = 10    def inner():<br>        nonlocal x<br>        x += 5<br>        print(x)    inner()outer()<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHY SCOPE AND LIFETIME ARE IMPORTANT<\/strong><\/h1>\n\n\n\n<p>\u2022 Prevent variable conflicts<br>\u2022 Improve code readability<br>\u2022 Help manage memory efficiently<br>\u2022 Make large programs easier to control<\/p>\n\n\n\n<p>Understanding scope and lifetime helps you write structured and professional Python programs.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FUNDAMENTALS (PYF) > Functions > Scope and Lifetime of Variables<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773380759015\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":25,"template":"","class_list":["post-68","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Scope and Lifetime of Variables - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python variable scope &amp; lifetime: understand local, global, enclosing, and built-in variables for clean, efficient code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/scope-and-lifetime-of-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scope and Lifetime of Variables - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python variable scope &amp; lifetime: understand local, global, enclosing, and built-in variables for clean, efficient code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/scope-and-lifetime-of-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-13T05:46:05+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/scope-and-lifetime-of-variables\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/scope-and-lifetime-of-variables\\\/\",\"name\":\"Scope and Lifetime of Variables - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T11:11:59+00:00\",\"dateModified\":\"2026-03-13T05:46:05+00:00\",\"description\":\"Learn Python variable scope & lifetime: understand local, global, enclosing, and built-in variables for clean, efficient code.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/scope-and-lifetime-of-variables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/scope-and-lifetime-of-variables\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/scope-and-lifetime-of-variables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FUNDAMENTALS (PYF) > Functions > Scope and Lifetime of Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scope and Lifetime of Variables - One Language. Endless Possibilities","description":"Learn Python variable scope & lifetime: understand local, global, enclosing, and built-in variables for clean, efficient code.","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:\/\/gigz.pk\/python\/lesson\/scope-and-lifetime-of-variables\/","og_locale":"en_US","og_type":"article","og_title":"Scope and Lifetime of Variables - One Language. Endless Possibilities","og_description":"Learn Python variable scope & lifetime: understand local, global, enclosing, and built-in variables for clean, efficient code.","og_url":"https:\/\/gigz.pk\/python\/lesson\/scope-and-lifetime-of-variables\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-13T05:46:05+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/scope-and-lifetime-of-variables\/","url":"https:\/\/gigz.pk\/python\/lesson\/scope-and-lifetime-of-variables\/","name":"Scope and Lifetime of Variables - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T11:11:59+00:00","dateModified":"2026-03-13T05:46:05+00:00","description":"Learn Python variable scope & lifetime: understand local, global, enclosing, and built-in variables for clean, efficient code.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/scope-and-lifetime-of-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/scope-and-lifetime-of-variables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/scope-and-lifetime-of-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FUNDAMENTALS (PYF) > Functions > Scope and Lifetime of Variables"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}