﻿ Python: Get the identity of an object - w3resource

# Python: Get the identity of an object

## Python Basic: Exercise-93 with Solution

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(id(a))
# Print the memory address of variable 'b'.
print(id(b))
# Compare the memory addresses of 'a' and 'b'.
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(id(c))
```
```

Sample Output:

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

140364203427088
140364203427056

False

Compare b and c:
False
140364203427088
```

Python Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿