Chapter 22: The C++ Preprocessor

Example 22.1, Page Number: 550

In [2]:
def MIN(a,b):
    if a<b:
        return a
    else:
        return b

#Variable declaration
x=10
y=20

#Result
print "The minimum is",MIN(x,y)
The minimum is 10

Example 22.2, Page Number: 551

In [3]:
def EVEN(a):
    if a%2==0:
        return 1
    else:
        return 0

if EVEN(9+1):
    print "is even"
else:
    print "is odd"
is even

Example 22.3, Page Number: 551

In [4]:
def EVEN(a):
    if a%2==0:
        return 1
    else:
        return 0

if EVEN(9+1):
    print "is even"
else:
    print "is odd"
is even

Example 22.4, Page Number: 553

In [5]:
def MAX():
    return 100

if MAX()>10:
    print "Extra memory required."
    
Extra memory required.

Example 22.5, Page Number: 554

In [7]:
def MAX():
    return 6

if MAX()>10:
    print "Extra memory required."
else:
    print "Current memory OK."
    
Current memory OK.

Example 22.6, Page Number: 556

In [8]:
def TOM():
    pass


try:
    TOM()
except NameError:
    print "Programmer is unknown."
else:
    print "Programmer is Tom."
    
try:
    RALPH()
except NameError:
    print "RALPH not defined."
    
Programmer is Tom.
RALPH not defined.

Example 22.7, Page Number: 558

In [9]:
import inspect
 
def lineno():
    return inspect.currentframe().f_back.f_lineno
    
print lineno()+200
209

Example 22.8, Page Number: 559

In [10]:
def mkstr(s):
    return str(s)

#Result
print mkstr('I like C++')
I like C++

Example 22.9, Page Number: 560

In [11]:
def concat(a,b):
    return a+b
#Variable declaration
xy=10

#Result
exec("print %s")%concat('x','y')
10
In [ ]: