{"id":15939,"date":"2020-05-13T23:06:05","date_gmt":"2020-05-14T06:06:05","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=15939"},"modified":"2023-12-01T02:45:26","modified_gmt":"2023-12-01T10:45:26","slug":"python-global-local-variables","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/","title":{"rendered":"Guide to Python Global and Local Variables"},"content":{"rendered":"\n<p>A variable is an important and ubiquitous concept in programming. Variables are containers for storing data. Variables can store many types of data, including usernames, email addresses, and items in a user\u2019s online game inventory.<br><\/p>\n\n\n\n<p>When programming in Python, you will encounter two types of variables: global and local. In this guide, we discuss the difference between these two types of variables, how they work, and how you can use them in your code.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Variables<\/h2>\n\n\n\n<p>In Python, variables essentially allow you to label and store data. When defining a variable, you will give it a name. You can then use that name later in your code to retrieve the data it represents. Variables can store strings, numbers, lists, or any other data type.<br><\/p>\n\n\n\n<p>Suppose we are building a game and want to store a user\u2019s name. Instead of having to type the name throughout our program, we can use a variable to store it.<br><\/p>\n\n\n\n<p>Here\u2019s a variable that stores the name of a user for a game:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>name = \"Carlton Hernandez\"<\/pre><\/div>\n\n\n\n<p>We declared a variable called <code>name<\/code> and assigned it the value \u201cCarlton Hernandez\u201d.<br><\/p>\n\n\n\n<p>Now that we declared that variable, we can manipulate it in our code. For example, if we wanted to change our user\u2019s name, we could do so using this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>name = \"Carlton Hernandez\"\nname = \"Carlton Lewis Hernandez\"<\/pre><\/div>\n\n\n\n<p>On the first line of our code, we assigned the value \u201cCarlton Hernandez\u201d to the <code>name<\/code> variable. Then, we assigned the value \u201cCarlton Lewis Hernandez\u201d to the <code>name<\/code> variable. When you assign a new value to a variable, the program overwrites the most recent value, replacing it with the new one.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Global Variables<\/h2>\n\n\n\n<p>In Python, there are two main types of variables: local and global.<br><\/p>\n\n\n\n<p>Global variables are variables declared outside of a function. Global variables have a global scope. This means that they can be accessed throughout an entire program, including within functions. Consider the following visual representation of this concept.<br><\/p>\n\n\n\n<p><strong>Global and Local Variables in Python<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/docs.google.com\/drawings\/u\/0\/d\/sL-dZHh1GWHZIjmXMPl-xAA\/image?w=217&amp;h=191&amp;rev=121&amp;ac=1&amp;parent=1A5Wo7LZ_I8LHpPSxwbgmukFjicY2AJlMrMQYJ-XOm04\" alt=\"\"\/><\/figure>\n\n\n\n<p>A <strong>global variable<\/strong> can be accessed throughout a program.<br><\/p>\n\n\n\n<p>Here is an example of a global variable in Python:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>name = \"Carlton Hernandez\"<\/pre><\/div>\n\n\n\n<p>Once we declare a global variable, we can use it throughout our code. For example, we can create a function that prints the value held by our global variable <code>name<\/code> using the following code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def printName():\n\tprint(name)\nprintName()<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"Carlton Hernandez\"<\/pre><\/div>\n\n\n\n<p>Here, we initialized a function called <code>printName()<\/code>. This function, when invoked, prints the value of the <code>name<\/code> variable to the console. This is what happened above when we invoked the function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Local Variables<\/h2>\n\n\n\n<p>Local variables, on the other hand, are variables declared inside a function. These variables are known to have <code>local scope<\/code>. This means they can be accessed only within the function in which they are declared. Consider again this visual representation we displayed earlier that depicts the concept of variables in Python:<br><\/p>\n\n\n\n<p><strong>Global and Local Variables<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/docs.google.com\/drawings\/u\/0\/d\/s4pkpHUH1nYzghv6tfNNKXg\/image?w=217&amp;h=191&amp;rev=1&amp;ac=1&amp;parent=1A5Wo7LZ_I8LHpPSxwbgmukFjicY2AJlMrMQYJ-XOm04\" alt=\"\"\/><\/figure>\n\n\n\n<p>A <strong>local variable<\/strong> can only be accessed within a particular function.<br><\/p>\n\n\n\n<p>The following is an example of a program that uses a local variable:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def printName():\n\tname = \"Georgia Anderson\"\n\tprint(name)\nprintName()<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"Georgia Anderson\"<\/pre><\/div>\n\n\n\n<p>In our code, we declared a function called <code>printName()<\/code>. Within that function, we defined a variable called <code>name<\/code>. Because we declared this variable within a function, it is a local variable.&nbsp;<br><\/p>\n\n\n\n<p>At the end of our code, we called our function using <code>printName()<\/code>. In response, the program executed the <code>printName()<\/code> function.&nbsp;<br><\/p>\n\n\n\n<p>The <code>name<\/code> variable in this example is local to the <code>printName()<\/code> function. Therefore, we cannot access that variable outside of our function. Here\u2019s what happens when we try to print this local <code>name<\/code> variable outside of the function in which it is declared:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def printName():\n\tname = \"Georgia Anderson\"\n\tprint(name)\nprintName()\nprint(name)<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>NameError: name 'name' is not defined<\/pre><\/div>\n\n\n\n<p>We cannot access a local variable outside of the function in which we assigned that variable. When we try to access a local variable in our main program, the program will return an error.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Global and Local Variables<\/h2>\n\n\n\n<p>It is possible for a program to use the same variable name for both a local and a global variable. In such a scenario, the local variable will be read in local scope, and the global variable will be read in global scope.<br><\/p>\n\n\n\n<p>Here\u2019s an example of a program that has local and global variables with the same name:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>score = 5\ndef calculateScore():\n\tscore = 10\n\tprint(\"Final Score:\", score)\ncalculateScore()\nprint(\"Initial Score:\", score)<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Final Score: 10\nInitial Score: 5<\/pre><\/div>\n\n\n\n<p>First, we assigned the global variable <code>score<\/code> in our code. Then, in our <code>calculateScore()<\/code> function, we created a local variable with the same name.<br><\/p>\n\n\n\n<p>The value of our local <code>score<\/code> variable in the <code>calculateScore()<\/code> function is 10. So, when we call our <code>calculateScore()<\/code> function, the message <code>Final Score: 10<\/code> is printed to the console.<br><\/p>\n\n\n\n<p>However, outside of our <code>calculateScore()<\/code> function, the value of the <code>score<\/code> variable is 5. This is because we set the value of the <code>score<\/code> variable in global scope to 5. So, when we print out <code>Initial Score:<\/code> , followed by the value of the <code>score<\/code> variable, the program displays the value 5.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Assigning Global Variables within a Function<\/h2>\n\n\n\n<p>In the above sections, we learned that global variables are defined outside of a function (i.e., globally), and local variables are defined inside a function (i.e., locally). However, if you use the <code>global<\/code> keyword to define a variable within a function (i.e., locally), and then run that function in the program (globally), that variable will become a global variable.<br><\/p>\n\n\n\n<p>Consider the following example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def setName():\n\tglobal name\n\tname = \"Bianca Peterson\"\nsetName()\nprint(name)<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"Bianca Peterson\"<\/pre><\/div>\n\n\n\n<p>In our code, we assigned the value of the <code>name<\/code> variable locally, inside the <code>setName()<\/code> function. However, because we used the global keyword before defining this variable, we prepare to give the <code>name<\/code> variable global scope. However, the <code>name<\/code> variable will become a global variable only <em>after<\/em> we run this function in the program.<br><\/p>\n\n\n\n<p>After the program runs the <code>setName()<\/code> function, any time we use the <code>name<\/code> variable in our program, as we did when we ran the <code>print()<\/code> function, the program will use the value we declared in the local function. This is because the <code>name<\/code> variable is now a global variable.<br><\/p>\n\n\n\n<p>Remember how earlier we said that can\u2019t <em>directly<\/em> change a global variable inside a function? That is true. However, you can use the global keyword to change a global variable inside a function indirectly.<br><\/p>\n\n\n\n<p>Here\u2019s an example of a program that uses the global keyword in a function to change the value of a global variable:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>name = \"Bianca Peterson\"\ndef setName():\n\tglobal name\n\tname = \"Bianca Lucinda Peterson\"\nsetName()\nprint(name)<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\"Bianca Lucinda Peterson\"<\/pre><\/div>\n\n\n\n<p>Let\u2019s break down this code. First, we declared a variable called <code>name<\/code> and assigned it the value \u201cBianca Peterson\u201d. This is a global variable. Then, we declared a function called <code>setName()<\/code>. When called, the <code>setName()<\/code> function changes the value of the <code>name<\/code> variable.&nbsp;<br><\/p>\n\n\n\n<p>We then called the <code>setName()<\/code> function, which changed the value of <code>name<\/code> to \u201cBianca Lucinda Peterson\u201d. Finally, we printed the value of <code>name<\/code> to the console. The value of the global variable <code>name<\/code> is now \u201cBianca Lucinda Peterson\u201d.<br><\/p>\n\n\n\n<p>Generally speaking, the global keyword should be used sparingly. Using the global keyword too often can make it difficult to understand the scope of variables in a program.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Global variables are variables declared outside a function. Local variables are variables declared inside a function.<br><\/p>\n\n\n\n<p>While global variables cannot be directly changed in a function, you can use the global keyword to create a function that will change the value of a global variable. In this case, the global variable\u2019s value will not actually change until you run that function.<br><\/p>\n\n\n\n<p>This tutorial discussed, with reference to examples, the basics of local and global variables, and how they work. Now you\u2019re ready to start using local and global variables in your Python code like a professional programmer!<\/p>\n","protected":false},"excerpt":{"rendered":"A variable is an important and ubiquitous concept in programming. Variables are containers for storing data. Variables can store many types of data, including usernames, email addresses, and items in a user\u2019s online game inventory. When programming in Python, you will encounter two types of variables: global and local. In this guide, we discuss the&hellip;","protected":false},"author":240,"featured_media":15940,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-15939","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Guide to Python Global and Local Variables | Career Karma<\/title>\n<meta name=\"description\" content=\"Global variables are variables defined outside of a function. Local variables are variables defined inside a function. On Career Karma, learn how to use Python global and local variables.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Guide to Python Global and Local Variables\" \/>\n<meta property=\"og:description\" content=\"Global variables are variables defined outside of a function. Local variables are variables defined inside a function. On Career Karma, learn how to use Python global and local variables.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-14T06:06:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T10:45:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/three-woman-in-front-of-laptop-computer-1181233.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"681\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Guide to Python Global and Local Variables\",\"datePublished\":\"2020-05-14T06:06:05+00:00\",\"dateModified\":\"2023-12-01T10:45:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/\"},\"wordCount\":1210,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/three-woman-in-front-of-laptop-computer-1181233.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/\",\"name\":\"Guide to Python Global and Local Variables | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/three-woman-in-front-of-laptop-computer-1181233.jpg\",\"datePublished\":\"2020-05-14T06:06:05+00:00\",\"dateModified\":\"2023-12-01T10:45:26+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"Global variables are variables defined outside of a function. Local variables are variables defined inside a function. On Career Karma, learn how to use Python global and local variables.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/three-woman-in-front-of-laptop-computer-1181233.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/three-woman-in-front-of-laptop-computer-1181233.jpg\",\"width\":1020,\"height\":681},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Guide to Python Global and Local Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Guide to Python Global and Local Variables | Career Karma","description":"Global variables are variables defined outside of a function. Local variables are variables defined inside a function. On Career Karma, learn how to use Python global and local variables.","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:\/\/careerkarma.com\/blog\/python-global-local-variables\/","og_locale":"en_US","og_type":"article","og_title":"Guide to Python Global and Local Variables","og_description":"Global variables are variables defined outside of a function. Local variables are variables defined inside a function. On Career Karma, learn how to use Python global and local variables.","og_url":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-05-14T06:06:05+00:00","article_modified_time":"2023-12-01T10:45:26+00:00","og_image":[{"width":1020,"height":681,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/three-woman-in-front-of-laptop-computer-1181233.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Guide to Python Global and Local Variables","datePublished":"2020-05-14T06:06:05+00:00","dateModified":"2023-12-01T10:45:26+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/"},"wordCount":1210,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/three-woman-in-front-of-laptop-computer-1181233.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/","url":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/","name":"Guide to Python Global and Local Variables | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/three-woman-in-front-of-laptop-computer-1181233.jpg","datePublished":"2020-05-14T06:06:05+00:00","dateModified":"2023-12-01T10:45:26+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"Global variables are variables defined outside of a function. Local variables are variables defined inside a function. On Career Karma, learn how to use Python global and local variables.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-global-local-variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/three-woman-in-front-of-laptop-computer-1181233.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/three-woman-in-front-of-laptop-computer-1181233.jpg","width":1020,"height":681},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Guide to Python Global and Local Variables"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/15939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=15939"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/15939\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/15940"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=15939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=15939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=15939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}