CHAPTER 11: CONSOLE INPUT/OUTPUT

EXAMPLE ON PAGE:372

In [1]:
 
avg=346
per=69.2
print "Average=%d\nPercentage=%f\n" % (avg,per)
Average=346
Percentage=69.200000

EXAMPLE ON PAGE:374

In [2]:
 
weight=63
print "weight is %d kg\n" % (weight)
print "weight is %2d kg\n" % (weight)
print "weight is %4d kg\n" % (weight)
print "weight is %6d kg\n" % (weight)
print "weight is %-6d kg\n" % (weight)
print "weight is %1d kg\n" % (weight)
weight is 63 kg

weight is 63 kg

weight is   63 kg

weight is     63 kg

weight is 63     kg

weight is 63 kg

EXAMPLE ON PAGE:374-375

In [3]:
 
print "%f%f%f\n" % (5.0,13.5,133.9)
print "%f%f%f\n" % (305.0,1200.9,3005.3)
5.00000013.500000133.900000

305.0000001200.9000003005.300000

EXAMPLE ON PAGE:375

In [4]:
 
print "%10.1f%10.1f%10.1f\n" % (5.0,13.5,133.9)
print "%10.1f%10.1f%10.1f\n" % (305.0,1200.9,3005.3)
       5.0      13.5     133.9

     305.0    1200.9    3005.3

EXAMPLE ON PAGE:375

In [5]:
 
firstname1="Sandy"
surname1="Malya"
firstname2="AjayKumar"
surname2="Gurubaxani"
print "%20s%20s\n" % (firstname1,surname1)
print "%20s%20s\n" % (firstname2,surname2)
               Sandy               Malya

           AjayKumar          Gurubaxani

EXAMPLE ON PAGE:376

In [6]:
 
print "You\tmust\tbe\tcrazy\nto\thate\tthis\tbook\n"
You	must	be	crazy
to	hate	this	book

EXAMPLE ON PAGE:377-378

In [9]:
 
ch='z'
i=125
a=12.55
s="hello there!"
print "%c %d %f\n" % (ch,ord(ch),ord(ch))
print "%s\n" % (s)                          #here conversation not possible
print "%c %d %f\n" % (i,i,i)
print "%f %d\n" % (a,a)
z 122 122.000000

hello there!

} 125 125.000000

12.550000 12

EXAMPLE ON PAGE:379

In [10]:
 
i=10
ch='A'
a=3.14
print "%d %c %f\n" % (i,ch,a)
str=[i,ch,a]
print "%s\n" % (str)
10 A 3.140000

[10, 'A', 3.14]

EXAMPLE ON PAGE:380

In [11]:
 
print "Press any key to continue"
raw_input()                                        #will echo the character
print "\nType any character"
ch=raw_input()                                     #will echo the character typed
print "\nType any character"
raw_input()                                        #will echo character
print "\nContinue Y/N"
raw_input()                                        #will echo character
Press any key to continue
 

Type any character
B

Type any character
W

Continue Y/N
Y
Out[11]:
'Y'

EXAMPLE ON PAGE:381

In [14]:
 
ch='A'
print ch,
print(ch),
print ch,
print 'Z',
print('Z'),
print'Z',
A A A Z Z Z

EXAMPLE ON PAGE:381

In [15]:
 
print "Enter name"
name=raw_input()
print "%s\n" % (name)
Enter name
Jonty Rhodes
Jonty Rhodes

EXAMPLE ON PAGE:382

In [16]:
 
print "Enter name"
footballer=raw_input()                        #sends base address of array
print "Happy footballing!"
print footballer
Enter name
Jonty Rhodes
Happy footballing!
Jonty Rhodes