Python: Get the identity of an object
Python Basic: Exercise-93 with Solution
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
Python Code Editor:
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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/python-basic-exercise-93.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics