my_tuple=tuple()
print(type(my_tuple))
Output: (There is no point in creating blank tuple as we can't add data.)
<class 'tuple'>
my_tuple=('Alex','Ronald','John')
print(my_tuple)
Output
('Alex', 'Ronald', 'John')
x=range(5,50,10)
my_tuple=tuple(x)
print(my_tuple)
Output
(5, 15, 25, 35, 45)
my_tuple=(4,2,5,1,3)
print(*my_tuple)
Output
4 2 5 1 3
my_tuple=('a','b','c')
print(type(my_tuple)) # <class 'tuple'>
my_list=list(my_tuple)
print(type(my_list)) # <class 'list'>
my_list=['a','b','c']
print(type(my_list)) # <class 'list'>
my_tuple=tuple(my_list)
print(type(my_tuple)) # <class 'tuple'>
my_tuple=('Alex','Ronald','John')
for i in my_tuple:
print(i)
Output is here
Alex
Ronald
John
my_tuple=("Alex","Ronald","John")
print(my_tuple[1])
Output is here
Ronald
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')
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'>
AttributeError: 'tuple' object has no attribute 'append'Tuples are immutable i.e we can't change the elements.
my_tuple=(5,7,5,4,3,5)
print(id(my_tuple)) # 139787173397704
my_tuple +=(9,8)
print(id(my_tuple)) # 139787174601112
print(my_tuple) #(5, 7, 5, 4, 3, 5, 9, 8)
my_tuple=('Alex','Ronald','John')
if 'Ronald' in my_tuple:
print("Yes, Present in Tuple")
else:
print("No , not present in Tuple")
Output is here
Yes, Present in Tuple
We can use len(), max(), min(),sum() functions using a tuple as input.
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
my_tuple=('Alex','Ronald','John')
print("Number of elements: ",len(my_tuple))
Output is here
Number of elements: 3
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.
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
index()
my_tuple=(5,7,5,4,3,5)
print ("Position of 3 is : ",my_tuple.index(3) )
Output
Position of 3 is : 4
my_tuple=('Welcome ','to ','plus2net')
my_str=''.join(my_tuple)
print(my_str) # Welcome to plus2net
Using format()
my_str = '{}{}{}'.format(*my_tuple)
Using for loop.
my_str=''
for element in my_tuple:
my_str=my_str + element
print(my_str)
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. )
[(-3, '2024-06-19', 'Jun-2024'), (-2, '2024-06-20', 'Jun-2024'),
(-1, '2024-06-21', 'Jun-2024'), (0, '2024-06-22', 'Jun-2024'),
(1, '2024-06-23', 'Jun-2024'), (2, '2024-06-24', 'Jun-2024')]
Python Questions on Tuple with solutions
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.