Chapter 6: Storage classes

Example 6.1, Page number: 180

In [1]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Function declaration, calculation and result
def fact (n) :
   prod = 1
   while n :
      prod = prod * n
      n = n - 1
   return prod

print ('Factorial of %d is %d' % (n, fact(n)))

m = int(raw_input('Enter another number '))

print ('Factorial of %d is %d' % (m, fact(m)))

print ('Factorial of 6 is %d' % fact(6))
Enter any number 5
Factorial of 5 is 120
Enter another number 3
Factorial of 3 is 6
Factorial of 6 is 720

Example 6.2, Page number: 183

In [2]:
# Variable declaration
a = int(raw_input('Enter first number '))
b = int(raw_input('Enter second number '))

# Function declaration, calculation and result
def add (a, b) :
   global som
   som = a + b

add (a, b)

print ('The sum of %d + %d = %d' % (a, b, som))
Enter first number 2
Enter second number 3
The sum of 2 + 3 = 5

Example 6.3, Page number: 187

In [3]:
# Variable declaration
global y
y = 90
m = 10
z = 2.18

# Function declaration, calculation and result
def fun1 (m, y, z) :
   z = 3.141
   print ('%f' % z)

fun1 (m, y, z)

print ('%d %d %f' % (m, y, z))
3.141000
10 90 2.180000

Example 6.4, Page number: 190

In [4]:
# Variable declaration
global x, y, z, ch, tmp
z = 2.18
y = 90
ch = '#'
m = 10
x = '*'
tmp = '^'

# Function declaration, calculation and result
def fun1 (m, y, z) :
   z = 3.141
   num = 100
   tmp = '$'
   x = 50
   ch = '@'
   print ('In FUNCTION FORMAL PARAMETERS')
   print ('In the function value of m = %d address of m = %x' % (m, id(m)))
   print ('In the function value of y = %d address of y = %x' % (y, id(y)))
   print ('In the function value of z = %f address of z = %x' % (z, id(z)))

   print ('In FUNCTION LOCAL VARIABLES')
   print ('In the function value of z = %f address of z = %x' % (z, id(z)))
   print ('In the function value of num = %d address of num = %x' % (num, id(num)))
   print ('In the function value of tmp = %s address of tmp = %x' % (tmp, id(tmp)))

   print ('In FUNCTION GLOBAL VARIABLES')
   print ('In the function value of x = %s address of x = %x' % (x, id(x)))
   print ('In the function value of y = %d address of y = %x' % (y, id(y)))
   print ('In the function value of ch = %s address of ch = %x' % (ch, id(ch)))


print ('In MAIN LOCAL VARIABLES')
print ('In Main the value of m = %d address of m = %x' % (m, id(m)))
print ('In Main the value of x = %s address of x = %s' % (x, id(x)))
print ('In MAIN GLOBAL VARIABLES')
print ('In Main the value of y = %d address of y = %x' % (y, id(y)))
print ('In Main the value of z = %f address of z = %x' % (z, id(z)))
print ('In Main the value of ch = %s address of ch = %s' % (ch, id(ch)))
print ('In Main the value of tmp = %s address of tmp = %x' % (tmp, id(tmp)))

fun1 (m, y, z)
In MAIN LOCAL VARIABLES
In Main the value of m = 10 address of m = 117d0c0
In Main the value of x = * address of x = 140148786619376
In MAIN GLOBAL VARIABLES
In Main the value of y = 90 address of y = 117d8d0
In Main the value of z = 2.180000 address of z = 16ca340
In Main the value of ch = # address of ch = 140148787513184
In Main the value of tmp = ^ address of tmp = 7f76eea46eb8
In FUNCTION FORMAL PARAMETERS
In the function value of m = 10 address of m = 117d0c0
In the function value of y = 90 address of y = 117d8d0
In the function value of z = 3.141000 address of z = 16ca2f8
In FUNCTION LOCAL VARIABLES
In the function value of z = 3.141000 address of z = 16ca2f8
In the function value of num = 100 address of num = 117d7e0
In the function value of tmp = $ address of tmp = 7f76eeb088a0
In FUNCTION GLOBAL VARIABLES
In the function value of x = 50 address of x = 117d4c8
In the function value of y = 90 address of y = 117d8d0
In the function value of ch = @ address of ch = 7f76ed6203f0

Example 6.5, Page number: 193

In [5]:
# Variable declaration
a = int(raw_input('Enter first number '))
b = int(raw_input('Enter second number '))

# Function declaration, calculation and result
def add (a, b) :
   global som
   som = a + b

add (a, b)

print ('The sum of %d + %d = %d' % (a, b, som))
Enter first number 20
Enter second number 30
The sum of 20 + 30 = 50

Example 6.6, Page number: 194

In [6]:
# Variable declaration
a = int(raw_input('Enter first number '))
b = int(raw_input('Enter second number '))

# Function declaration, calculation and result
def greater (a, b) :
   global big
   if a == b :
      big = 'equal'
   else :
      if a > b :
         big = 'first'
      else :
         big = 'second'
   return big

greater (a, b)

if big == 'equal' :
   print ('EQUAL')
else :
   if big == 'first' :
      print ('First number is greater %d' % a)
   else :
      print ('Second number is greater %d' % b)
Enter first number 20
Enter second number 20
EQUAL

Example 6.7, Page number: 196

In [7]:
# Variable declaration
a = int(raw_input('Enter first number '))
b = int(raw_input('Enter second number '))
c = int(raw_input('Enter third number '))

# Function declaration, calculation and result
def greater (a, b, c) :
   if a>b :
      if a>c :
         print ('The greatest number is %d ' % a)
      else :
         print ('The greatest number is %d ' % c)

   else :
      if b>c :
         print ('The greatest number is %d ' % b)
      else : 
         print ('The greatest number is %d ' % c)

greater (a, b, c)
Enter first number 190
Enter second number 128
Enter third number 45
The greatest number is 190 

Example 6.8, Page number: 198

In [8]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Function declaration, calculation and result
def summate (n) :
   i = 1
   global total
   total = 0
   while i <= n :
      total = total + i
      i = i+1
   
summate (n)

print ('Sum of first %d natural numbers is %d' % (n, total))
Enter any number 6
Sum of first 6 natural numbers is 21

Example 6.9, Page number: 199

In [9]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Function declaration, calculation and result
def sumofdigit (n) :
   global sum
   sum = 0
   while n > 0 :
      sum = sum + n%10
      n = n/10
   return sum

sumofdigit (n)

print ('Sum of digits of %d is %d' % (n, sum))
Enter any number 7612
Sum of digits of 7612 is 16

Example 6.10, Page number: 201

In [10]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Function declaration, calculation and result
def checkprime (n) :
   flag = 1
   i = 2
   while i<n and flag :
      if n%i == 0 :
         flag = 0
      i = i+1
   return flag

result = checkprime (n)

if result :
   print ('The number %d is PRIME ' % n)
else :
   print ('The number %d is COMPOSITE ' % n)
Enter any number 14
The number 14 is COMPOSITE 

Example 6.11, Page number: 203

In [11]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Function declaration, calculation and result
def makethird (n) :
   first = 0
   second = 1
   count = 3
   print ('%d terms of the Fibonacci series are ' % n)
   print ('%d  %d ' % (first, second)),
   while count<=n :
      third = first + second
      first = second
      second = third
      print (' %d ' % third),
      count = count + 1

makethird (n)
Enter any number 10
10 terms of the Fibonacci series are 
0  1   1   2   3   5   8   13   21   34 

Example 6.12, Page number: 205

In [12]:
# Variable declaration
n = int(raw_input('Enter any number '))
sum = n

# Function declaration, calculation and result
def sumofdigit (n, sum) :
   while sum>=10 :
      sum = 0
      while n>0 :
         sum = sum + n%10
         n = n/10
      n = sum
   return sum

result = sumofdigit (n, sum)

print ('Sum of digits of %d is %d' % (n, result))
Enter any number 7612
Sum of digits of 7612 is 7

Example 6.13, Page number: 207

In [14]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Function declaration, calculation and result
def prime (n) :
   for num in range (2, n) :
      flag = 1
      for i in range (2, num) :
         if num % i == 0 : 
            flag = 0
      if flag :
         print ('%d ' % num),

prime (n)
Enter any number 20
2  3  5  7  11  13  17  19 

Example 6.14, Page number: 210

In [15]:
# Variable declaration
m = int(raw_input('Enter any number '))

# Function declaration, calculation and result
def fun1 (m) :
   if not hasattr(fun1, "temp") : 
      fun1.temp = 0
   print ('In function value of temp = %d' % fun1.temp)
   if fun1.temp < m :
      fun1.temp = m
   else :
      fun1.temp = fun1.temp + 1

for i in range (1, 6) :
   fun1 (m)
Enter any number 10
In function value of temp = 0
In function value of temp = 10
In function value of temp = 11
In function value of temp = 12
In function value of temp = 13

Example 6.15, Page number: 212

In [16]:
# Variable declaration
m = 10
i = 1

# Function declaration, calculation and result
def fun1 (m) :
   x = m
   if not hasattr(fun1, "temp") : 
      fun1.temp = 0

   if fun1.temp == 0 :
      print ('\nIn FUNCTION Global variable')
      print ('Value of m = %d address of m = %x' % (m, id(m)))
      print ('\nIn FUNCTION Formal parameter variable')
      print ('Value of x = %d address of x = %x' % (x, id(x)))
      print ('\nIn FUNCTION Static variable')
      print ('Value of temp = %d address of temp = %x' % (fun1.temp, id(fun1.temp)))

   if fun1.temp < m :
      fun1.temp = m

   else :
      print ('Value of temp = %d' % fun1.temp)
      fun1.temp = fun1.temp + 1

print ('\nIn MAIN Global variable')
print ('Value of m = %d address of m = %x' % (m, id(m)))
print ('\nIn MAIN Local variable')
print ('Value of i = %d address of i = %x' % (i, id(i)))
      
for i in range (1, 6) :
   fun1 (m)
In MAIN Global variable
Value of m = 10 address of m = 117d0c0

In MAIN Local variable
Value of i = 1 address of i = 117d198

In FUNCTION Global variable
Value of m = 10 address of m = 117d0c0

In FUNCTION Formal parameter variable
Value of x = 10 address of x = 117d0c0

In FUNCTION Static variable
Value of temp = 0 address of temp = 117d1b0
Value of temp = 10
Value of temp = 11
Value of temp = 12
Value of temp = 13

Example 6.16, Page number: 215

In [17]:
# Variable declaration
n = int(raw_input('Enter number of Fibonacci terms required '))

# Function declaration, calculation and result
def nextfibo () :
   if not hasattr(nextfibo, "first") : 
      nextfibo.first = 0
   if not hasattr(nextfibo, "second") : 
      nextfibo.second = 1
   if not hasattr(nextfibo, "third") : 
      nextfibo.third = 0
   nextfibo.third = nextfibo.first + nextfibo.second
   nextfibo.first = nextfibo.second
   nextfibo.second = nextfibo.third
   return nextfibo.first
 
print ('%d terms of Fibonacci series are' % n) 
print 0,
for count in range (2, n+1) :
   result = nextfibo ()
   print ('%d' % result),
Enter number of Fibonacci terms required 10
10 terms of Fibonacci series are
0 1 1 2 3 5 8 13 21 34