Add an item to a tuple

EasyTuple Programs · 8 of 70

Write a Python program to add an item to a tuple.

Advertisement
Input
(18, 65, 3, 45), item 15
Output
(18, 65, 3, 45, 15)

Approach

  1. Tuples cannot change, so create a new tuple with +.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs