+ 1

Boolean Logic, not operator

Hello, I just recently started learning Python and right now I am on the Boolean Logic lesson in Control Structures of the 'Python Core' course. Does anyone else understand the not operator. It's confusing because 'and' and 'or' are straight forward but the 'not' operator is inverted so it's a bit confusing to me.

7th Feb 2024, 1:41 AM
Nicole Krason
11 Respuestas
+ 2
Wong Hei Ming there are a lot of inaccuracies in your answer. Please fix them. Multiple times you have used not age == 24 While this gives the correct answer (False), it does not do what you think it does. Since the precedence of 'not' is higher than that of (==), the statement will be read as (not age) == 24 Since age is a 'truthy' value (integers are truthy if they are not equal to 0), 'not age' yields False. So it boils down to False == 24 which is obviously, False The better and more obvious thing to do would be age != 24 Also you have used the 'is' operator to compare strings. Again while this will yield the correct result in the code you have shown, it is not the correct usage of 'is' 'is' keyword is used to check if two objects are 'same', not 'equal'. For example, x = [1,2,3] y = [1,2.3] while x is equal to y, it is not the 'same object' as y, so (x is y) yields False. See this answer for why it works in your example https://stackoverflow.com/a/50037786 You should always use != operator instead
7th Feb 2024, 7:58 AM
XXX
XXX - avatar
+ 2
Thank you to everyone response! I'm definitely going to practice this more
7th Feb 2024, 12:13 PM
Nicole Krason
+ 1
Assume you are a girl and age 24. Nicole_Krason = "girl" age = 24 print(age == 24) # True print(not age == 24) # False print(Nicole_Krason == "girl") # True print(Nicole_Krason is not "girl") # False print(Nicole_Krason == "boy") # False print(Nicole_Krason is not "boy") # True print(Nicole_Karson == "girl" and age == 24) # True print(Nicole_Karson == "girl" and not age == 24) # False print(Nicole_Karson is not "girl" and age == 24) # False print(Nicole_Karson == "boy" and age == 24) # False print(Nicole_Karson == "girl" or age == 60) # True print(Nicole_Karson == "girl" or not age == 24) # True print(Nicole_Karson is not "girl" or not age == 24) # False print(Nicole_Karson == "boy" or not age == 24) # False You are right, "not" operator is used to reverse the evaluation. If something is evaluated as True, turn it into False. It is a bit confusing because it is not intuitive as we used to speak. PS. I think the course didn't mention to use "is not" when comparing string.
7th Feb 2024, 4:50 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Wong Hei Ming , is is not aren't logical operators but identity comparison operators. They take two operands and compare the id() values of the objects they refer to, so they shouldn't be used with literals (it raises a non-fatal SyntaxWarning). https://docs.python.org/3/reference/expressions.html#is-not https://docs.python.org/3/library/exceptions.html#SyntaxWarning
7th Feb 2024, 8:02 AM
Rain
Rain - avatar
+ 1
XXX Thanks for pointing out my error. Maybe I should write as >>> not (age == 24), so operator 'not' flips the result and makes it more readable. However, I used 'is' on purpose, using a not so precise syntax / wording to explain a concept. Yes, I know some people won't agree with this approach, but OP said she recently started learning Python and feel confusing about 'not' operator. She feels confused because the syntax is different from our daily speaking language. And the example you provided about comparing two lists not only not answering the question, but adding confusion to a beginner.
7th Feb 2024, 9:16 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Wong Hei Ming My example was for removing the confusion around the 'is' keyword, and not as an answer to the question. I understand that 'is not' might sound closer to English, but I think it only adds confusion because it suggests that the 'is' operator can be used for comparison of strings (your last line seems to support that as well). Also, the 'not' in 'is not' does not mean the same as 'not' used as a unary operator elsewhere. So in my opinion, you should change the use of 'is' to equality operator, or atleast add a note that it should not be used like that, because 'is' is already misunderstood by many beginners to be the same as ==
7th Feb 2024, 2:13 PM
XXX
XXX - avatar
0
Nicole Krason , not only takes one operand. It flips, or inverts the truth value of its operand. print(not True) # False print(not False) # True https://docs.python.org/3/reference/expressions.html#boolean-operations
7th Feb 2024, 7:52 AM
Rain
Rain - avatar
0
not gives the opposite meaning and reverses whether a value is true or false in booleans
7th Feb 2024, 10:47 AM
Fateme Habibi
0
XXX Yes, using == can be a confusing if comparing a mutable object such as list for beginners. But by the time when a student learning list, mutable and immutable will be discussed, at least for most of my reading from introduction books, and it is where 'is' come in.
7th Feb 2024, 2:45 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming I hate to pry on this, but I'm not sure how mutability of objects is related to the (==) or the 'is' operator? I think both operators work the same for mutable or immutable objects?
8th Feb 2024, 5:20 AM
XXX
XXX - avatar
0
XXX Thank you!
8th Feb 2024, 5:24 AM
Average American
Average American - avatar