Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sections of Ch. 12, 13, 14 Prof. V. Norman PhD, MS, BS, DDS, Esq.

Similar presentations

Presentation on theme: "Sections of Ch. 12, 13, 14 Prof. V. Norman PhD, MS, BS, DDS, Esq."— Presentation transcript:

1 Sections of Ch. 12, 13, 14 Prof. V. Norman PhD, MS, BS, DDS, Esq.

2 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.

3 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’ ) )

4 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.

5 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)

6 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.

7 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

8 Tuple as a parameter # Assume we have a pixel type for which # you can set its color. def setColor(pixel, aTuple): pixel.setRed(aTuple[0]) pixel.setGreen(aTuple[1]) pixel.setBlue(aTuple[2]) setColor(myPixel, (30, 255, 192))

9 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.

10 Optional Parameter Example def setColor(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

11 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…

12 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()

13 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 = # entire file read in.

14 Format Operator Pattern: % 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

15 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.

16 Format examples print “girder id: %d, weight %10.5f” % (17, ) Produces : girder id: 17, weight print “Scribbler info: (%d, %d), angle %6.1f” % (x, y, angle) When called with (30, 20, -145) Scribbler info: (30, 20), angle

Download ppt "Sections of Ch. 12, 13, 14 Prof. V. Norman PhD, MS, BS, DDS, Esq."

Similar presentations

Ads by Google