Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Global and Local variables in Python

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 380
    Comment on it

    Python Variables that are defined inside a function body have a local scope, and those defined outside have a global scope and variable.
    That indicate local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions. When we call a function, the variables declared inside it are brought into function. For example you can see below code.

    #!/shiva/bin/python
    total = 0; # This is global variable.
    # Function definition is here
    def sum( t1, t2 ):
       # Add both the parameters and return them."
       total = t1 + t2; # Here total is local variable.
       print "Inside the function local total : ", total
       return total;
    
    # Now you can call sum function
    sum( 6, 5 );
    print "Outside the function global total : ", total 

    Output:

    Inside the function local total :11
    Outside the function global total : 0

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: