Chapter 10: Essential Input and Output

Program 10.1, page no. 400

In [3]:
import sys

def try_input(prompt, format):
    value_count = 5
    print prompt
    fp1 = float(raw_input("fp1: "))
    i = int(raw_input("i: "))
    j = int(raw_input("j: "))
    word1 = raw_input()
    size_word1 = sys.getsizeof(word1)
    word2 = raw_input()
    size_word2 = sys.getsizeof(word2)
    print "The input format string for scanf_s() is: \"%s\"" %format
    print "Count of bytes read = ", (size_word1 + size_word2)
    print "Count of values read = %d" % value_count
    print "fp1 = %f i = %d j = %d" %(fp1, i, j)
    print "word1 = %s word2 = %s" %(word1, word2)
 
try_input("Enter as input: -2.35 15 25 ready2go ", "%f %d %d %[abcdefghijklmnopqrstuvwxyz] %*1d %s%n" )
try_input("Enter the same input again: ",    "%4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n")
try_input("Enter as input: -2.3 15 25 ready2go\n","%4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n")
Enter as input: -2.35 15 25 ready2go 
fp1: -2.35
i: 15
j: 25
ready2
go
The input format string for scanf_s() is: "%f %d %d %[abcdefghijklmnopqrstuvwxyz] %*1d %s%n"
Count of bytes read =  82
Count of values read = 5
fp1 = -2.350000 i = 15 j = 25
word1 = ready2 word2 = go
Enter the same input again: 
fp1: -2.35
i: 15
j: 25
ready 2go
2go
The input format string for scanf_s() is: "%4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n"
Count of bytes read =  86
Count of values read = 5
fp1 = -2.350000 i = 15 j = 25
word1 = ready 2go word2 = 2go
Enter as input: -2.3 15 25 ready2go

fp1: -2.35
i: 15
j: 225
ready2
go
The input format string for scanf_s() is: "%4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n"
Count of bytes read =  82
Count of values read = 5
fp1 = -2.350000 i = 15 j = 225
word1 = ready2 word2 = go

Program 10.2, page no. 403

In [4]:
value_count = 2
print "Enter: fp1 = 3.14159 i = 7 j = 8"
print "Input: "
fp1 = float(raw_input("fp1: "))
i = int(raw_input("i: "))
j = int(raw_input("j: "))

print "Output:"
print "Count of values read = ", value_count
print "fp1 = %f\ti = %d\tj = %d " %(fp1, i, j)
Enter: fp1 = 3.14159 i = 7 j = 8
Input: 
fp1: 3.13159
i: 7
j: 8
Output:
Count of values read =  2
fp1 = 3.131590	i = 7	j = 8 

Program 10.3, page no. 405

In [5]:
value_count = 3
print "Enter: 3.14 3.14 3.14"
 
print "Input: "

fp1 = float(raw_input("fp1: "))
fp2 = float(raw_input("fp2: "))
fp3 = float(raw_input("fp3: "))

print "Output: "
print "Number of values read = ", value_count
print "fp1 = %f fp2 = %f fp3 = %f" %(fp1, fp2, fp3)
Enter: 3.14 3.14 3.14
Input: 
fp1: 3.14
fp2: 3.14
fp3: 3.14
Output: 
Number of values read =  3
fp1 = 3.140000 fp2 = 3.140000 fp3 = 3.140000

Program 10.4, page no. 406

In [6]:
print "Enter three integer values: "
n = 3
i = int(raw_input())
j = int(raw_input(), 16)
k = int(raw_input(), 8)
 
print "Output:"
print "%d values read." % n
print "i = %d j = %d k = %d " %(i, j, k )
Enter three integer values: 
5
8
6
Output:
3 values read.
i = 5 j = 8 k = 6 

Program 10.5, page no. 408

In [9]:
print "Enter your first initial: ",
initial = raw_input()
print "Enter your first name: ",
name = raw_input()

if(initial != name[0]):
    print "%s, you got your initial wrong. " % name
else:
    print "Hi, %s. Your initial is correct. Well done! " %name 
    print "Enter your full name and your age separated by a comma: ", 
    name_age = raw_input()
    print "Your name is %s and you are %s years old." %(name_age.split(',')[0], name_age.split(',')[1])
Enter your first initial: M
 Enter your first name: Michael
 Hi, Michael. Your initial is correct. Well done! 
Enter your full name and your age separated by a comma: Michael, 35
 Your name is Michael and you are  35 years old.

Program 10.6, page no. 410

In [10]:
print "Enter your first initial: ",
initial = raw_input()
print "Enter your first name: ",
name = raw_input()

if(initial != name[0]):
    print "%s, you got your initial wrong. " % name
else:
    print "Hi, %s. Your initial is correct. Well done! " %name 
Enter your first initial: M
 Enter your first name: Nichael
 Nichael, you got your initial wrong. 

Program 10.7, page no. 412

In [11]:
print "Enter a sequence of integers and alphabetic names in a single line(separated by spaces): ",
text = raw_input()
split_text = text.split(' ')
for element in split_text:
    if element.isdigit():
        print "Integer Value: ", element
    elif element.isalpha():
        print "Name: ", element
    else:
        pass 
Enter a sequence of integers and alphabetic names in a single line(separated by spaces): Fred 22 34 Mary Jack 89 Jane
 Name:  Fred
Integer Value:  22
Integer Value:  34
Name:  Mary
Name:  Jack
Integer Value:  89
Name:  Jane

Program 10.8, page no. 419

In [12]:
i = 15
j = 345
k = 4567
li  = 56789
lj = 67891234567
lk = 23456789

print "i = %d j = %d k = %d li = %6.3d lj = %6.3d lk = %6.3d" %(i ,j, k, i, j, k)
print "i = %-d j = %+d k = %-d i = %-6.3d j = %-6.3d k = %-6.3d " %(i ,j, k, i, j, k)
print "li = %d lj = %d lk = %d " %(li, lj, lk)
print "li = %d lj = %d lk = %d " %(li, lj, lk)
i = 15 j = 345 k = 4567 li =    015 lj =    345 lk =   4567
i = 15 j = +345 k = 4567 i = 015    j = 345    k = 4567   
li = 56789 lj = 67891234567 lk = 23456789 
li = 56789 lj = 67891234567 lk = 23456789 

Program 10.9, page no. 421

In [13]:
k = "678"
hex_k = int(k, 16)
oct_k = int(oct(int(k)))
k = 678
print "%%d %%o %%x %%X "
print "%d %d %d %d " %(k, oct_k, hex_k, hex_k)
print "|%%8d \t|%%-8d \t|%%+8d \t|%%08d \t|%%-+8d"
print "|%8d |%-8d |%+8d |%08d |%-+8d |" %(k, k, k, k, k)
%%d %%o %%x %%X 
678 1246 1656 1656 
|%%8d 	|%%-8d 	|%%+8d 	|%%08d 	|%%-+8d
|     678 |678      |    +678 |00000678 |+678     |

Program 10.10, page no. 422

In [15]:
fp1 = 345.678
fp2 = 1.234E6
fp3 = 234567898.0
fp4 = 11.22334455e-6
 
print "%f %+f %-10.4f %6.4f" %(fp1, fp2, fp1, fp2)
print "%e %+E\n" %(fp1, fp2)
print "%f %g %#+f %8.4f %10.4g " %(fp3,fp3, fp3, fp3, fp4)
345.678000 +1234000.000000 345.6780   1234000.0000
3.456780e+02 +1.234000E+06

234567898.000000 2.34568e+08 +234567898.000000 234567898.0000  1.122e-05 

Program 10.11, page no. 423

In [1]:
import string

count = 0
print "The printable characters are the following: "
printable = string.printable
for char in printable:
    if count % 32 == 0:
        print ""
    print char,
    count += 1
The printable characters are the following: 

0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v 
w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ! " 
# $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~