Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Comparison and Identity Operators in Python

Tech 1

Comparison operators are essentially the mathematical comparisons we learned in elementary school. They include ==, !=, >, <, >=, <=. These are used to compare numeric values.

Identity operators check whether two variables refer to the same memory address in Python. They determine if two variables originate from the same "home" by comparing the memory IDs returned by the id() function.

Below is a brief overview of comparison and identity operators.

Comparison Operators

  • ==: Checks equality. It works for numbers, strings, lists, tuples, dictionaries, etc.

Identity Operators

Identity operators compare the memory addresses of two variables. If the addresses match, the variables are considered identical (they belong together).

Memory storage refers to memory blocks (memory addresses). All comparisons above return a Boolean value.

# coding: utf-8

a = 1
b = 2.2
c = 0
d = 18
d_01 = 18
e = -3
f = 300
f_01 = 300

print(a == b)
print(a != b)
print(a < b)
print(a > e)
print(d >= b)
print(d >= d_01)

print(d == d_01)
print(d is d_01)
print('d id is:', id(d))
print('d_01 id is:', id(d_01))

print(f == f_01)
print(f is f_01)

print(f is d)
print(id(f))
print(id(d))

print(f is not d)

Output:

/Users/llq/PycharmProjects/pythonlearn/pythonlearn/.venv/bin/python /Users/llq/PycharmProjects/pythonlearn/pythonlearn1/compare.py 
False
True
True
True
True
True
True
True
d id is: 4388053808
d_01 id is: 4388053808
True
True
False
4378638288
4388053808
True

Process finished with exit code 0

Comparison and Identity Example

In the Python interpreter, when you enter Python in the terminal, it pre-defines memory addresses for integers from 0 to 255. Variables assigned values within this range directly reference these pre-alloacted memory addresses. However, for numbers above 255, like 300 in the example, the behavior differs.

Why do f and f_01, both set to 300 (above 255), return True for is in the script but not in the interactive interpreter?

In the script, when f is assigned 300, Python allocates a memory address for that vallue. When f_01 is later assigned the same value, Python reuses the existing memory address because the object already exists. This is why is returns True in the script. In the interactive interpreter, each assignment may create a new object, leading to different addresses.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.