Chapter 16: Input and Output Operations in Python

Program 16.1, Page number: 350

In [8]:
def main():

        #Variable Declaration
        c='X'
        s="abcdefghijklmnopqrstuvwxyz"
        i=425
        j=17
        u=0xf179
        l=75000
        L=0x1234567812345678
        f=12.978
        d=-97.4583
        cp=c
        ip=i
        

        #Print Integers
        print("Integers:\n")
        print("%i %o %x %u" %(i,i,i,i))
        print("%x %X %#x %#X" %(i,i,i,i))
        print("%+i % i %07i %.7i" %(i,i,i, i))
        print("%i %o %x %u" %(j,j,j,j))
        print("%i %o %x %u" %(u,u,u,u))
        print("%ld %lo %lx %lu" %(l,l,l,l))
        print("%li %lo %lx %lu" %(L,L,L, L))

        #Print Floats & Doubles
        print("\nFloats and Doubles:")
        print("%f %e %g"%( f, f, f))
        print("%.2f %.2e"%( f, f))
        print("%.0f %.0e"%( f, f))
        print("%7.2f %7.2e"%( f, f))
        print("%f %e %g"%( d, d, d))
        print("%.*f"%( 3, d))
        print("%*.*f" %(8, 2, d))

        #Print Characters
        print("\nCharacters:")
        print("%c"%(c))
        print("%3c%3c"%( c, c))
        print("{0:x} ".format(ord(c))) 

        #Print Strings
        print("\nStrings:")
        print("%s"%(s))
        print("%.5s"%(s))
        print("%30s"%(s))
        print("%20.5s"%(s))
        print("%-20.5s"%(s))

        #Print variables pointers
        print("\nPointers:")
        print("{0:x} {1:x}\n".format(int(ip),ord(cp)))

        print("This%n is fun.")
        print("c1 = %i, c2 = %i\n"%(4, 12))


if __name__=='__main__':
        main()
Integers:

425 651 1a9 425
1a9 1A9 0x1a9 0X1A9
+425  425 0000425 0000425
17 21 11 17
61817 170571 f179 61817
75000 222370 124f8 75000
1311768465173141112 110642547402215053170 1234567812345678 1311768465173141112

Floats and Doubles:
12.978000 1.297800e+01 12.978
12.98 1.30e+01
13 1e+01
  12.98 1.30e+01
-97.458300 -9.745830e+01 -97.4583
-97.458
  -97.46

Characters:
X
  X  X
58 

Strings:
abcdefghijklmnopqrstuvwxyz
abcde
    abcdefghijklmnopqrstuvwxyz
               abcde
abcde               

Pointers:
1a9 58

This%n is fun.
c1 = 4, c2 = 12

Program 16.2, Page number: 362

In [1]:
import sys

def main():
        
        while (True):
                #c=raw_input()              #User Input
                c="this is a sample text"   #Dummy text
                print(c);         #Display Result
                
                #Un-comment this while executing from terminal/command line
                #if(c=="EOF"):     #Check Exit condition
                sys.exit()
        

if __name__=='__main__':
        main()
An exception has occurred, use %tb to see the full traceback.

SystemExit
this is a sample text
To exit: use 'exit', 'quit', or Ctrl-D.

Program 16.3, Page number: 366

In [2]:
import sys
def main():

        #Variable Declaration/ User Input
        print("Enter name of the file to be copied: ")
        inName="source.txt"
        #inName=raw_input()

        print("Enter name of the output file: ")
        outName="target.txt"
        #outName=raw_input()

        
        try:
            inn=open(inName,"r")         
        except:# Exception:
                print("cant open {0} for reading".format(inName))
                sys.exit()

        try:
               out=open(outName,"w") 
        except:# Exception:
                print("cant open {0} for writing".format(outName))
                sys.exit()

        string=inn.read()               #Read content from File-1
        out.write(string)               #Write content to File-2

        inn.close()
        out.close()

        print("File has been copied.\n");

if __name__=='__main__':
        main()
An exception has occurred, use %tb to see the full traceback.

SystemExit
Enter name of the file to be copied: 
Enter name of the output file: 
cant open source.txt for reading
To exit: use 'exit', 'quit', or Ctrl-D.
In [ ]: