How to Use a Variable from Another Function in Python Last Updated : 31 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 to using a variable from another function in Python. Using Global VariablesUsing Function ParametersUsing Class AttributesUse a Variable from Another Function Using Global VariablesIn this example, we are using global variables to share a variable between functions. First, we declare the variable as global within one function to set its value, and then we access it using global in another function to use its value. Python def set_variable(): global my_variable my_variable = "Hello from GeeksforGeeks" def use_variable(): global my_variable print(my_variable) set_variable() use_variable() OutputHello from GeeksforGeeks Use a Variable from Another Function Using Function ParametersIn this example, we pass the variable as a parameter to another function. This approach makes sure that the function can access the variable's value without relying on global variables. Python def set_variable(): my_variable = "Hello from GeeksforGeeks" return my_variable def use_variable(variable): print(variable) my_variable = set_variable() use_variable(my_variable) OutputHello from GeeksforGeeks Use a Variable from Another Function Using Class AttributesIn this example, we use a class to encapsulate the variable as an attribute. This allows different methods within the class to access and modify the variable's value, providing a structured approach to sharing variables between functions. Python class MyClass: def set_variable(self): self.my_variable = "Hello from GeeksforGeeks" def use_variable(self): print(self.my_variable) obj = MyClass() obj.set_variable() obj.use_variable() OutputHello from GeeksforGeeks Comment More infoAdvertise with us Next Article Assign Function to a Variable in Python G gauravggeeksforgeeks Follow Improve Article Tags : Python python-basics Practice Tags : python Similar Reads How to use/access a Global Variable in a function - Python 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.Pythona = "Great" def fun(): # Accessing the global variable 'a' print("Python is " + a) fun()OutputPython is Great Explanation:Here a i 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 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 import variables from another file in Python? To import variables from another file in Python, you need to use the import statement. By importing a file, you gain access to its variables and functions. This can be done by simply using import filename or from filename import variable_name to access the required variables or functions in your cur 4 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 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 Like