Python - Tuples Concatenation and Repetition

Introduction

Tuples do not have all the methods that lists have.

They do support the usual sequence operations that we saw for both strings and lists:

Demo

print( (1, 2) + (3, 4) )            # Concatenation 
print( (1, 2) * 4 )                 # Repetition 
T = (1, 2, 3, 4)           # Indexing, slicing 
print( T[0], T[1:3] )

Result

Tuple syntax: Commas and parentheses

To create a single-item tuple, add a trailing comma after the single item, before the closing parenthesis:

Demo

x = (40)                   # An integer! 
print( x )
y = (40,)                  # A tuple containing an integer 
print( y )

Result