#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
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
#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()
#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
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
iNum = 5
jNum = 30
if True:
iNum = 10
print "%d %d" %(iNum, jNum)
print "%d %d" %(iNum, jNum)
cName = raw_input("Enter a string: ")
print "The reverse of the string is: "
print cName[::-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.
Count = 1
def PrintCount():
global Count
print "Count = %d" %Count
Count = Count + 1
PrintCount()
PrintCount()
PrintCount()
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.
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,
import myglobal
globalVar = 10
print "globalVar before calling = ", globalVar
myglobal.inc_global(globalVar)
print "globalVar after calling ", globalVar
import myglobal
globalVar = 10
print "globalVar before calling = ", globalVar
g = myglobal.inc_global(globalVar)
print "globalVar after calling ", g