#Working of auto variable
import sys
#Functio definitions
def call1():
v = 20
sys.stdout.write("\nV = %d"%(v))
def call2():
v = 30
call1()
sys.stdout.write("\nV = %d"%(v))
#Variable Initialization
v = 10
#Function call
call2()
#Result
sys.stdout.write("\nV = %d"%(v))
#Working of auto variables in different blocks
import sys
#Variable Initialization
x = 10
sys.stdout.write("\nX = %d"%(x))
x = 20
sys.stdout.write("\nX = %d"%(x))
x = 10
sys.stdout.write("\nX = %d"%(x))
#In python, block of statements are indicated using intendation.
#block of statements can be written for conditional,loop or function statements
#Use same variable in different blocks with different data types
import sys
#Variable Initialization & Result
x = 10
sys.stdout.write("\nX = %d"%(x))
x = 2.22
sys.stdout.write("\nX = %g"%(x))
x = "Auto Storage Class"
sys.stdout.write("\nX = %s"%(x))
x = 10
sys.stdout.write("\nX = %d"%(x))
#Working of external variables
import sys
#Function definitions
def call1():
sys.stdout.write("\nIn Call1() V = %d"%(v))
def call2():
sys.stdout.write("\nIn call2() V = %d"%(v))
#Variable Initialization
global v
v = 10
call1()
call2()
sys.stdout.write("\nIn main() V = %d"%(v))
#Working of auto and global variables with same name
import sys
#Function definitions
def call1():
v = 20
sys.stdout.write("\nIn Call1() V = %d"%(v))
def call2():
sys.stdout.write("\nIn call2() V = %d"%(v))
#Variable Initialization
global v
v = 10
#Function call
call1()
call2()
#Result
sys.stdout.write("\nIn main() V = %d"%(v))
#Declare external variables using extern keyword
#Variable Initialization
global m
m = 10
#There is no extern keyword in python, global is used instead
sys.stdout.write("\nM = %d"%(m))
#Use of static variable
import sys
#Function definition
def print1(m):
sys.stdout.write("\nm = %d"%(m))
if m == 3:
return
m = 0
for i in range(0,3):
m += 1
print1(m)
#Difference between variables of auto and static class
import sys
#Result
y = 0
sys.stdout.write("\nx = %d & y = %d"%(x,y))
#Since x display the junk value which stored already, it differs in different systems
#Register class variable
import sys
#There is no register keyword in python
m = 1
#Result
for m in range(1,5+1):
sys.stdout.write("\t%d"%(m))
#Register class variable
import sys
#There is no register class variable in python
m = 1
for m in range(1,6):
sys.stdout.write("\t%d"%(m))