# 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))
# 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))
# 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))
# 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)
# 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))
# 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)
# 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)
# 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))
# 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))
# 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)
# 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)
# 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))
# 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)
# 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)
# 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)
# 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),