I´m a 1 week self taught python3 guy, and i´m into the comparison and boolean operators and learn this:
True and True True
True and False False
False and True False
False and False False
So, i call
print(3 < 4) and (6 < 5)
True + False
So, why do i get True?
I have tried all other booleans trying to make some similar error, changing the way i enter this (<) sign, entering this line in python tutor and the result is the same, and makes me think 6 is minor then 5 some how, so i think i´m not looking at what should i look and it´s gonna be embarrassing to know the answer. Thanks.
Because you aren't printing
(3 < 4) and (6 < 5), you are printing3 < 4.First: You are calling the print function
When calling a function, you may recall that you use parenthesis to provide arguments to the function. For example:
Will print
Hello Worldbecause that is the argument provided to it.Similarly, when you invoke the
printfunction with3 < 4– this expression is in fact true.Second: You have a malformed boolean expression
When you say:
Python checks to see if both
aandbare true. So if you do:a = print(3 < 4)andb = (6 < 5)Third: You are wrong: the expression evaluates to
FalseYou said that the expression evaluates to
True. But that is not actually correct. The expression evaluates toFalsebecause (6 < 5)... but you only print out3 < 4.Fourth: You don't need parenthesis
Python was created to be a simple, clean, and readable language. By adding unnecessary symbols and tokens to the code, you obfuscate it. This leads to less readable code and can lead to many mistakes, such as yours.
Always avoid using parenthesis where not needed. You could have accomplished the same thing by writing:
However, you meant something totally different. You meant to write:*