A tuple in python is:
- an ordered sequence, iterable, immutable
- heterogeneous collection of Python objects
- generally defined using parenthesis ()
- elements separated by comma inside the parenthesis.
Tuple allows:
- indexing
- repetition.
- Nesting.
They can store values of different types.
Fig: Accessing Elements from a tuple |
Create a tuple without using round brackets
>>>my_tuple = 0,1,2,3,4,5
>>>print(type(my_tuple))
type: <class 'tuple'>
Create a tuple of numbers
>>>my_tuple = (1,2,3,4,5,6)
>>>print(my_tuple)
(1,2,3,4,5,6)
Creating tuple of mixed data types:number, string, list
>>>my_tuple = (12, "Binary", [21,"Study",23])
>>>print(my_tuple)
(12, "Binary", [21,"Study",23])
Creating a nested tuple
>>>my_tuple = ((1,2,3), ("binary", "Study"))
Access Elements from a Tuple
Python provides us various mechanism to select a single or a range of elements. We can use indexing, reverse indexing and slicing to access elements from a tuple in Python. Let's discuss the these three ways in detail:
1. Accessing Tuple Elements Using Indexing
Simplest is direct access where we use index operator [ ] to pick an item from tuple; the index is always an integer.
>>>my_tuple = ('a','e','i','o','u')
>>>print("The tuple:", my_tuple, "Length:", len(my_tuple))
# output
The tuple: ('a', 'e', 'i', 'o', 'u') Length: 5
Indexing the first element
>>>print("my_tuple[0]:", my_tuple[0])
my_tuple[0: a
Indexing the last element
>>>print("my_tuple[length-1]:", my_tuple[len(my_tuple) - 1])
my_tuple[length-1]: u
Accessing a non-existent member will raise Error
try:
print(my_tuple[len(my_tuple)+1])
except Exception as ex:
print("my_tuple[length+1] Error:", ex)
# output
my_tuple[length+1] Error: tuple index out of range
Indexing a non-integer index will raise TypeErr
try:
print(my_tuple[0.0])
except Exception as ex:
print("my_tuple[0.0] Error:", ex)
# output
my_tuple[0.0] Error: tuple indices must be integers or slices, not float
Indexing in a tuple of tuples
>>>t_o_t = (('jan', 'feb', 'mar'), ('sun', 'mon', 'wed'))
Accessing elements from the first sub tuple
>>>print("t_o_t[0][2]:", t_o_t[0][2])
# output
t_o_t[0][2]: mar
Accessing elements from the second sub tuple
>>>print("t_o_t[1][2]:", t_o_t[1][2])
# output
t_o_t[1][2]: wed
2. Accessing Tuple Elements Using Reverse Indexing
Python tuple supports reverse indexing, i.e., accessing elements using (-ve) index values.
Index -1 represents last item.
An index -2 will refer to the second item from the end.
>>> my_tuple = ('a','e','i','o','u')
>>> my_tuple
('a', 'e', 'i', 'o', 'u')
>>> my_tuple[-1]
'u'
>>> my_tuple[-5]
'a'
>>> my_tuple[-6]
IndexError: tuple index out of range
3. Accessing Tuple Elements Using Slicing Operator
>>> days = ('mon', 'tue', 'wed' ,'thu', 'fri', 'sat', 'sun')
Accessing elements leaving the first one
>>> days [1:]
('tue', 'wed', 'thu', 'fri', 'sat', 'sun')
Accessing elements between first & fifth positions excluding ones at ist & fifth position
>>> days [1:5]
('tue', 'wed', 'thu', 'fri')
Accessing elements after the fifth position
>>> days [5:]
('sat', 'sun')
accessing the first five elements
>>> days [:5]
('mon', 'tue', 'wed', 'thu', 'fri')
Accessing elements that appears after # counting five from rear end
>>> days [:-5]
('mon', 'tue')
Accessing five elements from the rear
>>> days [-5:]
('wed', 'thu', 'fri', 'sat', 'sun')
Accessing elements from the start to end (every element)
>>> days [:]
('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')
Comments
Post a Comment