Articles

Program to find element at any index position in a tuple in python

Program to find element at any index position in a tuple in python


In the python programming article, we are going to learn

  • program to find element at any given index position of the tuple in python

Program to find element at any given index position of a tuple in Python

The program is as follows:

 

# Owner : TutorialsInhand Author : Devjeet Roy

names = ("Devjeet","Ajit","Bulbul","Shawriya")
idx = int(input("Enter the index: ").strip())

try:
    element = names[idx]
    print("The element at {} is {}".format(idx, element))
except:
    print("The max index position is",len(names)-1)

The output of the program is as follows:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter the index: 2
The element at 2 is Bulbul
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter the index: 7
The max index position is 3

Few important tips about the program

1. In the given program, we have used the concept of indexing to find the element present in a given location.

2. If the index position is greater than the max possible index, it will raise an exception. We can handle that using the try-except statements.

 

program to find element at any given index position in tuple in python

 


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :    Published Date : Oct 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!