In the previous article, we have discussed Python Program for memoryview() Function
The tuple index function in Python returns the index of a given tuple item. The index position of the first found value is returned by the tuple index function.
Syntax:
TupleName.index(tupleValue, start, end)
If you specify a start value, the index function will begin searching from that point. Similarly, setting the end position causes the tuple index function to stop looking at that number.
Examples:
Example1:
Input:
Given tuple = (10, 20, 30, 50, 70, 20) Given first number = 50 Given second number = 20 Given start position = 2
Output:
The given number's 50 index position = 3 The given number's 20 index position after the given start position 2 = 5
Example2:
Input:
Given tuple = (1, 3, 5, 7, 8, 2, 5) Given first number = 3 Given second number = 5 Given start position = 3
Output:
The given number's 3 index position = 1 The given number's 5 index position after the given start position 3 = 6
Program to Find Index of a Tuple Item in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the tuple as static input and store it in a variable.
- Give the number as static input and store it in another variable.
- Get the index position of the given number using the index() method and store it in another variable.
- Print the index position of the given number.
- Give the second number as static input and store it in another variable.
- Give the start position as static input and store it in another variable.
- Pass the given second number, start position as arguments to the index() method to get the index value of the given second number after the given start position.
- Store it in another variable.
- Print the index position of the given second number after the given start position.
- The Exit of Program.
Below is the implementation:
# Give the tuple as static input and store it in a variable. gvn_tupl = (10, 20, 30, 50, 70, 20) # Give the number as static input and store it in another variable. gvn_numb1 = 50 # Get the index position of the given number using the index() method and # store it in another variable. indx_1 = gvn_tupl.index(gvn_numb1) # Print the index position of the given number. print("The given number's", gvn_numb1, "index position = ", indx_1) # Give the second number as static input and store it in another variable. gvn_numb2 = 20 # Give the start position as static input and store it in another variable. gvn_strtpositn = 2 # Pass the given second number, start position as arguments to the index() # method to get the index value of the given second number after the given # start position. # Store it in another variable. indx_2 = gvn_tupl.index(gvn_numb2, gvn_strtpositn) # Print the index position of the given second number after the given # start position. print("The given number's", gvn_numb2, "index position after the given start position", gvn_strtpositn, "= ", indx_2)
Output:
The given number's 50 index position = 3 The given number's 20 index position after the given start position 2 = 5
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the tuple user input using list(),map(),input(),and split() functions and store it in a variable.
- Give the number as user input using the int(input()) function and store it in another variable.
- Get the index position of the given number using the index() method and store it in another variable.
- Print the index position of the given number.
- Give the second number as user input using the int(input()) function and store it in another variable.
- Give the start position as user input using the int(input()) function and store it in another variable.
- Pass the given second number, start position as arguments to the index() method to get the index value of the given second number after the given start position.
- Store it in another variable.
- Print the index position of the given second number after the given start position.
- The Exit of Program.
Below is the implementation:
# Give the tuple user input using list(),map(),input(),and split() functions and store it in a variable. gvn_tupl = tuple(map(int, input( 'Enter some random Tuple Elements separated by spaces = ').split())) # Give the number as user input using the int(input()) function and store it in another variable. gvn_numb1 = int(input("Enter some random number = ")) # Get the index position of the given number using the index() method and # store it in another variable. indx_1 = gvn_tupl.index(gvn_numb1) # Print the index position of the given number. print("The given number's", gvn_numb1, "index position = ", indx_1) # Give the second number as user input using the int(input()) function and # store it in another variable. gvn_numb2 = int(input("Enter some random number = ")) # Give the start position as user input using the int(input()) function and # store it in another variable. gvn_strtpositn = int(input("Enter some random number = ")) # Pass the given second number, start position as arguments to the index() # method to get the index value of the given second number after the given # start position. # Store it in another variable. indx_2 = gvn_tupl.index(gvn_numb2, gvn_strtpositn) # Print the index position of the given second number after the given # start position. print("The given number's", gvn_numb2, "index position after the given start position", gvn_strtpositn, "= ", indx_2)
Output:
Enter some random Tuple Elements separated by spaces = 1 3 5 7 8 2 5 Enter some random number = 3 The given number's 3 index position = 1 Enter some random number = 5 Enter some random number = 3 The given number's 5 index position after the given start position 3 = 6
Program to Find Index of a Tuple Item in Python without using index() Function
Below is the implementation:
gvn_tupl = (10, 40, 50, 30, 60, 40, 20, 40, 30) print(gvn_tupl) gvn_valu = 40 for itr in range(len(gvn_tupl)): if gvn_tupl[itr] == gvn_valu: print("The given number", gvn_valu, "index position = ", itr) break print("The all Index Positions of given number:") for k in range(len(gvn_tupl)): if gvn_tupl[k] == gvn_valu: print("Index Position of 20 = ", k) print("The all the Index Positions of given number using enumerate function:") for itor, p in enumerate(gvn_tupl): if p == gvn_valu: print("Index Position of 20 = ", itor)
Output:
(10, 40, 50, 30, 60, 40, 20, 40, 30) The given number 40 index position = 1 The all Index Positions of given number: Index Position of 20 = 1 Index Position of 20 = 5 Index Position of 20 = 7 The all the Index Positions of given number using enumerate function: Index Position of 20 = 1 Index Position of 20 = 5 Index Position of 20 = 7
Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.