Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Puzzling Result from if statement in Python
    Why does the first IF statement not raise a typeError? What is going on here?
    >>> alphas = "abcdefghijklmnopqrstuvwxyz"
    >>> for n in range(len(y)-1):
    	if (y[0] or y[-1]) in alphas:
    		print "false"
    
    >>> for n in range(len(y)-1):
    	if (y[-1] or y[0]) in alphas:
    		print "false"
    
    Does ( y[0] or y[1] ) yield TRUE. If so, why does asking ( (TRUE) in alphas ) not raise and error?
    cdag22 posted this question on 2/15/15 | python, logic
    Answers
  • +
  • 5
  • -
  • The 'or' operator in python does not return a boolean. It is equivalent to:
    def or(left, right):
        if left != False: return left
        if right != False: return right
        return false
    
    This can be demonstrated in the python interactive REPL like so:
    >>> 'a' or 'b'
    'a'
    >>> False or 'c'
    'c'
    >>> False or False
    False
    >>> False or ""
    ""
    
    Note that even though "" is interpretted as False when treated as a boolean (i.e. bool("") == False), it is still returned by or instead of False, hence the "!= False" in the definition of or above.
  • +
  • 2
  • -
  • y = [0,1,2,3,4]
    print ( y[0] or y[-1] ) #prints 4, not TRUE
    
    y = ['h','e','l','l','o']
    print ( y[0] or y[-1] ) #prints 'h', not TRUE
    
    I got an error when y[0] and y[-1] were integers, and no error when they where strings. <string> in <string> is valid and throws no error. I conclude either y[0] or y[-1] is a string in the above code snippet. You should have included it, if possible. To get TRUE or FALSE from a string or integer you would do:
    if ( not ( not ( y[0] or y[-1] ) ) ) in alphas:
    
    And then you'd get an error. Hope that helped! :-)
  • +
  • 0
  • -
  • Name Error. variable y has not been declared
  • +
  • 0
  • -
  • Correct me if i'm wrong, but aren't you matching the index of alphas, 0 towards -1 ? There should not be any errors. Your for loop if statement is saying "is a or z in alphas?" if true print "false". so you're basically saying if any of the one conditions i'm asking for i present print me a false result. there is no else. it just continues to the next.
  • +
  • 0
  • -
  • Actually, I was a little off. Apparently it's more like this:
    def or(left, right):
        if left:
            return left
        else:
            return right
    
  • +
  • -1
  • -
  • Are you asking why using a negative number as an index (y[-1]) does not return an error? It seems that Python handles a negative index by adding the length of the list to the negative index. So: y = [1, 2, 3, 4] print y[-2] #will return 3, as print y[-2] = print y[2] #reference: effbot.org/zone/python-list.htm
  • +
  • -1
  • -
  • Another possibility is that most times, an array is actually stored as an address in the memory. What is stored is the position where the array is stored. So let's say that the array is stored in "box" 1. What is stored is a hexadecimal value for access of the values of the array. So, if the "box" refers you to memory 100, then what the array, let's say
    y = [1, 2, 3, 4]
    If you ask for
    y[1]
    , then the compiler goes and adds that to the address given in the array storage (for our example, 100+1 = 101). If you ask for
    y[-1]
    , then it will probably take the value BEFORE the array is stored. It is probably a string, and an integer in a string makes no sense. Hope this helped!
    Log in to write an answer.