Python id() Method with Examples

In the previous article, we have discussed Python hex() Method with Examples
id() Method in Python:

The id() function generates a unique id for the specified object.

In Python, each object has its own unique id.

When an object is created, it is given an id.

The id is the memory address of the object, and it will be different each time you run the program. (Except for objects with a fixed unique id, such as integers ranging from -5 to 256)

Note: The integer has a unique id. Throughout the lifetime, the integer id remains constant.

Syntax:

id(object)

Parameters

object: Any object, such as a string, number, list, or class.

Return Value:

The id() function returns the object’s identity. This is a unique integer for the given object that remains constant throughout its lifetime.

Examples:

Example1:

Input:

Given number = 9
Given string = "btechgeeks"
Given list = ["hello", 123, "this", "is", "btechgeeks"]

Output:

The id of the given number =  11094560
The id of the given string =  140697796571568
The id of the given list =  140697796536328

Example2:

Input:

Given number = 10.5
Given string = "good morning btechgeeks"
Given list =  [1, 3, 5, 2]

Output:

The id of the given number =  140002720203304
The id of the given string =  140002691658064
The id of the given list =  140002691627016

id() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Give the list as static input and store it in another variable.
  • Give the string as static input and store it in another variable.
  • Pass the given number as an argument to the id() function that generates a unique id for the given number.
  • Store it in another variable.
  • Pass the given string as an argument to the id() function that generates a unique id for the given string.
  • Store it in another variable.
  • Pass the given list as an argument to the id() function that generates a unique id for the given list.
  • Store it in another variable.
  • Print the id of the given number.
  • Print the id of the given string.
  • Print the id of the given list.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 9
# Give the list as static input and store it in another variable.
gvn_lst = ["hello", 123, "this", "is", "btechgeeks"]
# Give the string as static input and store it in another variable.
gvn_str = "btechgeeks"
# Pass the given number as an argument to the id() function that generates a
# unique id for the given number.
# Store it in another variable.
numbr_id = id(gvn_numbr)
# Pass the given string as an argument to the id() function that generates a
# unique id for the given string.
# Store it in another variable.
str_id = id(gvn_str)
# Pass the given list as an argument to the id() function that generates a
# unique id for the given list.
# Store it in another variable.
lst_id = id(gvn_lst)
# Print the id of the given number.
print("The id of the given number = ", numbr_id)
# Print the id of the given string.
print("The id of the given string = ", str_id)
# Print the id of the given list.
print("The id of the given list = ", lst_id)

Output:

The id of the given number =  11094560
The id of the given string =  139634246537648
The id of the given list =  139634246502408
Example of id() for a class:
class number:
    numb = 6


numb_id = number()
print('The id of given number =', id(numb_id))

Output:

The id of given number = 140125486982872

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the float(input()) function and store it in a variable.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the string as user input using the input() function and store it in another variable.
  • Pass the given number as an argument to the id() function that generates a unique id for the given number.
  • Store it in another variable.
  • Pass the given string as an argument to the id() function that generates a unique id for the given string.
  • Store it in another variable.
  • Pass the given list as an argument to the id() function that generates a unique id for the given list.
  • Store it in another variable.
  • Print the id of the given number.
  • Print the id of the given string.
  • Print the id of the given list.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using the float(input()) function and store it in a variable.
gvn_numbr = float(input("Enter some random number = "))
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the string as user input using the input() function and store it in another variable.
gvn_str = input("Enter some random string = ")
# Pass the given number as an argument to the id() function that generates a
# unique id for the given number.
# Store it in another variable.
numbr_id = id(gvn_numbr)
# Pass the given string as an argument to the id() function that generates a
# unique id for the given string.
# Store it in another variable.
str_id = id(gvn_str)
# Pass the given list as an argument to the id() function that generates a
# unique id for the given list.
# Store it in another variable.
lst_id = id(gvn_lst)
# Print the id of the given number.
print("The id of the given number = ", numbr_id)
# Print the id of the given string.
print("The id of the given string = ", str_id)
# Print the id of the given list.
print("The id of the given list = ", lst_id)

Output:

Enter some random number = 10.52
Enter some random List Elements separated by spaces = 12 45 6 9 1
Enter some random string = good morning btechgeeks
The id of the given number = 139861816847568
The id of the given string = 139861816051792
The id of the given list = 139861816050144

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.