How to use/access a Global Variable in a function - Python Last Updated : 16 Dec, 2024 Comments Improve Suggest changes Like Article Like Report In Python, variables declared outside of functions are global variables, and they can be accessed inside a function by simply referring to the variable by its name. Python a = "Great" def fun(): # Accessing the global variable 'a' print("Python is " + a) fun() OutputPython is Great Explanation:Here a is the global variable assigned the value "Great".This function fun() concatenates "Python is " with a+ and print "Python is Great".Defining by using global Keyword:By default, variables created inside a function are local to that function. To modify or create a global variable inside a function, you can use the global keyword, which allows the function to access and update the global variable. Python def fun(): # declaring varible by global keyword global a a = "great" # function calling fun() print("Python is " + a) OutputPython is great Accessing a Global Variables:Global Variables can be accessed inside functions by simply referring to their name. As long as the variable is not reassigned inside the function, you can use its value directly. Python x = 10 def fun(): # Using the global keyword to modify the global variable x global x x = 20 # Calling the function to modify the global variable fun() print(x) Output20 Explanation:The global variable x is initially set to 10, and inside the fun() function.After calling fun(), the global variable x is updated.Modifying a Global variable:While you can access global variables inside functions, modifying them requires using the global keyword. Without it, any assignment to a variable inside a function will create a local variable, which will shadow the global variable. Python a = "great" def fun(): # declaring variable by global keyword global a # modifying the global variable a = "Great" fun() print("Python is " + a) OutputPython is Great Explanation:The global keyword inside the fun() function allows the function to modify the global variable After calling fun(), the global variable a was updated . Comment More infoAdvertise with us Next Article How to use/access a Global Variable in a function - Python V vishakshx339 Follow Improve Article Tags : Python python Practice Tags : pythonpython Similar Reads How to Use a Variable from Another Function in Python Using a variable from another function is important for maintaining data consistency and code reusability. In this article, we will explore three different approaches to using a variable from another function in Python. Use a Variable from Another Function in PythonBelow are the possible approaches 2 min read Assign Function to a Variable in Python In Python, functions are first-class objects, meaning they can be assigned to variables, passed as arguments and returned from other functions. Assigning a function to a variable enables function calls using the variable name, enhancing reusability.Example:Python# defining a function def a(): print( 3 min read How to use Variables in Python3? Variable is a name for a location in memory. It can be used to hold a value and reference that stored value within a computer program. the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to the variables, you can store 3 min read How to check if a Python variable exists? Checking if a Python variable exists means determining whether a variable has been defined or is available in the current scope. For example, if you try to access a variable that hasn't been assigned a value, Python will raise a NameError. Letâs explore different methods to efficiently check if a va 3 min read How to use Function Decorators in Python ? In Python, a function can be passed as a parameter to another function (a function can also return another function). we can define a function inside another function. In this article, you will learn How to use Function Decorators in Python. Passing Function as ParametersIn Python, you can pass a fu 3 min read How to call a function in Python Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de 5 min read Access environment variable values in Python An environment variable is a variable that is created by the Operating System. Environment variables are created in the form of Key-Value pairs. To Access environment variables in Python's we can use the OS module which provides a property called environ that contains environment variables in key-va 3 min read How to Define and Call a Function in Python In Python, defining and calling functions is simple and may greatly improve the readability and reusability of our code. In this article, we will explore How we can define and call a function.Example:Python# Defining a function def fun(): print("Welcome to GFG") # calling a function fun() Let's unde 3 min read How to bind arguments to given values in Python functions? In Python, binding arguments to specific values can be a powerful tool, allowing you to set default values for function parameters, create specialized versions of functions, or partially apply a function to a set of arguments. This technique is commonly known as "partial function application" and ca 3 min read Use return value in another function - python In Python, one functionâs return value can be used in another, making code cleaner and more modular. This approach simplifies tasks, improves code reuse, and enhances readability. By breaking down logic into smaller functions that share data, you create flexible and maintainable programs. Letâs expl 2 min read Like