Chapter 11: Storage Class

Example 11.1, Page number: 376

In [1]:
#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))
V = 20
V = 30
V = 10

Example 11.2, Page number: 376

In [5]:
#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
X = 10
X = 20
X = 10

Example 11.3, Page number: 377

In [7]:
#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))
X = 10
X = 2.22
X = Auto Storage Class
X = 10

Example 11.4, Page number: 378

In [8]:
#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))
In Call1() V = 10
In call2() V = 10
In main() V = 10

Example 11.5, Page number: 379

In [9]:
#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))
In Call1() V = 20
In call2() V = 10
In main() V = 10

Example 11.6, Page number: 380

In [10]:
#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))
M = 10

Example 11.7, Page number: 380

In [15]:
#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)
m = 1
m = 2
m = 3

Example 11.8, Page number: 381

In [16]:
#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
x = 10 & y = 0

Example 11.9, Page number: 382

In [18]:
#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))
	1	2	3	4	5

Example 11.10, Page number: 382

In [19]:
#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))
	1	2	3	4	5
In [ ]: