User avatar
D.E.L.B.
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
Contact: Website

What's the difference between "=" and "=="?

Thu Jul 19, 2012 8:25 am

What is the difference between both of those equals in Python?

PaulCheffus
Posts: 381
Joined: Tue May 29, 2012 2:51 pm
Location: Lincolnshire UK

Re: What's the difference between "=" and "=="?

Thu Jul 19, 2012 8:42 am

D.E.L.B. wrote:What is the difference between both of those equals in Python?
Hi

I don't know Python but if its anything like C then = means assignment as in let x = 0 and == means comparision as in if x == 0 then do something.

Cheers

Paul
Procrastination - The Thief of Time.

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: What's the difference between "=" and "=="?

Thu Jul 19, 2012 8:43 am

I don't use Python however = is used to set a variable, == is when you're performing a true / false analysis of a variable.
Never check a variable with a single = it may cause a compiler / interpreter to error or to set the variable and pass true.

a = 100
if a == 0 then .. (will be false)
if a == 100 then ... (will be true)
if a = 50 then .. (may return true and a is set to 50)

Richard S.
Last edited by redhawk on Thu Jul 19, 2012 8:44 am, edited 1 time in total.

Yam
Posts: 68
Joined: Wed May 16, 2012 12:14 pm

Re: What's the difference between "=" and "=="?

Thu Jul 19, 2012 8:43 am

a = b*c
a gets assigned the value b*c

a == b*c
check to see is a is equal too b*c
(only returns true or false doesn't change any values)

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: What's the difference between "=" and "=="?

Thu Jul 19, 2012 9:56 am

Pretty much already been said, but there's a slight correction needs to be made to redman's post:

Code: Select all

a = 100
if a == 0 then .. (will be false)
if a == 100 then ... (will be true)
if a = 50 then .. (may return true and a is set to 50)
It's all good apart from that last line. In python, if you use the 'if' with a single '=', it'll just give you a syntax error message.
note: I may or may not know what I'm talking about...

User avatar
D.E.L.B.
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
Contact: Website

Re: What's the difference between "=" and "=="?

Thu Jul 19, 2012 12:55 pm

toxibunny wrote:Pretty much already been said, but there's a slight correction needs to be made to redman's post:

Code: Select all

a = 100
if a == 0 then .. (will be false)
if a == 100 then ... (will be true)
if a = 50 then .. (may return true and a is set to 50)
It's all good apart from that last line. In python, if you use the 'if' with a single '=', it'll just give you a syntax error message.
Could you give me an example of that 'if'?

Cheers to all of your help also.


obarthelemy
Posts: 1399
Joined: Tue Aug 09, 2011 10:53 pm

Re: What's the difference between "=" and "=="?

Thu Jul 19, 2012 4:28 pm

It just means that some variables are more equal than others ^^

karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

Re: What's the difference between "=" and "=="?

Thu Jul 19, 2012 8:47 pm

I remember it as:
A = B : A becomes B
A == B : does A equal B?

going further:
A != B : does A not equal B?
A <= B : is A less than or equal B?
etc...

Karl.

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: What's the difference between "=" and "=="?

Thu Jul 19, 2012 9:12 pm

D.E.L.B. wrote:
toxibunny wrote:Pretty much already been said, but there's a slight correction needs to be made to redman's post:

Code: Select all

a = 100
if a == 0 then .. (will be false)
if a == 100 then ... (will be true)
if a = 50 then .. (may return true and a is set to 50)
It's all good apart from that last line. In python, if you use the 'if' with a single '=', it'll just give you a syntax error message.
Could you give me an example of that 'if'?

Cheers to all of your help also.
type this into IDLE's interactive window (the one with the >>>)

x = 10

and then:

if x = 10: print ("woohoo!")

You'll get a syntax error. now try

if x == 10: print ("woohoo!")

woohoo! :)
note: I may or may not know what I'm talking about...

pygmy_giant
Posts: 1562
Joined: Sun Mar 04, 2012 12:49 am

Re: What's the difference between "=" and "=="?

Thu Jul 19, 2012 9:21 pm

in php you can have '==='.

The additional '=' means 'has same variable type' as integers can otherwise be deemed equivelent to a string resembling that integer.
Ostendo ignarus addo scientia.

JonB
Posts: 220
Joined: Tue Nov 29, 2011 9:26 pm

Re: What's the difference between "=" and "=="?

Thu Jul 19, 2012 9:27 pm

redhawk wrote:I don't use Python however = is used to set a variable, == is when you're performing a true / false analysis of a variable.
Never check a variable with a single = it may cause a compiler / interpreter to error or to set the variable and pass true.

a = 100
if a == 0 then .. (will be false)
if a == 100 then ... (will be true)
if a = 50 then .. (may return true and a is set to 50)

Richard S.
A minor point of style when using == to do comparisons is to put the value before the variable. This is considered good practice in languages like C or C++ that allow a single = in an if statement.

So, instead of saying:

if(a == 3)

say

if(3 == a)

The reason being that, at least for C, C++ and Java, if you accidentally enter it with a single equal, in the first case you get the result of a being set to 3 and the if statement always returning true, whereas in the second case you get a compilation error that alerts you to the typo.

User avatar
D.E.L.B.
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
Contact: Website

Re: What's the difference between "=" and "=="?

Sat Jul 21, 2012 7:09 pm

Thanks very much.

Why the brackets? This is for JonB or anyone able to answer that question.

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: What's the difference between "=" and "=="?

Sat Jul 21, 2012 7:40 pm

Just be grateful you ain't working with Prolog that language is damn right weird and sadistic. :lol:

The numbers are in brackets because that's the syntax for if statements the stuff inside the bracket are converted to true or false, I guess it makes the code look nice and tidy.
You can also perform multiple if statements e.g. if ((a == 0) and (b == 1)) then ... (I don't do Python so this code may or may not work).

Richard S.

User avatar
D.E.L.B.
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
Contact: Website

Re: What's the difference between "=" and "=="?

Sat Jul 21, 2012 11:26 pm

So what about this code?
http://dl.dropbox.com/u/30557697/Raspbe ... ounter.zip

When could somebody explain why the == is needed in some areas? If you could pinpoint these out then I'm sure I will really get it sunk into my mind when it is explained with this code, right now there are only number examples in the thread, but a lot of the things in that code are numbers and words :D. If someone could give it a try it would be super good!

Thanks!

P.S.: Thanks for everyone's help.

timhoffman
Posts: 85
Joined: Sat Nov 05, 2011 11:31 pm

Re: What's the difference between "=" and "=="?

Sun Jul 22, 2012 12:45 am

A minor point of style when using == to do comparisons is to put the value before the variable. This is considered good practice in languages like C or C++ that allow a single = in an if statement.

.[/quote]

Whilst this might be good practice with C/C++ its definitely not the general use pattern for python.
Python code you read will will almost always define the variable first not the value, and the order you use can be significant. When operator overloading the rhs of the expression is passed to the left hand side as an argument.

At some point it's worth having a look at python PEP8 style guide. http://www.python.org/dev/peps/pep-0008/

Cheers

PaulCheffus
Posts: 381
Joined: Tue May 29, 2012 2:51 pm
Location: Lincolnshire UK

Re: What's the difference between "=" and "=="?

Sun Jul 22, 2012 11:17 am

D.E.L.B. wrote:So what about this code?
http://dl.dropbox.com/u/30557697/Raspbe ... ounter.zip

When could somebody explain why the == is needed in some areas? If you could pinpoint these out then I'm sure I will really get it sunk into my mind when it is explained with this code, right now there are only number examples in the thread, but a lot of the things in that code are numbers and words :D. If someone could give it a try it would be super good!

Thanks!

P.S.: Thanks for everyone's help.
Hi

Everywhere you see the == in the code above it is doing a comparision (check whether the outcome is true or false). Its just the syntax of the language that it has to be ==.

For example the following two code snippets mean the same.
In VB

Code: Select all

if x = y then
    x = x + 1
endif 
In Python

Code: Select all

if x == y then
     x = x + 1
Cheers

Paul
Procrastination - The Thief of Time.

JonB
Posts: 220
Joined: Tue Nov 29, 2011 9:26 pm

Re: What's the difference between "=" and "=="?

Mon Jul 23, 2012 11:40 am

D.E.L.B. wrote:Thanks very much.

Why the brackets? This is for JonB or anyone able to answer that question.
Because in C / C++ / Java they are mandated in an if statement. Basically the language syntax dictates it.

As to Python... well, I've never used it. ;)

timhoffman
Posts: 85
Joined: Sat Nov 05, 2011 11:31 pm

Re: What's the difference between "=" and "=="?

Mon Jul 23, 2012 11:56 am

Python only needs the brackets if you need to control the order of operator precedence and/or to make the expression more readable.

Return to “Python”