Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is and '==' i'm confused
#2
== when comparing values and is when comparing identities(which is object's memory address).
Can use id() to see this.
>>> a = "this is test string"
>>> b = "this is test string"
>>> id(a)
2777033042000
>>> id(b)
2777033041840
>>> a is b
False

>>> help(id)
Help on built-in function id in module builtins:

id(obj, /)
    Return the identity of an object.
    
    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)
So there is underlaying stuff going on eg -5 to 256 most used numbers has a optimization to cache these in same place in memory.
>>> c = 50
>>> d = 50
>>> c is d
True
>>> c = 257
>>> d = 257
>>> c is d
False
jefsummers likes this post
Reply


Messages In This Thread
is and '==' i'm confused - by hshivaraj - Sep-12-2021, 02:58 PM
RE: is and '==' i'm confused - by snippsat - Sep-12-2021, 04:50 PM
RE: is and '==' i'm confused - by Notabene - Sep-15-2021, 08:05 AM
RE: is and '==' i'm confused - by deanhystad - Sep-12-2021, 09:22 PM
RE: is and '==' i'm confused - by naughtyCat - Sep-13-2021, 08:47 AM
RE: is and '==' i'm confused - by deanhystad - Sep-15-2021, 08:46 AM
RE: is and '==' i'm confused - by snippsat - Sep-15-2021, 09:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 1,171 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 1,017 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Pandas confused DPaul 6 2,658 Sep-19-2021, 06:45 AM
Last Post: DPaul
  Confused with 'flags' tester_V 10 5,039 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 2,262 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 3,086 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 2,545 Feb-10-2020, 09:15 AM
Last Post: snippsat
  Dazed and confused... RodNintendeaux 10 7,668 May-28-2017, 01:32 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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