

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Access front and rear element of Python tuple
When it is required to access the front and rear elements of a Python tuple, the access brackets can be used.
A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access.
Below is a demonstration of the same −
Example
my_tuple_1 = (87, 90, 31, 85,34, 56, 12, 5) print("The first tuple is :") print(my_tuple_1) my_result = (my_tuple_1[0], my_tuple_1[-1]) print("The front and rear elements of the tuple are : " ) print(my_result)
Output
The first tuple is : (87, 90, 31, 85, 34, 56, 12, 5) The front and rear elements of the tuple are : (87, 5)
Explanation
- A tuple is defined, and is displayed on the console.
- The first element and the last element of the tuple are accessed using indexing and negative indexing respectively.
- This is assigned to a value.
- It is displayed as output on the console.
- Related Questions & Answers
- Append at front and remove from rear in Python
- Python Front and rear range deletion in a list?
- Python – Concatenate Rear elements in Tuple List
- Rear element extraction from list of tuples records in Python
- C# Program to access tuple elements
- Maximum element in tuple list in Python
- Get tuple element data types in Python
- Sort tuple based on occurrence of first element in Python
- Python – Convert Rear column of a Multi-sized Matrix
- Update each element in tuple list in Python
- Python program to count occurrences of an element in a tuple
- Check if element is present in tuple of tuples in Python
- Python – Extract Kth element of every Nth tuple in List
- Python – Extract Rear K digits from Numbers
- Flatten tuple of List to tuple in Python
Advertisements