Hour 5: Handling Standard Input and Output

Example 5.1, Page No.73

In [1]:
ch=raw_input("Please type in one character")
print "The Character you just entered is: ", ch
Please type in one characterH
The Character you just entered is:  H

Example 5.2, Page No.74

In [2]:
ch1=raw_input("Please type in first character")
ch2=raw_input("Please type in second character")

print "The first character you just entered is: ",ch1
print "The second character you just entered is: ",ch2
Please type in first characterH
Please type in second characteri
The first character you just entered is:  H
The second character you just entered is:  i

Example 5.3, Page No.76

In [3]:
ch=65
print "The character that has numeric value of 65 is:",chr(ch)
The character that has numeric value of 65 is: A

Example 5.4, Page No.77

In [4]:
print chr(65)
print chr(10)
print chr(66)
print chr(10)
print chr(67)
print chr(10)
A


B


C


Example 5.5, Page No.80

In [31]:
print "Hex(uppercase)       Hex(lowercase)       Decimal"
print  "{:01X}                    {:01x}                    {:d}".format(0,0,0)
print  "{:01X}                    {:01x}                    {:d}".format(1,1,1)
print  "{:01X}                    {:01x}                    {:d}".format(2,2,2)
print  "{:01X}                    {:01x}                    {:d}".format(3,3,3)
print  "{:01X}                    {:01x}                    {:d}".format(4,4,4)
print  "{:01X}                    {:01x}                    {:d}".format(5,5,5)
print  "{:01X}                    {:01x}                    {:d}".format(6,6,6)
print  "{:01X}                    {:01x}                    {:d}".format(7,7,7)
print  "{:01X}                    {:01x}                    {:d}".format(8,8,8)
print  "{:01X}                    {:01x}                    {:d}".format(9,9,9)
print  "{:01X}                    {:01x}                    {:d}".format(10,10,10)
print  "{:01X}                    {:01x}                    {:d}".format(11,11,11)
print  "{:01X}                    {:01x}                    {:d}".format(12,12,12)
print  "{:01X}                    {:01x}                    {:d}".format(13,13,13)
print  "{:01X}                    {:01x}                    {:d}".format(14,14,14)
print  "{:01X}                    {:01x}                    {:d}".format(15,15,15)
Hex(uppercase)       Hex(lowercase)       Decimal
0                    0                    0
1                    1                    1
2                    2                    2
3                    3                    3
4                    4                    4
5                    5                    5
6                    6                    6
7                    7                    7
8                    8                    8
9                    9                    9
A                    a                    10
B                    b                    11
C                    c                    12
D                    d                    13
E                    e                    14
F                    f                    15

Example 5.6, Page No.82

In [32]:
num1=12
num2=12345
print num1
print num2
print "{:5d}".format(num1)
print "{:05d}".format(num1)
print "{:2d}".format(num2)
12
12345
   12
00012
12345

Example 5.7, Page No.83

In [35]:
num1=1
num2=12
num3=123
num4=1234
num5=12345
print "{:8d}{:-8d}".format(num1,num1)
print "{:8d}{:-8d}".format(num2,num2)
print "{:8d}{:-8d}".format(num3,num3)
print "{:8d}{:-8d}".format(num4,num4)
print "{:8d}{:-8d}".format(num5,num5)
       1       1
      12      12
     123     123
    1234    1234
   12345   12345

Example 5.8, Page No.84

In [44]:
int_num=123
flt_num=123.456789
print "Default integer format: ",int_num
print "With Precision Specifier: ",format(int_num,'08d')
print "Default float format: ",flt_num
print "With Precision Specifier: ",format(flt_num,'6.2f')
Default integer format:  123
With Precision Specifier:  00000123
Default float format:  123.456789
With Precision Specifier:  123.46