Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Equality in Python
#1
Hello!

what is the actual difference between

=
and
==

I have some trouble understanding the actual meaning of theses 2 operators.

Could someone explain it in a simple way?
Reply
#2
Single "=" makes things equal. And "==" compares to see if they are equal.

one = 1
vs
if one == 1:
Reply
#3
= isn't equality, it's assignment - i.e. assigning a value to a variable. a = 1, for example, is a statement. It doesn't return a value; it just sets the value of a to 1.

== checks whether the values on the left hand and right hand sides are equal. 1 == 2, for example, is an expression. It does return a value (i.e. False in this case) and can be used in other expressions.
Reply
#4
An exemple with num%2==0

liist=[0,1,2,3,4,5,6,7,8,9,10]
summ=0

for num in liist:
    if num%2==0:
        print(f'that\'s a even number : {num}')
    else:
        print(f'That\'s an odd number : {num}')
        


here if num%2==0 why cant I use a single equal signe "="?
Reply
#5
(Feb-05-2020, 08:03 PM)el_bueno Wrote: here if num%2==0 why cant I use a single equal signe "="?

You already have two answers from two people to that exact question. The better question is "why do you think you should be able to use a single '='?" Why do you?
Reply
#6
Maybe, but I reaaly need to make sure I did understand the difference between the two, so when I'll code Iwont stumble and think why do we choose that over the other.
Reply
#7
Think of it this way, you are learning a foreign language and in this new language the word for equal is == and the word for assignment is = and that is it. Just as equals and assignment are not related in English neither are == and = in Python. They are two completely different concepts in both languages.
There is no passion to be found playing small - in settling for a life that is less than the one you are capable of living.
Reply
#8
Some facts, that not many people knows:

for i in range(100):
#   ^ assignment to name i
    pass
with open('some_file.txt') as fd:
#                             ^^ assignment to name fd
    pass
[x for x in range(100)]
#      ^ assignment to name x
def foo(a, b):
#   ^^^ assignment of the function to the name foo
#       ^ assignment to name a
#          ^ assignment to name b
    pass
class Foo:
#     ^^^ assignment of class to name Foo
    pass
What we call variables, are just names. We assign an object (lives somewhere in memory) to a name.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
Thank you so much, guys.

it really help me .

@jim2007 yes its a new language. I've learnt 4 languages and yes in the beginning we tend to think with the learnt language and try to apply its grammar to the new language, thats what I was doing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Checking for equality PythonGainz 8 3,149 Apr-10-2020, 03:00 PM
Last Post: PythonGainz
  Dictionnaries - Equality SupaFlamme 3 2,691 Jan-13-2019, 04:50 PM
Last Post: perfringo
  Problems with Equality of Numbers SheeppOSU 3 2,332 Jan-04-2019, 07:34 AM
Last Post: Gribouillis
  Shared reference and equality zyo 3 3,096 Jun-30-2018, 07:10 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020