160 likes | 239 Vues
Sections of C h. 12, 13, 14. Prof. V. Norman PhD, MS, BS, DDS, Esq. Tuples. Q: I don’t understand the difference between a tuple and a list. Can you explain it?
E N D
Sections of Ch. 12, 13, 14 Prof. V. Norman PhD, MS, BS, DDS, Esq.
Tuples Q: I don’t understand the difference between a tuple and a list. Can you explain it? A: A tuple is essentially an immutable list. (Like a string, which is an immutable list of characters.) Once the tuple has been created, it can’t be changed.
Tuple creation Q: Is any bunch of values separated by commas a tuple? The book said they're usually inside parentheses, but that the parentheses are unnecessary; so why would people put the parentheses in? A: The comma operator is really the tuple creation operator, but sometimes it is necessary to use the parentheses. E.g., to pass a tuple literal to a function: s = function( ( ‘a’, ‘tuple’, ‘of’, ‘strings’ ) )
What good are tuples? Q: What good are tuples? Why use them? A: Tuples are not used that much… Some things seem natural as a tuple, like an (x, y) coordinate, or an (r, g, b) color. (Tuples have to be used when using dictionary keys, as keys have to be immutable.) Note: lists are more versatile, because they can be modified.
Tuple example Suppose x and y coordinates of a Scribbler need to be stored in a variable. You could store them as a tuple. Then, you can do a “transformation” on the point, by multiplying it by a factor: def transform(point, factor): return (point[0] * factor, point[1] * factor) orig_coord = (30, 50) # create tuple new_coord = transform(orig_coord, 0.5)
More on Tuples Q: Is there any reason to create single element tuples? A: IMO, no. Q: Doesn’t the last example of 12.1 modify a tuple? A: No: it creates a new tuple and then makes t refer to it. Q: Can a tuple only hold single-character strings? A: No, they can hold anything of any type, like a list can.
Tuple Assignment Q: Can you give more tuple assignment examples? A: Sure… coords = (3, 4) # a tuple with 2 values x, y = coords # x and y created and given # values from coords r, g, b = getColor(pixel) # getColor returns tuple color = (r, g, b) # put them back in a tuple
Tuple as a parameter # Assume we have a pixel type for which # you can set its color. defsetColor(pixel, aTuple): pixel.setRed(aTuple[0]) pixel.setGreen(aTuple[1]) pixel.setBlue(aTuple[2]) setColor(myPixel, (30, 255, 192))
Optional Parameters Terminology • Required Parameter: a parameter for which a value must be supplied by the caller. • Optional Parameter: a parameter in a function’s parameter list that the caller does not have to supply a value for. • Default value: the value the code provides for the optional parameter in the function definition. It is used when the caller does not supply a value.
Optional Parameter Example defsetColor(pixel, r=0, g=0, b=0): pixel.setColor( (r, g, b) ) setColor(pixel, 255) # full red only setColor(pixel) # (0, 0, 0) is black setColor(pixel, 0, 255, 0) # green setColor(pixel, 255, 255, 255) # white
File operations Q: Why when one opens a file using the write method does all the data in that file get deleted? A: If you open a file for writing, the file contents are wiped out. If you open a file for reading, nothing happens, and you can’t write to it. You can open a file for writing and appending, in which case you don’t wipe out the contents. Prof. Norman to provide examples here…
More useful file operations • Can read entire file line by line: f = open(“aFile.txt”) # read-mode for line in f: # do stuff with this line # etc f.close()
More useful file operations Or: f = open(“aFile.txt”) lines = f.readlines() # lines is a list of lines from the file. Or: f = open(“aFIle.txt”) contents = f.read() # entire file read in.
Format Operator • Pattern:<format-string> % <value or tuple of values> • Format string contains format sequences: • %d : format number as an integer • %f or %g: format number as a float • %s : format number as a string • %x : format number as an integer in hexadecimal notation
Format operator • Format sequence can contain modifiers to describe exactly how to print something out: • %7.3f : format float as using 7 columns, with 3 behind the decimal point • %-8s : format string as using 8 columns, left justified • Without this, there is no way to print out stuff exactly as you want it.
Format examples print “girder id: %d, weight %10.5f” % (17, 1056.789203) Produces: girder id: 17, weight 1056.78920 print “Scribbler info: (%d, %d), angle %6.1f” % (x, y, angle) When called with (30, 20, -145) Scribbler info: (30, 20), angle -145.0