0
Does == mean not equal to?
im getting confused on the use of the double equals sign
3 odpowiedzi
+ 3
So,
x = 5
print(x)
will put 5 on the screen. You "assigned" 5 to x, and any place where you need a 5, you can now use x instead.
Putting the variables aside for a bit, to check for 2 things being equal, we would use "==". You use it mostly for if/else, yeah. It's a natural fit! For example, to check whether 10 is equal to 10:
if(10 == 10):
print("This happens")
else:
print("This doesn't happen")
Since 10 is in fact 10 (duh), we'll se the program jump into the if part of the if/else. Here:
if(11 == 10):
print("This doesn't happen")
else:
print("This happens")
we see the opposite. The condition fails, so we jump into the else block.
Now back to variables!
if(x == 5):
print("This happens")
because way up above we assigned 5 to x, and now we check whether that holds.
The behind the scenes if you will are booleans, maybe you've heard of them. Check this:
print(2 == 2)
is a correct program and will print "true".
print(2 == 3)
will print "false". These are the two boolean values we use to check for truthy-ness or falsy-ness inside if statements.
That is to say,
if(true):
print("This happens")
y = true
if(y):
print("This happens")
y = x == 5
if(y):
print("This happens")
will all work.
I hope that explains things!
+ 2
= is for assigning things to variables, like
x = 5
x now stores the value 5.
== is for equality checks, like
if(x == 5)
Its like that for most programming languages, by the way, not just python :)
0
can you explain what eqaulity checks are? is it the if else statment only?