Newbie that don't understand

Bengt Richter bokr at oz.net
Sat Jun 29 15:14:46 EDT 2002


On Sat, 29 Jun 2002 17:00:16 GMT, "Martin Ståhl" <martin.stahl at telia.com> wrote:

>I have never been programming before and now I am reading the
>"Non-Programmers Tutorial For Python"
>http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html#SECTION0091000
>0000000000000
>And don't understand the following
>I quote
>
>"To start off this chapter I am going to give you a example of what you
>could do but shouldn't (so don't type it in):
>
>a = 23
>b = -23
>
>if a < 0:
23 is not less than 0, so the indented statements(only one here) are (is) not executed,
and a remains bound to the value 23
>    a = -a
>
>if b < 0:
-23 _is_ less than 0, so the indented statement(s)(only one here) _is_ executed,
and b becomes bound to the value of the expression -b which is -(-23) which is positive 23
>    b = -b
>
>if a == b:
at this point a is bound to 23 and b is also bound to a number whose value is 23,
so the indented statement(s) below the if statement is(are) executed
and you see the output printed.
>    print "The absolute values of", a,"and",b,"are equal"

>else:
This block of indented statement(s) would only get executed if the preceding
if statement at the same indentation level were false (i.e., if a==b were
false in this example). But it was true, so this statement doesn't get executed.
>    print "The absolute values of a and b are different"
>
>with the output being:
>The absolute values of 23 and 23 are equal"
As you might expect now ;-)
>
>how can 23 and -23 be equal?
>
It's not saying that. The absolute values are the positive magnitudes of the numbers,
ignoring sign. It would be clearer to write

    a = abs(a)

instead of writing the equivalent code

    if a < 0:
        a = - a

But the result is the same, and that is why the printed message is
referring to "The absolute values."

HTH

Regards,
Bengt Richter



More information about the Python-list mailing list