Newcomer struggling with tutorial

Discussion in 'Python' started by CPK Smithies, Oct 5, 2003.

  1. CPK Smithies

    CPK Smithies Guest

    Just joined this group, fascinated by Python. Many thanks to those who have
    worked so hard on it!

    One big problem, though: I was completely thrown by this, which I quote
    from the Tutorial:

    > Comparisons can be chained. For example, a < b == c tests whether a is

    less than b and moreover b equals c.

    I threw a mental wobbly and wasted two hours over this:

    a = -1
    b = 77
    c = 1

    (a < b)
    True

    True == c
    True

    (a < b) == c
    False

    Unfortunately I was brought up with the belief that if A == B and B == C,
    then A should == C.

    I confess that after two hours worrying about this I have given up on
    Python and uninstalled it. A shame: it looked so good!

    CPKS
     
    CPK Smithies, Oct 5, 2003
    #1
    1. Advertising

  2. CPK Smithies

    Peter Hansen Guest

    CPK Smithies wrote:
    >
    > I threw a mental wobbly and wasted two hours over this:
    >
    > a = -1
    > b = 77
    > c = 1
    >
    > (a < b)
    > True
    >
    > True == c
    > True
    >
    > (a < b) == c
    > False
    >
    > Unfortunately I was brought up with the belief that if A == B and B == C,
    > then A should == C.


    Strangely enough it does. Your last line is incorrect. Try executing
    it again, from scratch, without reassigning any of the three values
    you originally entered, because that's surely what you have done to
    get that result.

    Using Python 2.3:

    C:\>python
    Python 2.3.1 (#47, Sep 23 2003, 23:47:32) [MSC v.1200 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a = -1
    >>> b = 77
    >>> c = 1
    >>> a < b

    True
    >>> True

    True
    >>> True == c

    True
    >>> (a < b) == c

    True

    > I confess that after two hours worrying about this I have given up on
    > Python and uninstalled it. A shame: it looked so good!


    A shame indeed, were it in fact how Python worked.

    -Peter
     
    Peter Hansen, Oct 5, 2003
    #2
    1. Advertising

  3. > a = -1
    > b = 77
    > c = 1
    >
    > (a < b)
    > True
    >
    > True == c
    > True
    >
    > (a < b) == c
    > False
    >
    > Unfortunately I was brought up with the belief that if A == B and B == C,
    > then A should == C.
    >
    > I confess that after two hours worrying about this I have given up on
    > Python and uninstalled it. A shame: it looked so good!


    well isnt that the defeatist way to go.

    sounds like you're trying to compare two values that are not necessarily
    the same. You're thinking in boolean logic, and trying to express it in
    a totally different way.

    You wanted :

    a = 1
    b = 2
    c = 1
    print ((a<2) & c)

    So we are saying we want to have BOTH (a<2) AND (c) to be true.

    Dave
     
    Dave Harrison, Oct 5, 2003
    #3
  4. On Sun, Oct 05, 2003 at 03:17:31AM +0000, CPK Smithies wrote:
    > Just joined this group, fascinated by Python. Many thanks to those who have
    > worked so hard on it!
    >
    > One big problem, though: I was completely thrown by this, which I quote
    > from the Tutorial:
    >
    > > Comparisons can be chained. For example, a < b == c tests whether a is

    > less than b and moreover b equals c.
    >
    > I threw a mental wobbly and wasted two hours over this:
    >
    > a = -1
    > b = 77
    > c = 1
    >
    > (a < b)
    > True
    >
    > True == c
    > True
    >
    > (a < b) == c
    > False


    Did you type this from memory, rather than copying and pasting? This is
    what I see:

    >>> a = -1
    >>> b = 77
    >>> c = 1
    >>> (a < b)

    True
    >>> True == c

    True
    >>> (a < b) == c

    True
    >>> a < b == c

    False
    >>> (a < b) and (b == c)

    False


    Do you see the distinction? "a < b == c" is the same as writing "(a < b)
    and (b == c)" in python, but "(a < b) == c" isn't. Perhaps the tutorial's
    wording is unclear.

    > I confess that after two hours worrying about this I have given up on
    > Python and uninstalled it. A shame: it looked so good!


    Then why are you posting here? ;)

    -Andrew.
     
    Andrew Bennetts, Oct 5, 2003
    #4
  5. CPK Smithies

    David Lees Guest

    CPK Smithies wrote:
    > Just joined this group, fascinated by Python. Many thanks to those who have
    > worked so hard on it!
    >
    > One big problem, though: I was completely thrown by this, which I quote
    > from the Tutorial:
    >
    >
    >>Comparisons can be chained. For example, a < b == c tests whether a is

    >
    > less than b and moreover b equals c.
    >
    > I threw a mental wobbly and wasted two hours over this:
    >
    > a = -1
    > b = 77
    > c = 1
    >
    > (a < b)
    > True
    >
    > True == c
    > True
    >
    > (a < b) == c
    > False
    >
    > Unfortunately I was brought up with the belief that if A == B and B == C,
    > then A should == C.
    >
    > I confess that after two hours worrying about this I have given up on
    > Python and uninstalled it. A shame: it looked so good!
    >
    > CPKS

    I have not read the tutorial, but the section you quote is quite clear
    about what chaining means. I did not know about this, but it is kind of
    neat. For example:

    >>> a = 1; b = 2; c = 3; d = 4
    >>> a < b < c < d < 5

    True
    >>> a < b < c < d < 3

    False

    Python actually conforms more to standard math notation for transitivity
    than whatever language you were 'brought up' on, which results in
    confusion when you are used to standard algebraic notation. I still
    recall 39 years ago being quite puzzled when I saw the statement:
    LET A = A+1

    Good luck with whatever language you choose. Personally, I have found
    Python to be a wonderful practical tool for the occasional programmer
    like me. And this group is a nice community for the isolated user
    because someone usually supplies answers quickly and non-judgementally
    (for the most part).

    david
     
    David Lees, Oct 5, 2003
    #5
  6. CPK Smithies

    Terry Reedy Guest

    "CPK Smithies" <> wrote in message
    news:blo2gb$ng0$...
    > from the Tutorial:
    >
    > > Comparisons can be chained. For example, a < b == c tests whether

    a is
    > > less than b and moreover b equals c.


    This means that a < b == c is equivalent to
    (a < b) and (b == c) and not
    (a < b) == c., which is something else.

    Chained operators are *not* 'associative' -- you cannot insert parens
    unless you so for each overlapping pair of args and insert 'and'
    between each.

    > I threw a mental wobbly and wasted two hours over this:
    >
    > a = -1
    > b = 77
    > c = 1
    >
    > (a < b)
    > True
    >
    > True == c
    > True


    For a < b == c, the second test Python makes is b==c (False).
    But this matches for your next test.

    > (a < b) == c
    > False


    For 2.2.1 (which does not have True/False on Windows .
    >>> a,b,c = -1, 77, 1
    >>> a < b

    1
    >>> (a < b) == 1

    1

    What version/machine were you running? Please cut and paste actual
    output. If you have really discovered a bug in current Python, we
    would want to know and fix it.

    > Unfortunately I was brought up with the belief that if A == B and B

    == C,
    > then A should == C.


    Why 'unfortunately'? For builtin objects, this is true in Python.

    > I confess that after two hours worrying about this I have given up

    on
    > Python and uninstalled it. A shame: it looked so good!


    If you really did that, it was a real shame.

    Terry J. Reedy
     
    Terry Reedy, Oct 5, 2003
    #6
  7. Like others have pointed out, Python does that chained comparison right.


    > I confess that after two hours worrying about this I have given up on
    > Python and uninstalled it. A shame: it looked so good!
    >
    > CPKS


    That seems to be a flame bait to me. ;)
    Perhaps not a good one because people didn't bite. Or we are just too
    Pythonic (and squeeze instead) :)

    Cheers,
    Pm
     
    Jegenye 2001 Bt, Oct 5, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alto

    SOAP server (newcomer q)

    Alto, Dec 15, 2005, in forum: Java
    Replies:
    4
    Views:
    439
  2. Tim Peters

    RE: Newcomer struggling with tutorial

    Tim Peters, Oct 5, 2003, in forum: Python
    Replies:
    9
    Views:
    328
    Gerrit Holl
    Oct 6, 2003
  3. Shankar Iyer ()

    a question from a newcomer to this language

    Shankar Iyer (), Jun 10, 2005, in forum: Python
    Replies:
    0
    Views:
    250
    Shankar Iyer ()
    Jun 10, 2005
  4. Florian Daniel Otel

    Newcomer question wrt variable scope/namespaces

    Florian Daniel Otel, Jan 13, 2006, in forum: Python
    Replies:
    8
    Views:
    310
    Florian Daniel Otel
    Jan 18, 2006
  5. Alan Cameron

    Newcomer to Python tutorial question

    Alan Cameron, May 7, 2009, in forum: Python
    Replies:
    12
    Views:
    416
    Steven D'Aprano
    May 9, 2009
Loading...

Share This Page