views:

98

answers:

4

.. the difference between the = and the == signs in Python? i.e provide examples when each is used so there's no confusion between the two?

+2  A: 

= is used to assign variables ie number = 30 - the "number" variable now holds the number 30.

== is used as a boolean operator to check whether variables are equal to each other ie 1 == 1 would give true and 1 == 2 would return false

Tim
So == is only used in evaluating whether something is true or not and not for any other case?
Frens34
Nope - thats pretty much it.
Tim
A: 

= is assignment, you would use it to give a value to a variable.

str = "hello" assigns "hello" to str, so that if you were to get the value of str, it would be a hello.

== is equality comparison, you use it to compare two values.

if str == "hello": 
   print "equal"
else:
   print "not equal"

In that code you want to see if the value of str is equal to the string "hello", and if we assigned it as above, this would result in "equal" being printed.

birryree
+1  A: 

= is assignment, == is equality.

a = 5  # assigns the variable a to 5
a == 5 # returns true
a == 4 # returns false
a = 4 # a is now 4
a == 4 # returns true
sdolan
A: 

"==" is checking for equality. "=" is for assignment of values. eg v="100" Then to check whether v is 100, v==100

ghostdog74