Python Programming Local and Global Variables

What is variable?

  • Variables are reserved memory locations to store values.
  • if you create a variable in python program, then reserves some spaces in memory to store value of particular data type.
  • Based on the data type of a variable, interpreter allocates memory and decides what can be stored in the reserved memory.
  • variable can store integers, decimals and characters.

How to assign value to variable in python?

Assignment operator '=' is used to assign value to variable.
var_name = "test";
var_integer = 100;
var_decimal = 20.5;
left side of the assignment operator is the variable name and right side is the value of that variable.

Is that expression can be used to declare variable?

yes, variable can be declared using expression result.
var_a = (21 + 24) * 23;                                                                                                                                     

Python Variables Example

#!/usr/bin/python                                                               
                                                                                
var_a = 100;                                                                                                                                        
var_b = var_a * 23;                                                             
                                                                                
print "Initialization value: ", var_a;                                                                                                   
print "Expression result: ", var_b; 
Output:
$ python test_variable.py
Initialization value:  100
Expression result:  2300

Multiple assignment to declare variables

Python allows to declare multiple variables with same value as below.
var_a = var_b = var_c = 1;
All the variables point to value 1.
Python also supports to assign different values to multiple variables in same declaration statement.
var_a, var_b, var_c = 15, 20.4,"test";
here var_a value is 15, var_b value is 20.4 and var_c value is "test"

What is scope of variable?

All variables may not be accessible at all locations in python program. variables visibility depends on where you have declared a variable in program.
There are two different scopes in python.
  • Local variables: variables declared inside a function is the local scope, variables visibility only inside the function.
  • Global variables: variables declared outside a function is the global scope, variables visibility at all locations in program.

Global Variables and Local Variables Difference

Variables that are defined inside a function body have a local scope, and those defined outside have a global scope. local variable always overrides global variable in the local scope.
#!/usr/bin/python                                                               
                                                                                
# global variable                                                               
avg = 0;                                                                        
                                                                                
def get_avg_mark(name, *marks):                                                 
    avg = mark = 0;                                                             
    for m in marks:                                                             
        mark +=m;                                                               
    # local variable                                                            
    avg = mark/len(marks);                                                      
    print "Inside function, Average mark: ", avg;                               
    return avg;                                                                 
                                                                                
get_avg_mark('student1', 76, 70, 72);                                           
print "Outside function, Average mark: ", avg;   
Output:
$ python test_avg.py
Inside function, Average mark:  72
Outside function, Average mark:  0

Global variable access inside function

#!/usr/bin/python                                                               
                                                                                
# global variable                                                               
avg = 0;                                                                        
                                                                                
def get_avg_mark(name, *marks):                                                 
    avg = mark = 0;                                                             
    global avg;                                                                 
    for m in marks:                                                             
        mark +=m;                                                               
    # local variable                                                            
    avg = mark/len(marks);                                                      
    print "Inside function, Average mark: ", avg;                               
    return avg;                                                                 
                                                                                
get_avg_mark('student1', 76, 70, 72);                                           
print "Outside function, Average mark: ", avg;   
Output:
$ python test_avg.py
test_avg.py:8: SyntaxWarning: name 'avg' is assigned to before global declaration
  global avg;
Inside function, Average mark:  72
Outside function, Average mark:  72
if we declare global variable in first line of function definition, syntax warning will not appear.
#!/usr/bin/python                                                               
                                                                                
# global variable                                                               
avg = 0;                                                                        
                                                                                
def get_avg_mark(name, *marks):                                                 
    global avg;                                                                 
    avg = mark = 0;                                                             
    for m in marks:                                                             
        mark +=m;                                                               
    # local variable                                                            
    avg = mark/len(marks);                                                      
    print "Inside function, Average mark: ", avg;                               
    return avg;                                                                 
                                                                                
get_avg_mark('student1', 76, 70, 72);                                           
print "Outside function, Average mark: ", avg; 
Output:
$ python test_avg.py
Inside function, Average mark:  72
Outside function, Average mark:  72

Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Free online programming tutorials

Email Facebook Google LinkedIn Twitter
^