The position starts from 0 and first element value is my_tuple[0], so the data at my_tuple[1] gives us Ronald.
my_tuple=('a','c','d','c','x','k','d','c')
print("first position : ",my_tuple[0])
print("Last position : ",my_tuple[-1])
print("third position : ",my_tuple[2])
print("All from 2nd postion : ",my_tuple[1:])
print("All from first position : ",my_tuple[0:])
print("All from 3rd position : ",my_tuple[2:])
print("All from 4th position to 6th position : ",my_tuple[3:6])
Output
first position : a
Last position : c
third position : d
All from 2nd postion : ('c', 'd', 'c', 'x', 'k', 'd', 'c')
All from first position : ('a', 'c', 'd', 'c', 'x', 'k', 'd', 'c')
All from 3rd position : ('d', 'c', 'x', 'k', 'd', 'c')
All from 4th position to 6th position : ('c', 'x', 'k')
Single element tuple
We used type() to get the data type of the object. Note that we must use one trailing comma when there is only one element in our tuple.
my_tuple=('abc') # not a tuple
print(type(my_tuple))
my_tuple=('abc',) # it is a tuple
print(type(my_tuple))
Output
<class 'str'>
<class 'tuple'>
Add , remove , update elements
We can't add or remove elements of tuple. There is no append() or insert() method in tuple
AttributeError: 'tuple' object has no attribute 'append'
Tuples are immutable i.e we can't change the elements.
However we can use + operator to add element but that will create a new tuple. We can check this by using id() . After adding we will get new id for the tuple ( object )
my_tuple=(8,2,4,3,7,3)
print("Maximum value : ",max(my_tuple))
print("Minimum value : ",min(my_tuple))
print("Sum of all values : ",sum(my_tuple))
print("Number of elements : ",len(my_tuple))
Output
Maximum value : 8
Minimum value : 2
Sum of all values : 27
Number of elements : 6
Number of elements in the tuple ( string elements )
my_tuple=('Alex','Ronald','John')
print("Number of elements: ",len(my_tuple))
Output is here
Number of elements: 3
Deleting tuple
my_tuple=('Alex','Ronald','John')
del my_tuple
print("Number of elements: ",len(my_tuple))
The last line will create error as we are deleting the tuple before this line. Read more on del keyword.
Methods
Tuple supprts two methods count(x) and index(x). Here x the element or value to check.
Number of occurrence
count()
my_tuple=(5,7,5,4,3,5)
print ("Number of times 5 is : ",my_tuple.count(5) )
Output is here
Number of times 5 is : 3
Position of element
index()
my_tuple=(5,7,5,4,3,5)
print ("Position of 3 is : ",my_tuple.index(3) )
my_str=''
for element in my_tuple:
my_str=my_str + element
print(my_str)
List of Tuples
Create tuples against each day for last 3 days and future 3 days. Keep these tuples inside a List.
from datetime import date,timedelta
my_data=[] # List to store data
dt=date.today() # Todays date as base date
for delta in range(-3,3):
dt2=dt + timedelta(days=delta) # new date object
## One Tuple with three elements is added to list
my_data.append((delta,dt2.strftime('%Y-%m-%d'),dt2.strftime('%b-%Y')))
print(my_data) # to check the list with Tuples
Output ( this format is used to import or export records from database table. )