Chapter 12: Preprocessor Directives

Example 12.1, Page number: 386

In [1]:
#Area of circle

import sys

#Defining the macro
#There is no macro definitions/preprocessors in python
PI =  3.14

#Variable Initialization
r = float(raw_input("Enter radius of circle in cms."))

#Calculation
area = PI * r * r

#Result
sys.stdout.write("Area of a Circle = %.2f cm2"%(area))
Enter radius of circle in cms.7
Area of a Circle = 153.86 cm2

Example 12.2, Page number: 387

In [7]:
#Identifers for 'c' statements and variables

import sys
import os

#Macro definitions
#in python, function definitions are used instead of macros
def N():
    return 10

def cls():
    os.system('cls')
    
def wait():
    t = raw_input("")
    
def display(s):
    sys.stdout.write(s)

for k in range(1,N()+1):
    display("%d\t"%(k))
    
1	2	3	4	5	6	7	8	9	10	

Example 12.3, Page number: 387

In [2]:
#Macros for logical operators

import sys

#Macro definitions
#in python, function definitions are used as macros

#There is no preprocessors in python

#Variable Initialization
a = int(raw_input("Enter Three Numbers : "))
b = int(raw_input("Enter Three Numbers : "))
c = int(raw_input("Enter Three Numbers : "))

if a > b and a > c:
    sys.stdout.write("%d is the larger number"%(a))
else:
    if b > a and b > c:
        sys.stdout.write("%d is the larger number"%(b))
    else:
        if c > a and c > b:
            sys.stdout.write("%d is the larger number"%(c))
        else:
            if a == b and b == c:
                sys.stdout.write("\nNumbers are same.")
Enter Three Numbers : 5
Enter Three Numbers : 4
Enter Three Numbers : 7
7 is the larger number

Example 12.4, Page number: 388

In [13]:
#Identifier for displaying double and triple of a number.

#Macro definitions
#Function definitions are used as macros in python

def DOUBLE(a):
    return a*2

def TRIPLE(a):
    return a*3

#Variable initialization
a = 1

#Result
sys.stdout.write("\nSINGLE\tDOUBLE\tTRIPLE")

for a in range(1,5+1):
    sys.stdout.write("\n%d\t%d\t%d"%(a,DOUBLE(a),TRIPLE(a)))
SINGLE	DOUBLE	TRIPLE
1	2	3
2	4	6
3	6	9
4	8	12
5	10	15

Example 12.5, Page number: 389

In [14]:
#Undefine a macro

#There is no preprocessor directives in python

def wait():
    return (raw_input(""))

#Result
for k in range(1,5+1):
    sys.stdout.write("%d\t"%(k))
1	2	3	4	5	

Example 12.6, Page number: 389

In [16]:
#Stringizing operation

#Macro definition
#in python, function definition can be used as macro

def say(m):
    sys.stdout.write(m)
    
#Function call
say("Hello")
Hello

Example 12.7, Page number: 390

In [3]:
#Stringizing operation and macro arguments

#There is no macro in python, function definitions can be used as macros

def DOUBLE(x):
    sys.stdout.write("Double of m = %d\n"%(x*2))
    
def TRIPLE(x):
    sys.stdout.write("Triple of m = %d\n"%(x*3))
    
#Variable Initialization
m = int(raw_input("Enter a number : "))

#Function call & Result
DOUBLE(m)
TRIPLE(m)
Enter a number : 5
Double of m = 10
Triple of m = 15

Example 12.8, Page number: 390

In [1]:
#Larger of two numbers using macro with arguments.

import sys

#Macro definition  //function definition itself is used as macro
def MAX(x,y):
    if x > y:
        c = x
    else:
        c = y
    return c

#Variable Initialization
x = 5
y = 8

#Function call
c = MAX(x,y)

#Result
sys.stdout.write("\nLarger of two numbers = %d"%(c))
Larger of two numbers = 8

Example 12.9, Page number: 391

In [3]:
#Function defined in "udf.c" file.

import sys
#since ipython notebook does not support this function, function is used
#in python, the udf.c file can be written and saved in udf.py and import command
#can be used to include that file in this program

def display():
    sys.stdout.write("\nFunction Called")
    
#Function call
display()
Function Called

Example 12.10, Page number: 392

In [7]:
#Conditional compilation 

import sys

#There is no preprocessor directives in python

LINE = 1

if LINE:
    sys.stdout.write("This is line number one")
else:
    sys.stdout.write("This is line number two")
This is line number one

Example 12.11, Page number: 393

In [9]:
#Conditional compilation 
import sys

#There is no preprocessor directives in python
def E():
    return 1

#Function call
if E():
    a = 2
    b = 3
    sys.stdout.write("A = %d  &  B = %d"%(a,b))
else:
    c = 2
    d = 3
    sys.stdout.write("C = %d  &  D = %d"%(c,d))
    
A = 2  &  B = 3

Example 12.12, Page number: 394

In [12]:
#Conditional compilation directives 

import sys

#There is no preprocessor directives in python
T = 8

#Result
if T == 0:
    sys.stdout.write("\nMacro is not defined.")
else:
    sys.stdout.write("\nMacro is defined")
Macro is defined

Example 12.13, Page number: 394

In [14]:
#User defined error message 

import sys

#There is no preprocessor directives in python
B = 1
A = 0

#Result
if A == 0:
    sys.stdout.write("MACRO A IS NOT DEFINED.")
else:
    sys.stdout.write("Macro found.")
MACRO A IS NOT DEFINED.

Example 12.14, Page number: 397

In [15]:
#Set off certain errors 

import sys

#There is no preprocessor directives in python
x = 2
y = 1

#Result
sys.stdout.write("\ny = %d"%(y))
y = 1

Example 12.15, Page number: 398

In [31]:
#Predefined macros 

import sys
import datetime

#Variable Initialization
FILE = 'PRE_MA~1.C'
LINE = 10
STDC = 1

#Date/time can be set using datetime.date(2001,5,12)
#Result
sys.stdout.write("\nDATE    :  %s"%('Dec 05 2001')) 
sys.stdout.write("\nTIME    :  %s"%(datetime.time(21,7,39)))
sys.stdout.write("\nFILE NAME : %s"%(FILE))
sys.stdout.write("\nLINE NO : %d"%(LINE))
sys.stdout.write("\n_STDC_  :  %x"%(STDC))
DATE    :  Dec 05 2001
TIME    :  21:07:39
FILE NAME : PRE_MA~1.C
LINE NO : 10
_STDC_  :  1

Example 12.16, Page number: 399

In [32]:
#Display the name of memory model that is currently in use.

import sys

#There is no formal memory model and preprocessor directives in python

TINY = 0
SMALL = 0
MEDIUM = 0
COMPACT = 0
LARGE = 1
HUGE = 0

#Result
if TINY:
    sys.stdout.write("\nTINY %d"%(TINY))
else:
    if SMALL:
        sys.stdout.write("\nSMALL %d"%(SMALL))
    else:
        if MEDIUM:
            sys.stdout.write("\nMEDIUM %d"%(MEDIUM))
        else:
            if COMPACT:
                sys.stdout.write("\nCOMPACT %d"%(COMPACT))
            else:
                if LARGE:
                    sys.stdout.write("\nLARGE %d"%(LARGE))
                else:
                    sys.stdout.write("\nHUGE %d"%(HUGE))
LARGE 1

Example 12.17, Page number: 400

In [4]:
#Macro expansions

import sys

#There is no preprocessor directives in python
ch = raw_input("Input a Text : ")

#Result
sys.stdout.write("Text Input : %s"%(ch))
Input a Text : Programming
Text Input : Programming

Example 12.18, Page number: 401

In [5]:
#Predefined macros.

import sys

#Variable Initialization
d = raw_input("Enter any character : ")

#Result
f = d.isalpha()

if f != 0:
    sys.stdout.write("\n%c is a letter in"%(d))
    f = d.isupper()
    if f != 0:
        sys.stdout.write(" Capital case")
    else:
        sys.stdout.write(" Small case")
else:
    f = d.isdigit()
    if f != 0:
        sys.stdout.write("\n%c is a digit"%(d))
    else:
        f = d.ispunct()
        if f != 0:
            sys.stdout.write("\n%c is a punctuation symbol"%(d))
Enter any character : C

C is a letter in Capital case
In [ ]: