#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))
#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))
#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.")
#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)))
#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))
#Stringizing operation
#Macro definition
#in python, function definition can be used as macro
def say(m):
sys.stdout.write(m)
#Function call
say("Hello")
#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)
#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))
#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()
#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")
#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))
#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")
#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.")
#Set off certain errors
import sys
#There is no preprocessor directives in python
x = 2
y = 1
#Result
sys.stdout.write("\ny = %d"%(y))
#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))
#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))
#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))
#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))