Python Practice › Tuple Programs › Program 8
Add an item to a tuple
Write a Python program to add an item to a tuple.
Input
(18, 65, 3, 45), item 15
Output
(18, 65, 3, 45, 15)
Approach
- Tuples cannot change, so create a new tuple with
+.
Solution
t = (18, 65, 3, 45)t = t + (15,)print(t)Output
(18, 65, 3, 45, 15)
Line by line
Line 2
(15,) is a one-item tuple; the comma is required.
Learn the concept
New to this topic? Read the lesson, then come back.