550 likes | 1.58k Views
Input and Output in python. Josh DiCristo. How to output numbers. str() You can put any variable holding a number inside the parentheses and it will display the number. If you put a string inside the parentheses, it returns the original string. How to output numbers. repr()
E N D
Input and Output in python Josh DiCristo
How to output numbers • str() • You can put any variable holding a number inside the parentheses and it will display the number. • If you put a string inside the parentheses, it returns the original string
How to output numbers • repr() • Like the str() function, you can put any variable holding a number inside the parentheses and it will display the number. • >>> x = 10 * 3.25 • >>> y = 200 * 200 • >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) • >>>print s • The value of x is 32.5, and y is 40000...
How to output numbers (cont’d) • If a string is put into a repr() command, the resulting string will have quotation marks around it. • >>> s = 'Hello, world.' • >>>printrepr(s) • "'Hello, world.'"
Differences • str() is meant to be readable while repr() is meant to be unambiguous • Preciseness • >>>str(1.0/7.0) • '0.142857142857' • >>>repr(1.0/7.0) • '0.14285714285714285'
Justifying • To align text to the left, use ljust() • To align text to the right, use rjust() • To align text to the center, use center() • >>> for x in range(1, 11): • printrepr(x).rjust(2), repr(x*x).rjust(3), repr(x*x*x).rjust(4) 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
str.format() • Str.format() works like this: • >>>print ‘{} and {}'.format(‘green eggs’, ’ham') • green eggs and ham • You can use positional arguments: • >>>print '{1} and {0}'.format('spam', 'eggs') • eggs and spam • You can use keyword arguments: • >>>print ‘This {food} tastes {adjective}’.format(food=‘baba ganoush’, adjective=‘divine’) • This baba ganoush tastes divine • You can combine them
Other commands • zfill(n) adds n zeroes to the ends of numbers • !r can be used inside brackets to apply repr() to whatever string is going to output in place of the brackets. • >>>print 'The value of PI is approximately {!r}.'.format(math.pi) • The value of PI is approximately 3.141592653589793. • The same goes for !s and the str() command
Other Commands (cont’d) • : can denote how many place an integer is going to be carried out or how much space to put in a column • >>>print 'The value of PI is approximately {0:.3f}.'.format(math.pi) • The value of PI is approximately 3.142. • >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678} • >>>for name, phone in table.items(): • print '{0:10} ==> {1:10d}'.format(name, phone) • Jack ==> 4098 • Dcab ==> 7678 • Sjoerd ==> 4127
Other Commands (cont’d) • >>>table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} • >>>print ('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; 'Dcab: {0[Dcab]:d}'.format(table)) • Jack: 4098; Sjoerd: 4127; Dcab: 8637678 • You can use ** notation for tables • >>>print 'Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table) • You can use % instead of {} and . instead of : • >>>print 'The value of PI is approximately %5.3f.' % math.pi • The value of PI is approximately 3.142.
Opening and Closing files • open(filename, mode) • >>> f = open('/tmp/workfile', 'w') • mode is optional • w: writes or overwrites onto a new file • a: appends • r: reading (default) • r+: reading and writing • b: Binary mode (windows only) • wb, ab, r+b • f.close() closes the file
File Objects • file_object.read() returns the contents of the entire file • file_object.readline() returns one line at a time and includes newline characters at the end of the string unless it’s at the end of the file
Writing to files • file_object.write(‘text’) • Only strings may be written to files so any integers must first be converted using str() or repr()
Other Commands • file_object.seek(offset, from_what) • offset: the integer for how which byte you want to go to • from_what is optional • 0: from the beginning of the file (default) • 1: from the current position • 2: from the end • f.seek(4) goes to the 4th character • f.seek(-3, 2) goes to the 3rd to last character • f.read(1) returns the current character