[sf-lug] [PYCLASS] Still on exercise 2.1

Jeffrey Malone ieatlint at tehinterweb.com
Sun Oct 5 22:31:12 PDT 2008


Charles is correct, with one minor oversight.

%02d can be broken up as:

% -- Indicates this is a "conversion specifier" and the following
characters indicate what kind.  A conversion specifier simply
indicates that it should be replaced in the resulting string with
another variable.
0 -- This is an optional "flag" which indicates that the printed
variable should be padded with the character "0" to match the field
with specifier (by default, it will just use spaces)
2 -- This is the optional field width specifier, meaning that the
printed variable must be at least 2 characters long.
d -- This indicates that conversion will be a decimal/integer one
(there are many different specifiers)
There are other things you can put to modify how variables are printed
through conversion specifiers -- but an entire chapter could probably
be devoted to them.  In general, at some point the book should explain
the basics behind this.  Then, one can just go online to get a quick
reference on how to accomplish something.

The reason some are %d and some are %02d in my example is due to readability.
If there are 4 minutes and 5 seconds, and you use:
"%d:%d" % (4, 5)
You get:
"4:5"
Using the width specifier on seconds:
"%d:%2d" % (4, 5)
You get:
"4: 5"
Using the width specifier and the "0" pad-flag:
"%d:%02d" % (4, 5)
"4:05"
Which is a much more readable output for time.

Sadly, this gets more complicated if you want to print a % character.
Say you want to print "50%":
print "50%"
Will work fine.  Bus say you want to print a variable as a percentage:
x = 50
print "%d%" % x
Will result in a syntax error.
The reason is that if you have a simplistic print statement, with no
"% (variable[s])" following it, then it ignores the concept of
conversion specifiers in general.  This will work without error, for
instance:
print "%d"
And will print "%d" as-is.
The answer is that whenever you use conversion specifiers, and you
want to print a %, you use "%%".  (This is the same concept as the \
in strings, for those familiar with them, that \\ is needed to print a
single \)
x = 50
print "%d%%" % x
Will print "50%".

Hopefully that made some sense.


After writing this, I went through the book trying to find where it
spoke about this.  Impressively, I couldn't find anywhere where this
whole issue is addressed -- but the book does use conversion
specifiers at times in strings.
If this doesn't make sense, however -- don't worry!  None of this has
been covered, and unless you have a specific interest in all this --
you need not comprehend, use or fret about anything this entire email
says.

Jeffrey

On Sun, Oct 5, 2008 at 9:54 PM, Charles-Henri Gros <chgros at coverity.com> wrote:
> Christian Einfeldt wrote:
>> hi
>>
>> 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:
>>
>> %d:%02d
>>
>> 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
>
> --
> Charles-Henri
>
> _______________________________________________
> sf-lug mailing list
> sf-lug at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/sf-lug
>




More information about the sf-lug mailing list