Comparator vs. arithmetic order of operations


#1

https://www.codecademy.com/courses/learn-python/lessons/conditionals--control-flow/exercises/else-problems-i-feel-bad-for-you-son?action=lesson_resume

Did you add a False after each else?

I expect the expression return 4**0 != 1 to return false but it seems not to do so. Does this mean that comparators are evaluated before arithmetic operations?



answer = "'Tis but a scratch!"

def black_knight():
    if answer == "'Tis but a scratch!":
        return True
    else:             
        return not True       # Make sure this returns False

def french_soldier():
    if answer == "Go away, or I shall taunt you a second time!":
        return True
    else:             
        return 4**0 != 1       # Make sure this returns False



#2

I think there may be something wrong about how you tested that.

$ python
>>> 4**0 != 1
False
>>>

#3

A Python Operator precedence table may be helpful.

The table linked above lists Python 3 operator precedence from lowest to highest precedence, so operations specified at the bottom are performed first, while ones at the top are last.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.