Chapter 6: Scope & Extent

Example scope1.c, page no. 202

In [2]:
#as mentioned in the textbook, the statement j = 30 will not give an error as in Python you can declare a vairable anywhere
#however, the statement does not refer to the variable j of the function.
def func():
    j = 20
    print "j is a variable in the function"
    print "Value of j is ",j
i = 10
print "i is a variable in main."
print "Value of i is ", i
print "Calling func..."
func()
j = 30
i is a variable in main.
Value of i is  10
Calling func...
j is a variable in the function
Value of j is  20

Example global.c, page no. 202

In [5]:
g = 0
def func():
    global g
    print "In func. g is visbile here, since it is global"
    print "Incrementing g in func..."
    g += 1
    return g
print "In main. g is visible here, since it is global"
print "assigning 20 to g in main..."
g = 20
print "Callint func..."
func()
print "func returned g is ", g
In main. g is visible here, since it is global
assigning 20 to g in main...
Callint func...
In func. g is visbile here, since it is global
Incrementing g in func...
func returned g is  21

Example colcolon.c, page no. 203

In [10]:
#there is no concept of scope resolution operator in Python. we will do it in different way
a = 10
def func():
    global a
    b = 20
    print b
    print a
func()
20
10

Example 6.1, page no. 204

In [12]:
#there is no concept of block in Python. writing if True is smiliar to it but still, output will differ
j = 144
print "The value of j outside the block is ", j
if True:
    j = 12
    print "The value of j inside the block is ", j
print "The value of j outside the block is ", j
The value of j outside the block is  144
The value of j inside the block is  12
The value of j outside the block is  12

Example 6.2, page no. 205

In [14]:
i = 144
j = 132
print "i = ", i
if True:
    k = 12
    k = int(raw_input())
    i = i%k
if i == 0:
    print "i is a divisor of %d " %k
i =  144
25

Example 6.3, page no. 205

In [16]:
iNum = 5
jNum = 30
if True:
    iNum = 10
    print "%d %d" %(iNum, jNum)
print "%d %d" %(iNum, jNum)
10 30
10 30

Example 7, page no. 207

In [18]:
cName = raw_input("Enter a string: ")
print "The reverse of the string is: "
print cName[::-1]
Enter a string: this is a string
The reverse of the string is: 
gnirts a si siht

Example 6.4 pageno : 206

In [1]:
iGlobal = 35
Number = 47
iVal = 99
Number = 1000
print "The value of local variable Number is %d"%Number
print "The value of global variable iGlobal is %d"%iGlobal
print "The value of global variable Number is %d"%Number
print "The value of static global variable iVal is %d"%iVal

# Python has no static functionality. and also local variable changes value when value assigned to it.
The value of local variable Number is 1000
The value of global variable iGlobal is 35
The value of global variable Number is 1000
The value of static global variable iVal is 99

Example 6.5, page no. 208

In [19]:
Count = 1
def PrintCount():
    global Count
    print "Count = %d" %Count
    Count = Count + 1
PrintCount()
PrintCount()
PrintCount()
Count = 1
Count = 2
Count = 3

Example 6.6, page no.208

In [2]:
uF1 = 0
uF2 = 1
def PrintNextFibo():
    global uF1,uF2
    uFpre = uF1 + uF2
    print "%d" %uFpre ,
    uF2 = uF1
    uF1 = uFpre
print "Fibonacci sequences using static variables :"
print "Enter how many number are to be generated :",
itotNum = int(raw_input())
print "The first %d fibonacci numbers are" %itotNum
for iIndex in range(1,itotNum+1):
    PrintNextFibo()

# it starts with zero but here function created called uses global variable so we need to print/assume manually zero value.
Fibonacci sequences using static variables :
Enter how many number are to be generated :5
 The first 5 fibonacci numbers are
1 1 2 3 5

Example on page no. 209

In [2]:
import linsrch
import bubble
arr = []
num = 5
i = 0
pos = 1
print "Enter five elements"
while i < num:
    arr.append(int(raw_input()))
    i+=1
print "Enter element to be searched: ",
key = int(raw_input())
pos = linsrch.LinSrch(arr, num, key)
if pos:
    print "Element %d found at %d position" %(key, pos)
else:
    print "Ellement %d not found" %key
bubble.BubSort(arr, num)
print "The sorted element are"
for ele in arr:
    print ele,
Enter five elements
12
15
56
48
22
Enter element to be searched: 22
 Element 22 found at 5 position
The sorted element are
12 15 22 48 56

Example on page no. 211

In [6]:
import myglobal
globalVar = 10
print "globalVar before calling = ", globalVar
myglobal.inc_global(globalVar)
print "globalVar after calling  ", globalVar
globalVar before calling =  10
globalVar after calling   10

Example on page no. 212

In [7]:
import myglobal

globalVar = 10
print "globalVar before calling = ", globalVar
g = myglobal.inc_global(globalVar)
print "globalVar after calling  ", g
globalVar before calling =  10
globalVar after calling   11