How to use a global variable in a Python function


How to use a global variable in a Python function

Answers

  • January 21, 2022

    To use the global variable in the Python function, define it as a global variable inside each function that assigns a value to it. To create a variable, write the variable name = assign the value to the right = sign the variable name.

        # example of a global variable  
        a = 120   # defined outside the function  
        b = 110  
          
        def sum():  
            c = a + b  # Using global variables  
            print("Sum of a and b is :", c)  
          
        def sub():  
            d = a - b   # using global variables  
            print("Substraction of a and b is :", d)  
          
          
        sum() 
        sub() 

     

    Comment

Have More Question?

Subscribe For Daily Updates

Flutter Questions
Android Questions