Chapter 2: Syntactic Aspects

Example 1, Page number: 39

In [11]:
# Calculation and result
class color() :
   RED = 5
   YELLOW = 6
   GREEN = 4
   BLUE = 5

print ('RED = %d ' % color.RED)
print ('YELLOW = %d ' % color.YELLOW)
print ('GREEN = %d ' % color.GREEN)
print ('BLUE = %d ' % color.BLUE)
RED = 5 
YELLOW = 6 
GREEN = 4 
BLUE = 5 

Example 2, Page number: 41

In [6]:
# Calculation and result
i = 2004
c = 'Year'

print ('%s %d' % (c, i))
Year 2004

Example 2.1, Page number: 44

In [1]:
# Calculation and result
num = 5
c = '$'
pi = 3.141
print ('Hello World')
print ('%d %c %f' % (num, c, pi))
Hello World
5 $ 3.141000

Example 2.2, Page number: 45

In [2]:
# Calculation and result
num = int(raw_input('Enter your favorite number '))
print ('You have entered your favorite number as %d ' % num)
Enter your favorite number 3
You have entered your favorite number as 3 

Example 2.3, Page number: 60

In [3]:
# Calculation and result
fahrenheit = float(raw_input('Please enter the Fahrenheit temperature '))

centigrade = float(5)/9 * (fahrenheit - 32)

print ('The temperature in Centigrade is %.2f ' % centigrade)
Please enter the Fahrenheit temperature 100
The temperature in Centigrade is 37.78