fbpx
skip to content

Tuple Copy

A tuple is another sequence data type that is quite similar to the list. A Python tuple consists of a number of values that are separated by commas. Tuples and lists are different in the sense that the lists are enclosed in brackets ( [ ] ) whereas tuples are enclosed in parentheses ( ( ) ). Also, the elements and size of a list can be changed, unlike tuples which cannot be updated. Hence, tuples can be understood as read-only lists.

Example:

tuple=(‘abcd’,786,2.23,’john’,70.2 )
tinytuple=(123,’john’)
print(tuple)                       # Prints complete list
print(tuple[0])                   # Prints first element of the list
print(tuple[1:3])                # Prints elements starting from 2nd till 3rd
print(tuple[2:)])                 # Prints elements starting from 3rd element
print(tinytuple*2)             # Prints list two times
print(tuple+tinytuple)      # Prints concatenated lists

Output:

(‘abcd’, 786, 2.23, ‘john’, 70.2)
abcd
(786, 2.23)
(2.23, ‘john’, 70.2)
(123, ‘john’, 123, ‘john’)
(‘abcd’, 786, 2.23, ‘john’, 70.2, 123, ‘john’)

 

Please enter your email address to receive your cart as a PDF.