[sf-lug] [PYCLASS] Still on exercise 2.1
chgros at coverity.com
Sun Oct 5 21:54:31 PDT 2008
Christian Einfeldt wrote:
> On Sat, Oct 4, 2008 at 1:02 AM, Jeffrey Malone <ieatlint at tehinterweb.com
> <mailto:ieatlint at tehinterweb.com>> wrote:
> /* Set some constants */
> This was wonderfully enlighting to see this problem solved this way. It
> is elegant, because once the constants have been set, it is possible to
> throw many scenarios at those constants. Never in a million years would
> I have thought of this myself. This is excellent instruction, thanks.
> It would seem that it would probably be a good idea to create constants
> in almost every problem, is that correct?
> Also, it is surprising to me that one would need to define minutes. You
> would think that the Python programmers would anticipate that people are
> going to need those values and compute with them frequently.
> Also, it would seem that there would need to be a library or a config
> file that one could edit to permanently reflect set values for MIN and
> HOUR. Are there such files? Or, to put it another way, where are the
> values for MIN and HOUR stored? In /tmp? I am assuming those values go
> away when you exit python, is that correct?
> MIN = 60
> HOUR = MIN*60
> easy_pace = 8*MIN+15
> tempo_pace = 7*MIN+12
> start_time = 6*HOUR+52*MIN
> Coincidentally, in the law, we have to assign values to works as well in
> the course of written discovery, and there are constantly fights over
> those kinds of things in cases where discovery is hotly disputed, which
> is almost every large civil case.
> secs = easy_pace * 1 + tempo_pace * 3 + easy_pace * 1
> print "%d:%02d" % ( (secs/ MIN), (secs%MIN) )
> Since I am still on chapter 2 of the book, we have not covered this kind
> of syntax, which is taken from the print statement above:
> %d:%02d" % ( (secs/ MIN), (secs%MIN) )
> What does the colon denote above there, please? And is there a name for
> this structure:
> And what does the % mean, please? I am guessing we are going to cover
> that in Chapter 3, so if that is the case, my apologies. I wanted to
> get my questions out there while I had them in mind.
%d means: there will be a decimal value to print
%02d means: there will be a decimal value to print; print 2 digits (this
is all a bit too complicated to explain in detail in an e-mail)
The next '%' announces the arguments, i.e. the values referred to above
( a, b ) [where a is (secs/MIN) and b is (secs%MIN) specifies the values
The colon is just printed verbatim.
The (a,b) structure is called a "tuple", it represents multiple values.
For instance to print 3 integers you could use
print "integer 1:%d; integer 2:%d; integer 3:%d" % (11, 12, 13)
This will print:
integer 1:11; integer 2:12; integer 3:13
More information about the sf-lug