bhylu.com

Advertisements

Join Tuples

Adding tuples makes a new tuple with the elements of both inputs. It preserves order and duplicates.

Understand the idea

The input tuples remain unchanged. Repetition with * repeats references to the elements; it does not deep-copy nested objects. A tuple can also be built from an iterable with tuple(...), which consumes that iterable. A generator input may be exhausted after that conversion.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

A worked example

Python

core = ("HTML", "CSS")
extra = ("Python",)
print(core + extra)
print(extra * 2)

Read the result

The outputs are ('HTML', 'CSS', 'Python') and ('Python', 'Python').

Try one change

Join three short tuples, then explain why a set would lose the same positional contract.