Confused about logical operators (i.e. "comparison operators", or "boolean operators") in Python
Note: The attached image is a screenshot of page 31 of Dr. Charles Severance's book, Python for Everybody: Exploring Data Using Python 3 (2024-01-01 Revision).
I thought = was a mathematical operator, not a logical operator; why does Python use
>= instead of >==, or
<= instead of <==, or
!= instead of !==?
Thanks in advance for any clarification. I would have posted this in the help forums of FreeCodeCamp, but I wasn't sure if this question was too.......unspecified(?) for that domain.
It would be confusing and weird if "=" did different things depending on the context.
= is the assignment operator
== is the comparison operator.
the others using = only is probably just to keep things short, and the fact that the context is a lot clearer with another character like < next to the =
Some people in mathematics use := to assign functions, like f(x) := x^2; then when evaluating the function you use f(2) = 4, because it can be ser as a "true" comparison
>= and <= match the mathematical operators. The question you want to ask is why doesn't it use = for equality, and the answer is that = is already used for assignment (inherited from C among other languages).
In theory a language could use = for assignment and equality but it might be a bit confusing and error prone. Maybe not though. Someone try it and report back.
I've written code before in some hardware-specific languages before (I think it was for programming a stepper motor or something?) that used = for both assignment and comparison. If I recall correctly, the language was vaguely C-like, but assignment was not permitted in the context of a comparison. So something like if( a = (b+c) ) would not assign a value to a, it would just do the comparison.
Rust does an interesting thing in this regard. It does still have == for checking if two values are equal, but well, it actually doesn't have a traditional assignment operator. Instead, it has a unification operator, which programmers usually call "pattern matching".
It does still have a traditional assignment operator. You can assign values to mutable variables.
Also I would say let-binds are still pretty much assignment; they just support destructuring. Plenty of languages support that to some extent (JavaScript for example) and you wouldn't say they don't have assignment.
I don't think it affects the ability to overload = anyway. I think there aren't any situations in Rust where it would be ambiguous which one you meant. Certainly none of the examples you gave compile with both = and ==. Maybe there's some obscure case we haven't thought of.
I think what I'm most confused about is I cannot for the life of me seem to wrap my head around the difference between "assignment" and "equality". They seem exactly the same to me: when a variable is assigned a value, it's equal to that value now.
Even if I were write the program
x = 20
print(x*2)
it would still print 40. Because x is equal to 20. Because it was assigned the value of 20.
Hell, I've even heard Dr. Severance say "equal to" in this context in earlier videos multiple times.
They seem exactly the same to me: when a variable is assigned a value, it’s equal to that value now.
Yeah it's confusing because in maths they are the same and use the same symbol but they are 100% not the same in programming, yet they confusingly used the same symbol. In fact they even used the mathematical equality symbol (=) for the thing that is least like equality (i.e. assignment).
To be fair not all languages made that mistake. There are a fair few where assignment is like
x := 20
Or
x <- 20
which is probably the most logical option because it really conveys the "store 20 in x" meaning.
Anyway on to your actual question... They definitely aren't the same in programming. Probably the simplest way to think of it is that assignment is a command: make these things equal! and equality is a question: are these things equal?
So for example equality will never mutate it's arguments. x == y will never change x or y because you're just asking "are they equal?". The value of that equality expression is a bool (true or false) so you can do something like:
a = (x == y)
x == y asks if they are equal and becomes a bool with the answer, and then the = stores that answer inside a.
In contrast = always mutates something. You can do this:
a = 3
a = 4
print(a)
And it will print 4. If you do this:
a = 3
a == 4
print(a)
It will (if the language doesn't complain at you for this mistake) print 3 because the == doesn't actually change a.
My logic was always, if == is equal, then for >= we replace one of the equal signs to denote that it doesn't have only be equal but can be both.
But that was probably also influenced by languages where == means the value is equal and === means value and type have to be equal for the comparison to be true. If you compare "5" and 5 in those languages, == will be true and === will be false, since one is a string and one is a number.
At the end of the day, those signs are arbitrary conventions. People agree on them meaning something in a specific context, and the same thing can mean different things in different contexts. A in English represents a different sound than A in Spanish, and sometimes even in other dialects of English. Thinking of out like that helped me to keep the conventions of different programming languages apart.
Don't remember what language uses === to check if two objects have the same memory id assigned. Like a = 1, b= 1 would give true to a == b but false to a === b; while a = 1, b = a, would give true to both.
It's a convention set by early programming languages.
In most C-like languages, if (a = b)... is also a valid comparison. The = (assignment) operation returns the assigned value as a result, which is then converted to a boolean value by the if expression. Consider this Javascript code:
let a = b = 1
It first declares the b variable and assigns it the value of the expression 1, which is one.
It returns the result of the expression b = 1, which is the assigned value, which is 1.
It declares the a variable and assigns the previously returned value, which is 1.
Another example:
let a = 1
let b = 2
let c = 3
console.log(a == b) // prints "false" because the comparison is false
console.log(a = b) // prints 2 because the expression returns the value of the assignment, which is 'b', which is 2
// Using this in an 'if' statement:
if (b = c) { // the result of the assignment is 3, which is converted to a boolean true
console.log("what")
}
You can't do the same in Python (it will fail with a syntax error), but it's better to adhere to convention because it doesn't hurt anyone, but going against it might confuse programmers who have greater experience with another language. Like I was when I switched from Pascal (which uses = for comparison and := for assignment) to C.
It's still a mathematic operator. There's an entire field of math dedicated to comparisons, and it's featured heavily in most programming languages. Computer science is a field dedicated to the application of math
Pretty sure this is directly inspired by C so I would guess Guido van Rossum (the author of Python) just used what was already common back then. As in, = is assignment operator and == is equality/comparison operator.
It’s all convention coming from older programming languages, particularly C, which comes from programmers wanting shorthand for things like “BRANCH_EQUAL $1 $2 $3” which is shorthand for some binary code.
Python has changed the logical and and or operators to be and and or instead of && and ||.
So it makes sense to use >= as "is greater than or equal to"
You'd think = would be "is equal to", but it's already used for "set equal to" (i.e., assignment).
So what symbol do we use for "is equal to"? The symbol used in many programming languages is ==, so Python chose to follow that convention.
It's worth noting that there are other languages that use = as "is equal to", and use something else for assignment (like := for example). It just comes down to the history of the language and what conventions the original authors decided to use.
If I were to speculate, I'd say it came from the == operator (Boolean equality comparison) and then later, when that was extended to include Boolean less-than-or-equal and greater-than-or-equal, the decision was made to keep them 2 characters long. Either because it was visually cleaner, or just because programmers love being lazy (read: efficient)
@EveryMuffinIsNowEncrypted
Careful about "is" and "is not"
These compare identity of objects, which in CPython usually checks if objects are at the same memory address.
This works for some objects like True, False, None and small integer numbers, since they are created as unique objects when the interpreter starts.
Usually you would use == or != for these types of comparison. See below an example where "is" fails comparing two identical float numbers.