Chapter 4: Data Input and Output

Example 4.4, Page number: 4.4

In [3]:
ch='a'
print 'equivalent uppercase character is ',ch.upper()
equivalent uppercase character is  A

Example 4.16, Page number: 4.14

In [2]:
import math

i,j=2.0,3.0
print "%f %f %f %f" %(i,i,i+j,math.sqrt(i+j))
2.000000 2.000000 5.000000 2.236068

Example 4.18, Page number: 4.15

In [2]:
x,y=5000.0,0.0025

print "%f %f %f %f\n\n" %(x,y,x*y,x/y)
print "%e %e %e %e" %(x,y,x*y,x/y)
5000.000000 0.002500 12.500000 2000000.000000


5.000000e+03 2.500000e-03 1.250000e+01 2.000000e+06

Example 4.19, Page number: 4.16

In [3]:
line="The PITTSBURG STEELERS is one of America's favorite football teams!"
print "%s" %(line)
The PITTSBURG STEELERS is one of America's favorite football teams!

Example 4.20, Page number: 4.17

In [4]:
i=12345
x=345.678

print "%3d %5d %8d\n\n" %(i,i,i)
print "%3f %10f %13f" %(x,x,x)
print "%3e %13e %16e" %(x,x,x)
12345 12345    12345


345.678000 345.678000    345.678000
3.456780e+02  3.456780e+02     3.456780e+02

Example 4.21, Page number: 4.17

In [5]:
i=12345
x=345.678

print "%3d %5d %8d\n\n" %(i,i,i)
print "%3g %10g %13g" %(x,x,x)
print "%3g %13g %16g" %(x,x,x)
12345 12345    12345


345.678    345.678       345.678
345.678       345.678          345.678

Example 4.22, Page number: 4.18

In [6]:
x=123.456

print "%7f %7.3f %7.1f" %(x,x,x)
print "%12e %12.5e %12.3e" %(x,x,x)
123.456000 123.456   123.5
1.234560e+02  1.23456e+02    1.235e+02

Example 4.23, Page number: 4.19

In [7]:
x=123.456

print "%f %.3f %.1f" %(x,x,x)
print "%e %.5e %.3e" %(x,x,x)
123.456000 123.456 123.5
1.234560e+02 1.23456e+02 1.235e+02

Example 4.24, Page number: 4.20

In [8]:
line="hexadecimal"

print "%10s %15s %15.5s %.5s" %(line,line,line,line)
hexadecimal     hexadecimal           hexad hexad

Example 4.26, Page number 4.21

In [9]:
a=0x80ec
b=0.3e-12

print "%4x %10.2e\n\n" %(a,b)
print "%4X %10.2E" %(a,b)
80ec   3.00e-13


80EC   3.00E-13

Example 4.27, Page number: 4.22

In [10]:
i,x,y=123,12.0,-3.3

print ":%6d %7.0f %10.1e: \n" %(i,x,y)
print ":%-6d %-7.0f %-10.1e: \n" %(i,x,y)
print ":%+6d %+7.0f %+10.1e: \n" %(i,x,y)
print ":%-+6d %-+7.0f %-+10.1e: \n" %(i,x,y)
:   123      12   -3.3e+00: 

:123    12      -3.3e+00  : 

:  +123     +12   -3.3e+00: 

:+123   +12     -3.3e+00  : 

:     12     12.    -3.3 -3.30000: 

Example 4.28, Page number: 4.23

In [11]:
i,j,k=1234,01777,0xa08c

print ":%8u %8o %8x:\n" %(i,j,k)
print ":%-8u %-8o %-8x:\n" %(i,j,k)
print ":%08u %08o %08x:\n" %(i,j,k)
:    1234     1777     a08c:

:1234     1777     a08c    :

:    1234    01777   0xa08c:

:00001234 00001777 0000a08c:

Example 4.29, Page number: 4.24

In [12]:
line="lower-case"

print ":%15s %15.5s %.5s:" %(line,line,line)
print ":%-15s %-15.5s %-.5s:" %(line,line,line)
:     lower-case           lower lower:
:lower-case      lower           lower:

Example 4.30, Page number: 4.24

In [13]:
a,b,x1,x2=2.2,-6.2,0.005,-12.88

print "$%4.2f %7.1f%%\n" %(a,b)
print "x1=%7.3f x2=%7.3f" %(x1,x2)
$2.20    -6.2%

x1=  0.005 x2=-12.880

Example 4.32, Page number: 4.26

In [15]:
name="Robert Smith"
score1=88
score2=62.5
score3=90

average=(float)(score1+score2+score3)/3
print "Name: %-s" %(name)
print "Score1:  %-5.1f" %(score1)
print "Score2:  %-5.1f" %(score2)
print "Score3:  %-5.1f" %(score3)
print "Average: %-5.1f" %(average)
Name: Robert Smith
Score1:  88.0 
Score2:  62.5 
Score3:  90.0 
Average: 80.2 
In [ ]: