{"id":4359,"date":"2021-06-23T14:42:47","date_gmt":"2021-06-23T09:12:47","guid":{"rendered":"https:\/\/blog.guvi.in\/?p=4359"},"modified":"2025-10-22T17:40:24","modified_gmt":"2025-10-22T12:10:24","slug":"how-to-use-global-variables-inside-a-function-learn-python-with-examples","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/how-to-use-global-variables-inside-a-function-learn-python-with-examples\/","title":{"rendered":"How To Use Global Variables Inside A Function In Python?"},"content":{"rendered":"\n<p><span style=\"font-weight: 400;\">What are global variables and how to use Global Variables inside a function in Python? Don&#8217;t worry we will answer this question very shortly. Also, we will take you through a couple of examples followed by practice questions to make you just about ready for the topic! Before we jump into the primary subject of discussion, let us understand in brief the memory allocations in Python.&nbsp;<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding memory allocation in Python<\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Firstly, <a href=\"https:\/\/www.guvi.in\/blog\/do-you-know-how-to-create-variables-in-python\/\">variables in Python<\/a> are reserved memory locations where a user can store their data values. When we declare a variable, the interpreter ensures to reserve some part of the memory in the name of that variable. And the variable then utilizes that memory location for storing the data. That is to say, the names that were given to the <\/span><span style=\"font-weight: 400;\">variable act as a referencing tag. And these referencing tags mention the reserved blocks of memory. <\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Thus, after we declare and initialize the variables inside a program, we can access them within the program. However, we need to realize that not all variables can be accessed anywhere inside the program. On the other hand, the part of the program where a variable can be accessed inside the program is called the scope of the variable. To clarify, there are multiple types of variable scope like a local variable, global variable, non-local variable. This article will discuss global variables and their usage inside a Python program in detail.<\/span><\/p>\n\n\n\n<p>Before diving into the next section, ensure you&#8217;re solid on <a href=\"https:\/\/www.guvi.in\/hub\/python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python <\/a>essentials from basics to advanced level. If you are looking for a detailed Python career program, you can join<a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=global-variables-inside-a-function\"> <strong>HCL GUVI\u2019s Python Career Program<\/strong><\/a><strong> <\/strong>with placement assistance. You will be able to master the Multiple Exceptions, classes, OOPS concepts, dictionary, and many more, and build real-life projects.<\/p>\n\n\n\n<p>Also, if you would like to explore Python through a Self-paced course, try<a href=\"https:\/\/www.guvi.in\/courses\/programming\/python\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=global-variables-inside-a-function\"> <strong>HCL GUVI\u2019s Python Self-Paced course<\/strong><\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are global variables?<\/strong><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">The variables that we declare outside a function are called <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Global_variable\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Global_variable\" rel=\"noreferrer noopener\">global variables<\/a><\/strong>. We can reference such variables from anywhere inside the program. Hence, the scope of the variable is global. They can be accessed from inside or outside of the function. Let\u2019s learn more about these by implementing a practical example:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"436\" height=\"200\" src=\"http:\/\/blog.guvi.in\/wp-content\/uploads\/2021\/06\/image-1-global-variable-2.png\" alt=\"python coding 1\" class=\"wp-image-4444\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/image-1-global-variable-2.png 436w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/image-1-global-variable-2-300x138.png 300w\" sizes=\"(max-width: 436px) 100vw, 436px\" title=\"\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">In the above-written program, we first declared a variable named var that stores a string inside it. It should be noted that this variable is declared outside a function. Now, we declare a user-defined function having name custom_function(). Then, inside the function, we print the data stored inside the var variable. Also, an external print statement is written outside of the function block that prints the data that we store in the var variable. When we execute and run this program, we will get the output as given below:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"433\" height=\"69\" src=\"http:\/\/blog.guvi.in\/wp-content\/uploads\/2021\/06\/global-variable-2.png\" alt=\"using global variable\" class=\"wp-image-4449\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-2.png 433w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-2-300x48.png 300w\" sizes=\"(max-width: 433px) 100vw, 433px\" title=\"\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">If we observe the program&#8217;s output, we can see the value stored inside the var variable is the same when we try to access from inside the function or from outside the function. Thus, it can be verified that globally declared variables can be accessed from anywhere inside the program.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Manipulating the value stored in a global variable inside a function<\/strong><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">To this point, accessing the values in a global variable inside a user-defined function was handy. We were able to do it easily as global variables are accessible from anywhere inside the program. Now, let\u2019s try to manipulate the value of a global variable inside the function. Let us find out whether the changes occurring inside the function get reflected globally or not.<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"574\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-3-1024x574.png\" alt=\"how to use global variable in function \" class=\"wp-image-4451\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-3-1024x574.png 1024w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-3-300x168.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-3-768x430.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-3.png 1353w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">Upon executing the above-written program, we obtain the following output&nbsp;<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"284\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-4-1024x284.png\" alt=\"global variable 4\" class=\"wp-image-4452\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-4-1024x284.png 1024w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-4-300x83.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-4-768x213.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-4.png 1353w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">From the output, we can see the interpreter throws a traceback (error). It says that the local variable \u2018var\u2019 needs a definition when we try to update the value stored inside the global variable. This happens because the Python interpreter assumes any variable to be a local variable inside the function when we try to perform the update operation over that variable. That is why here the program throws an error saying local variable \u2018var\u2019 needs a definition. Hence, we can conclude that we cannot assign other values to the global variables. Also, we cannot update the global variables inside a function. To overcome this situation, we make use of the <\/span><b>global <\/b><span style=\"font-weight: 400;\">keyword.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Using global Variables inside a function<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">The <\/span><b>global <\/b><span style=\"font-weight: 400;\">keyword is used to make a local variable turn into global and allows making changes to the variable in the local context. Let\u2019s turn ourselves back to the previous example but this time after making some subtle changes.<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"300\" height=\"197\" src=\"http:\/\/blog.guvi.in\/wp-content\/uploads\/2021\/06\/global-variable-5-300x197.png\" alt=\"python coding 2\" class=\"wp-image-4455\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-5-300x197.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-variable-5.png 433w\" sizes=\"(max-width: 300px) 100vw, 300px\" title=\"\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">To be able to assign and manipulate the global variable \u2018var\u2019 inside the function, we declare it with a <\/span><b>global <\/b><span style=\"font-weight: 400;\">keyword inside the function. Now, as we try to make changes in the \u2018var\u2019 variable, the interpreter does not interpret it as a local variable and hence, changes can be made easily.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Upon compiling and executing the above code, the output we will see will be given as:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"200\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-6-1024x200.png\" alt=\"python coding 3\" class=\"wp-image-4457\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-6-1024x200.png 1024w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-6-300x59.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-6-768x150.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-6.png 1364w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">The changes that we make to the global variable inside the function will be visible throughout the program, as seen from the last print statement written outside the function body.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Local and global variables having the same name inside a function<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Let\u2019s see the behavior of the function when we use both local and global variables having the same name inside the program.<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"514\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-7-1024x514.png\" alt=\"global Variables\" class=\"wp-image-4458\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-7-1024x514.png 1024w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-7-300x151.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-7-768x386.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-7.png 1358w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">We first have declared the \u2018var\u2019 variable globally and later inside the function declare again a local variable with the same name \u2018var\u2019. When we call the function the print statement executed inside the function prints a value. We can see that the value that the print statement prints are the local variable inside the function. On the contrary, when the control of the program goes outside of the block and the second print statement executes, the value that it prints is of the global variable. This is because the scope of the local variable remains inside the function block only. The output is as shown below:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"300\" height=\"48\" src=\"http:\/\/blog.guvi.in\/wp-content\/uploads\/2021\/06\/global-Variable-8-300x48.png\" alt=\"python coding 4\" class=\"wp-image-4459\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-8-300x48.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/global-Variable-8.png 435w\" sizes=\"(max-width: 300px) 100vw, 300px\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Test your understanding of the scope of variables in Python. Attempt these practice MCQs. <\/b><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><b>See if you are clear with the concept on the use of global variables inside a function in Python:<\/b><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>Question 1. What will be the output of following code?<\/b>\n\n<span style=\"font-weight: 400;\">var_1 = 9<\/span>\n\n<span style=\"font-weight: 400;\">if True:<\/span>\n\n<span style=\"font-weight: 400;\">var_1 = 16<\/span>\n\n<span style=\"font-weight: 400;\">var_1 = var_1 + 34<\/span>\n\n<span style=\"font-weight: 400;\"># outside the if block<\/span>\n\n<span style=\"font-weight: 400;\">print(var_1)<\/span>\n\n<span style=\"font-weight: 400;\">i. 16<\/span>\n\n<span style=\"font-weight: 400;\">ii. 9<\/span>\n\n<span style=\"font-weight: 400;\">iii. 34<\/span>\n\n<span style=\"font-weight: 400;\">iv. 50<\/span>\n\n<span style=\"font-weight: 400;\">Correct Answer: (iv.) 50 <\/span><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>Question 2. What will be the output of following code?<\/b>\n\n<span style=\"font-weight: 400;\">var_1 = 50<\/span>\n\n<span style=\"font-weight: 400;\">def func(var_2):<\/span>\n\n<span style=\"font-weight: 400;\">var_1 = 77<\/span>\n\n<span style=\"font-weight: 400;\">var_1 = var_2<\/span>\n\n<span style=\"font-weight: 400;\">return (var_1)%10<\/span>\n\n<span style=\"font-weight: 400;\"># ouside the function block<\/span>\n\n<span style=\"font-weight: 400;\">print (func(1))<\/span>\n\n<span style=\"font-weight: 400;\">i. 1<\/span>\n\n<span style=\"font-weight: 400;\">ii. 50<\/span>\n\n<span style=\"font-weight: 400;\">iii. 77<\/span>\n\n<span style=\"font-weight: 400;\">iv. 7<\/span>\n\n<span style=\"font-weight: 400;\">Correct Answer: (i) 1<\/span><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">Question 3. <a href=\"https:\/\/stackoverflow.com\/questions\/53958876\/any-type-of-scope-resolution-operator-in-python\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Scope resolution in Python<\/a> is based on which of the following rule?<\/span>\n\n<span style=\"font-weight: 400;\">i. DMAS rule<\/span>\n\n<span style=\"font-weight: 400;\">ii. LEGB rule<\/span>\n\n<span style=\"font-weight: 400;\">iii. LBEG rule<\/span>\n\n<span style=\"font-weight: 400;\">iv. BGEL rule<\/span>\n\n<span style=\"font-weight: 400;\">Correct Answer: (ii) LEGB rule<\/span>\n\n<span style=\"font-weight: 400;\">The interpreter uses the <a href=\"https:\/\/www.guvi.in\/blog\/description-of-scope-in-python-programming-language\/\" target=\"_blank\" rel=\"noreferrer noopener\">LEGB rule in Python<\/a> to decide the order in which it looks out for namespace in the program. It also uses the same and performs scope resolution. The hierarchy that is to be followed for scope resolution is given as Local -&gt; Enclosed -&gt; Global -&gt; Built-in.<\/span><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>Question 4. Which of the following keyword can transform the scope of the variable from local to global?<\/b>\n\n<span style=\"font-weight: 400;\">i. global<\/span>\n\n<span style=\"font-weight: 400;\">ii. convert_global<\/span>\n\n<span style=\"font-weight: 400;\">iii. __global__<\/span>\n\n<span style=\"font-weight: 400;\">iv. None of these<\/span>\n\n<span style=\"font-weight: 400;\">Correct Answer: (i) global<\/span>\n\n<span style=\"font-weight: 400;\">We make use of the <\/span><b>global<\/b><span style=\"font-weight: 400;\"> keyword to make a local variable turn into the global scope.<\/span><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>Question 5.&nbsp; Which of the following is not a reserved keyword in Python?<\/b>\n\n<span style=\"font-weight: 400;\">i. for<\/span>\n\n<span style=\"font-weight: 400;\">ii. global<\/span>\n\n<span style=\"font-weight: 400;\">iii. type<\/span>\n\n<span style=\"font-weight: 400;\">iv. none of these<\/span>\n\n<span style=\"font-weight: 400;\">Correct Answer: (iv) none of these<\/span>\n\n<span style=\"font-weight: 400;\">All the keywords given above are reserved keywords. The reserved keywords are so-called because we cannot use them as ordinary identifiers. We use the \u2018for\u2019 keyword to denote a \u2018for\u2019 loop. The \u2018global\u2019 keyword is used to define a global variable. The \u2018type\u2019 keyword determines the type of Python object, we use.&nbsp;<\/span><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Wrapping Up&nbsp;<\/strong><\/h2>\n\n\n\n<p>We hope this article could throw some light on using global variables inside a function in Python. For more such articles and tips and tricks in Python, <a href=\"https:\/\/www.guvi.in\/blog\/category\/python\/\">keep up-to-date with our blogs on Python<\/a>.<\/p>\n\n\n\n<p>Kickstart your Programming journey by enrolling in<a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=global-variables-inside-a-function\"> <strong>HCL GUVI\u2019s Python Career Program<\/strong><\/a> where you will master technologies like multiple exceptions, classes, OOPS concepts, dictionaries, and many more, and build real-life projects.<\/p>\n\n\n\n<p>Alternatively, if you would like to explore Python through a Self-Paced course, try<a href=\"https:\/\/www.guvi.in\/courses\/programming\/python\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=global-variables-inside-a-function\"> <strong>HCL GUVI\u2019s Python Self-Paced course<\/strong><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What are global variables and how to use Global Variables inside a function in Python? Don&#8217;t worry we will answer this question very shortly. Also, we will take you through a couple of examples followed by practice questions to make you just about ready for the topic! Before we jump into the primary subject of [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":4555,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,717],"tags":[],"views":"7342","authorinfo":{"name":"Archana","url":"https:\/\/www.guvi.in\/blog\/author\/archana\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/how-to-use-global-variables-inside-functions-in-Python-1-300x300.gif","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/how-to-use-global-variables-inside-functions-in-Python-1.gif","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/4359"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=4359"}],"version-history":[{"count":39,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/4359\/revisions"}],"predecessor-version":[{"id":90830,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/4359\/revisions\/90830"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/4555"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=4359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=4359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=4359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}