{"id":4705,"date":"2022-08-16T11:15:33","date_gmt":"2022-08-16T11:15:33","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=4705"},"modified":"2022-08-16T11:16:17","modified_gmt":"2022-08-16T11:16:17","slug":"what-are-the-rules-for-a-local-and-global-variable-in-python","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/","title":{"rendered":"What are the rules for a local and global variable in Python ?"},"content":{"rendered":"<ul>\n<li style=\"text-align: justify;\">In python local variables are those which are defined inside a function and its scope is limited to that function only whereas\u00a0global variables are those which are not defined inside any function and have a global scope.<\/li>\n<li style=\"text-align: justify;\">We can say that global variables are accessible throughout the program and inside every function whereas local variables are accessible only inside the function in which it was initialized.<\/li>\n<\/ul>\n<h2 id=\"local-variables\" style=\"text-align: justify;\">Local Variables<\/h2>\n<ul>\n<li style=\"text-align: justify;\">Inside function local variables are those which are initialized and belongs only to that particular function.<\/li>\n<li style=\"text-align: justify;\">Then it cannot be accessed anywhere outside the function.<\/li>\n<\/ul>\n<h3 id=\"sample-code\" style=\"text-align: justify;\">Sample Code<\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">def f():<br\/>     <br\/>    # local variable<br\/>    s = &quot;I love Wikitechy&quot;<br\/>    print(s)<br\/> <br\/># Driver code<br\/>f()<\/code><\/pre> <\/div>\n<h3 id=\"output\" style=\"text-align: justify;\">Output<\/h3>\n<p style=\"text-align: justify;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-4706\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable.png\" alt=\"\" width=\"969\" height=\"223\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable.png 969w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable-300x69.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable-768x177.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable-390x90.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable-820x189.png 820w\" sizes=\"(max-width: 969px) 100vw, 969px\" \/><\/p>\n<h2 id=\"global-variables\" style=\"text-align: justify;\">Global Variables<\/h2>\n<ul style=\"text-align: justify;\">\n<li>Outside any function the global variables are those which are defined and which are accessible throughout the program.<\/li>\n<\/ul>\n<h3 id=\"sample-code-2\" style=\"text-align: justify;\">Sample Code<\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># This function uses global variable s<br\/>def f():<br\/>    print(&quot;Inside Function&quot;, s)<br\/> <br\/># Global scope<br\/>s = &quot;I love Wikitechy&quot;<br\/>f()<br\/>print(&quot;Outside Function&quot;, s)<\/code><\/pre> <\/div>\n<h3 id=\"output-2\" style=\"text-align: justify;\">Output<\/h3>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"alignnone size-full wp-image-4707\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/global-variable.png\" alt=\"\" width=\"1027\" height=\"255\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/global-variable.png 1027w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/global-variable-300x74.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/global-variable-1024x254.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/global-variable-768x191.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/global-variable-390x97.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/global-variable-820x204.png 820w\" sizes=\"(max-width: 1027px) 100vw, 1027px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In python local variables are those which are defined inside a function and its scope is limited to that function only whereas\u00a0global variables are those which are not defined inside any function and have a global scope. We can say that global variables are accessible throughout the program and inside every function whereas local variables [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5608],"tags":[18212,18215,18210,18216,18217,18214,18213,18218,18211],"class_list":["post-4705","post","type-post","status-publish","format-standard","hentry","category-python","tag-example-of-global-variable-in-python","tag-global-variables-python-3","tag-how-to-change-global-variable-in-function-python","tag-local-and-global-variables-in-c","tag-python-global-variable-across-modules","tag-python-global-variable-in-function","tag-python-global-variables-best-practices","tag-what-are-the-rules-for-a-local-and-global-variable-in-python","tag-what-is-local-variable-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What are the rules for a local and global variable in Python ? - Wikitechy<\/title>\n<meta name=\"description\" content=\"What are the rules for a local and global variable in Python ? - In python local variables are those which are defined inside a function\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What are the rules for a local and global variable in Python ? - Wikitechy\" \/>\n<meta property=\"og:description\" content=\"What are the rules for a local and global variable in Python ? - In python local variables are those which are defined inside a function\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-16T11:15:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-16T11:16:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable.png\" \/>\n<meta name=\"author\" content=\"webmaster\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"webmaster\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-are-the-rules-for-a-local-and-global-variable-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-are-the-rules-for-a-local-and-global-variable-in-python\\\/\"},\"author\":{\"name\":\"webmaster\",\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/#\\\/schema\\\/person\\\/f785ba3ecc599133e65ab6138042a3e4\"},\"headline\":\"What are the rules for a local and global variable in Python ?\",\"datePublished\":\"2022-08-16T11:15:33+00:00\",\"dateModified\":\"2022-08-16T11:16:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-are-the-rules-for-a-local-and-global-variable-in-python\\\/\"},\"wordCount\":201,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-are-the-rules-for-a-local-and-global-variable-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/local-variable.png\",\"keywords\":[\"example of global variable in python\",\"global variables python 3\",\"how to change global variable in function python\",\"local and global variables in c\",\"python global variable across modules\",\"python global variable in function\",\"python global variables best practices\",\"what are the rules for a local and global variable in python\",\"what is local variable in python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-are-the-rules-for-a-local-and-global-variable-in-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-are-the-rules-for-a-local-and-global-variable-in-python\\\/\",\"url\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-are-the-rules-for-a-local-and-global-variable-in-python\\\/\",\"name\":\"What are the rules for a local and global variable in Python ? - Wikitechy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-are-the-rules-for-a-local-and-global-variable-in-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-are-the-rules-for-a-local-and-global-variable-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/local-variable.png\",\"datePublished\":\"2022-08-16T11:15:33+00:00\",\"dateModified\":\"2022-08-16T11:16:17+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/#\\\/schema\\\/person\\\/f785ba3ecc599133e65ab6138042a3e4\"},\"description\":\"What are the rules for a local and global variable in Python ? - In python local variables are those which are defined inside a function\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-are-the-rules-for-a-local-and-global-variable-in-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-are-the-rules-for-a-local-and-global-variable-in-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/local-variable.png\",\"contentUrl\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/local-variable.png\",\"width\":969,\"height\":223},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/#website\",\"url\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/#\\\/schema\\\/person\\\/f785ba3ecc599133e65ab6138042a3e4\",\"name\":\"webmaster\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"caption\":\"webmaster\"},\"sameAs\":[\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\"],\"url\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/author\\\/webmaster\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What are the rules for a local and global variable in Python ? - Wikitechy","description":"What are the rules for a local and global variable in Python ? - In python local variables are those which are defined inside a function","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:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/","og_locale":"en_US","og_type":"article","og_title":"What are the rules for a local and global variable in Python ? - Wikitechy","og_description":"What are the rules for a local and global variable in Python ? - In python local variables are those which are defined inside a function","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/","og_site_name":"Wikitechy","article_published_time":"2022-08-16T11:15:33+00:00","article_modified_time":"2022-08-16T11:16:17+00:00","og_image":[{"url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable.png","type":"","width":"","height":""}],"author":"webmaster","twitter_card":"summary_large_image","twitter_misc":{"Written by":"webmaster","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/#article","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/"},"author":{"name":"webmaster","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4"},"headline":"What are the rules for a local and global variable in Python ?","datePublished":"2022-08-16T11:15:33+00:00","dateModified":"2022-08-16T11:16:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/"},"wordCount":201,"commentCount":0,"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable.png","keywords":["example of global variable in python","global variables python 3","how to change global variable in function python","local and global variables in c","python global variable across modules","python global variable in function","python global variables best practices","what are the rules for a local and global variable in python","what is local variable in python"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/","name":"What are the rules for a local and global variable in Python ? - Wikitechy","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable.png","datePublished":"2022-08-16T11:15:33+00:00","dateModified":"2022-08-16T11:16:17+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4"},"description":"What are the rules for a local and global variable in Python ? - In python local variables are those which are defined inside a function","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-are-the-rules-for-a-local-and-global-variable-in-python\/#primaryimage","url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable.png","contentUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/local-variable.png","width":969,"height":223},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4","name":"webmaster","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","caption":"webmaster"},"sameAs":["https:\/\/www.wikitechy.com\/interview-questions"],"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=4705"}],"version-history":[{"count":2,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4705\/revisions"}],"predecessor-version":[{"id":4709,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4705\/revisions\/4709"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=4705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=4705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=4705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}