Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python: Input and Output Yuen Kwan Lo. Output Format str( ) and repr( ) same representation but String and Floating point number a=0.24 str(a)‘0.24’ repr.

Similar presentations


Presentation on theme: "Python: Input and Output Yuen Kwan Lo. Output Format str( ) and repr( ) same representation but String and Floating point number a=0.24 str(a)‘0.24’ repr."— Presentation transcript:

1 Python: Input and Output Yuen Kwan Lo

2 Output Format str( ) and repr( ) same representation but String and Floating point number a=0.24 str(a)‘0.24’ repr (a)‘0.2399999999999999999’ s = 'Hello, world.' str(s) 'Hello, world.' repr(s) "'Hello, world.'" print ( repr(s) )‘Hello, word.’

3 rjust(), ljust(), center() Returns string justified in a string of given length str = “hello world” print str.rjust (20) >> hello world print str.rjust(20, ‘!’) >> !!!!!!!!!hello world zfill() Pads a numeric string on the left with zeros '-3.14'.zfill(7) '-003.14'

4 for x in range(1, 5): print repr(x).rjust(2), repr(x*x).rjust(3), print repr(x*x*x).rjust(4) format() for x in range(1, 5): print '{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x) print ‘PI : {0:.3f}'.format(math.pi) >> PI: 3.142 !s ( str() ) and !r ( repr() ) print ‘PI : {!r}'.format(math.pi) >> PI: 3.141592653589793 print ‘PI : {!s}'.format(math.pi) >> PI: 3.14159265359

5 Print set of variables print '{1} and {0}'.format('spam', 'eggs') >> eggs and spam print '{dollar} dollars and {cent} cents'.format(dollar=‘ten’, cent=‘forty’) >> ten dollars and forty cents Print table table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678} for name, phone in table.items(): print '{0:10} ==> {1:10d}'.format(name, phone) table = {'Sjoerd': 4127, 'Dcab': 8637678} print (‘Sjoerd: {0[Sjoerd]:d}; Dcab: {0[Dcab]:d}'.format(table)) >> Jack: 4098; Sjoerd: 4127; Dcab: 8637678 print 'Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table)

6 String Formatting veg = “apple” color = “red” print veg + “ is “ + color>> apple is red print “%s is %s” % (veg, color)>> apple is red count= 6 print “There are %d %s” % (count, veg)>> There are 6 apple print “There are “ + count + vegERROR!! Numbers Formatting print "Today's stock price: %f" % 50.4625 >> Today's stock price: 50.462500 print "Today's stock price: %.2f" % 50.4625 >> Today's stock price: 50.46 print "Change since yesterday: %+.2f" % 1.5 >> Change since yesterday: +1.50

7 Reading and Writing Files open() open(filename, mode) f = open('/cs/PA', 'w') mode: ‘r’read ‘w’write*file with same name will be erased* ‘a’append ‘r+’read and write ‘b’open file in binary mode (on Windows)

8 Methods of File Objects f.read(size) f.read()**read the entire file and return string** f.read(1)**read one character at a time** f.readline() reads a single line from the file until it reaches ‘\n’ f.readlines() Read all lines and returns a list of all lines >>['This is the first line of the file.\n', 'Second line of the file\n']

9 f.write(string) write the contents of string to the file f.write(‘first line\n’) * *make numbers to string before writing** s = str (0.24) f.write(s) f.tell() return current position in file measured in bytes from the beginning of the file f.close() close file and file object cannot be used unless being reopened

10 f.seek(offset, from) from: 0: beginning of file 1: current position 2: end of file f = open('/tmp/workfile', 'r+') f.write('0123456789abcdef') f.seek(5) # Go to the 6th byte in the file f.read(1) '5' f.seek(-3, 2) # Go to the 3rd byte before the end f.read(1) 'd'

11 Pickle Module selfref_list = [1, 2, 3] output = open('data.pkl', 'wb') pickle.dump(selfref_list, output) data1 = pickle.load(output) When reading a pickle-containing file, you should open the file in binary mode because you can't be sure if the ASCII or binary format was used

12 Reference http://docs.python.org/tutorial/inputoutput.html http://www.tutorialspoint.com/python/string_rjust.htm http://diveintopython.org/native_data_types/formatting_ strings.html http://diveintopython.org/native_data_types/formatting_ strings.html http://docs.python.org/library/ http://en.wikibooks.org/wiki/Python_Programming/Input _and_output http://en.wikibooks.org/wiki/Python_Programming/Input _and_output http://docs.python.org/release/2.5.2/lib/pickle- example.html


Download ppt "Python: Input and Output Yuen Kwan Lo. Output Format str( ) and repr( ) same representation but String and Floating point number a=0.24 str(a)‘0.24’ repr."

Similar presentations


Ads by Google