What do the symbol "=" and "==" mean in python?

When should i use the symbol '==' and when only the symbol '=' is enough? What does the symbol '==' mean in python?

I started python coding 2 weeks ago and these two symbols confused me sometimes. Several times when i used '=', i would get an error message. After i changed it to '==', no error message any more. It seems that '==' can be used in any situation when '=' works. Is it true? Can any one of you explain the principle of using these two symbols?

Greatly appreciate your help!


Solution 1:

The simple answer is = is an assignment operator, == is a comparison operator. And you are wrong in saying that == can be used in any situation when = works. For example if I wanted to create the variable my_string and set it equal to "something" I would use the = operator.

my_string = "something"

I am assigning the variable to an object using that operator.

If I want to compare two strings (or other data types) like this:

if "something" == "nothing":
    #perform a function

I am comparing the two strings.

However if I try to assign a variable with the == operator for the initial assignment of the variable it will not work. If it is already assigned it will work but it will not do what you expect. For example:

my_string == "something"

Will raise an error a Name Error.

Likewise if you try to compare two strings (or other data types) with the = operator like this:

if "something" = "nothing":
    #perform a function

This will raise a Syntax Error.

I hope this helps you understand this concept. Happy programming!

Solution 2:

== is a comparison operator while = will assign a value to said variable.

You can use == to see whether any two items as long they are the same type are equivalent:

if a == 2: # Compares whether a is equal to 2
    print a

Now here's the thing. If you are comparing any two items like these, an error will pop up:

  • String with integer

  • Integer with string

  • String and float

  • Float and string

Floats and integers are comparable as they are numbers but are usually not equal to each other except when the float is basically the integer but with .0 added to the end. When using ==, if the two items are the same, it will return True. Otherwise, it will return False.

You can use = to assign values to variables. Using == will either do nothing or throw an error (if the variable is undefined). For example, you wanted the variable hi to have the value of 2. Then use the =:

hi = 2

Now hi is equal to 2. You can combine = with operations like + and - assuming the variable is an integer or float:

hi += 1
hi -= 1

Now by using += or -= like above, the variable must already be defined as these operators will directly change the value of the variable. Basically, they are like this:

hi += 1 # is the same as hi = hi + 1
hi -= 1 # is the same as hi = hi - 1

So in conclusion, they are different as:

  • == is a comparison operator: returns True is the two items are equal, returns False if not, throws error if used to assign variable before definition and if the two items are not compatible

  • = is an assignment operator: will assign values like strings or numbers to variables. Can be used in forms like += when variable's value is a number and is already defined.

The only way they can be used the same time is that they can be used in strings:

"hi = hello"
"2 == 3 probably returns False don't you think?" 

Solution 3:

= is assignment operator, it is used to assign something to a variable:

A = 67 #this is called assignment. With this, A has a value of 67

== is comparison operator, it is used to compare an item to another item to see if they are equal. It results in true or false

A == 65 #Is A equal to 65? This does NOT assign A to 65. If A was previously assigned as 67, it still retains its 67 value