GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Get the identity of an object - w3resource

Python: Get the identity of an object

Last update on May 17 2025 05:44:06 (UTC/GMT +8 hours)


Object Identity and Type

Write a Python program to get the Identity, Type, and Value of an object.

Sample Solution:

Python Code:

# Define a variable 'x' and assign the value 34 to it.
x = 34

# Print the identity of the variable 'x'.
print("\nIdentity: ", x)

# Print the data type of the variable 'x'.
print("\nType: ", type(x))

# Print the memory address (value) of the variable 'x'.
print("\nValue: ", id(x))

Sample Output:

Identity:  34

Type:  <class 'int'>

Value:  11066944

Check whether two objects are the same!

Python Code:

# Define two variables 'a' and 'b' with some values.
a = 34
b = 33
# Print the values of 'a' and 'b'.
print('a = ', a)
print('b = ', b)
# Define another variable 'c' and assign the value of 'a' to it.
c = a
# Compare the values of 'a' and 'b' for identity.
print("Compare a and b:")
print(a is b)
# Print the memory address of variable 'a'.
print("\nMemory address of a:")
print(id(a))
# Print the memory address of variable 'b'.
print("Memory address of b:")
print(id(b))
# Compare the memory addresses of 'a' and 'b'.
print("\nCompare the said memory address:")
print(id(a) == id(b))
# Compare the values of 'b' and 'c' for identity.
print("\nCompare b and c:")
print(b is c)
# Print the memory address of variable 'c'.
print("Memory address of c:")
print(id(c))

Sample Output:

a =  34
b =  33
Compare a and b:
False

Memory address of a:
140364203427088
Memory address of b:
140364203427056

Compare the said memory address:
False

Compare b and c:
False
Memory address of c:
140364203427088

For more Practice: Solve these Related Problems:

  • Write a Python program to check if two variables reference the same object in memory.
  • Write a Python program to find the type of all elements in a given list.
  • Write a Python program to determine if an object is an instance of a user-defined class.
  • Write a Python program to check whether two objects are of the same type without using `type()`.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to define a string containing special characters in various forms.
Next: Write a Python program to convert a byte string to a list of integers.

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.