GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Global and Local Variables in Python - GeeksforGeeks Global and Local Variables in PythonLast Updated : 21 Apr, 2026 Variables store data in memory and scope defines the specific region of a program where a variable is accessible. It dictates the visibility and lifetime of the variable within the source code. Variables are categorized into two primary scopes: Global and Local. Local VariablesLocal variables are defined inside a function and exist only during its execution. They cannot be accessed from outside the function. Example 1: In this example, we are creating and accessing a local variable inside a function.
Output Hello from inside the function! Explanation: We define greet() with a local variable msg and print it. Since msg exists only during the function's execution, it's accessed within the function. Calling greet() displays the message. Example 2: In this example, we are creating a local variable inside a function and then trying to access it outside the function, which causes an error.
Output
Explanation: msg is a local variable inside greet() and can only be accessed there. Printing it outside causes an error because it doesn't exist globally. Global VariablesGlobal variables are declared outside all functions and can be accessed anywhere in the program, including inside functions. Example: In this example, we are creating a global variable and then accessing it both inside and outside a function.
Output Inside function: Python is awesome! Outside function: Python is awesome! Explanation: msg is a global variable accessible both inside and outside the display() function. Calling display() prints the global msg and printing msg outside the function works as expected.
Use of Local and Global variablesIf a variable is defined both globally and locally with the same name, local variable shadows the global variable inside the function. Changes to the local variable do not affect the global variable unless explicitly declare variable as global.
Output Me too. I love Geeksforgeeks Explanation: Inside fun(), 's' is a local variable set to "Me too." and prints that value. Outside, the global s remains "I love Geeksforgeeks", so printing s afterward shows the global value. Modifying Global Variables Inside a FunctionBy default, one cannot modify a global variable inside a function without declaring it as global. If you try, Python will raise an error because it treats variable as local. To modify a global variable use the global keyword. Without global (causes error)
Output
Explanation: Inside fun(), Python assumes 's' is local since we try to modify it. But no local 's' exists before that line, so an error occurs. With global (works correctly)
Output Python is great! GFG Look for GeeksforGeeks Python Section Look for GeeksforGeeks Python Section Explanation:
Global vs Local with Same Name
Output global: 1 f(): 1 global: 1 g(): 2 global: 1 h(): 3 global: 3 Explanation:
Local Variable vs Global Variable
Comment Explore | ||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |