Python: Tuple [Data Type]

Tuple is an immutable and ordered collection of items. A tuple is similar to a list, but the items can not be changed, once the tuple is created.

Define a Tuple

Case #1: Standard Tuple Definition

big_box_tpl = ("First", "Second", "test", 123, 456, 789, "last")

print(big_box_tpl)
print(type(big_box_tpl))
print(len(big_box_tpl))
print(dir(big_box_tpl))
Python

Output:

('First', 'Second', 'test', 123, 456, 789, 'last')

<class 'tuple'>

7

['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
Plaintext

WARNING

If we try to change any item of a tuple, it will throw an error-

big_box_tpl = ("First", "Second", "test", 123, 456, 789, "last")

big_box_tpl[1] = "changed"
Python

Output:

Traceback (most recent call last):
  File "tuple.py", line 3, in <module>
    big_box_tpl[1] = "changed"
    ~~~~~~~~~~~^^^
TypeError: 'tuple' object does not support item assignment
Plaintext

Case #2: Just Comma Separating Items

We can define a tuple by just separating items with commas.

big_box_tpl = "Big", "Box", "Code"

print(big_box_tpl)
print(type(big_box_tpl))
print(len(big_box_tpl))
Python

Output:

('Big', 'Box', 'Code')

<class 'tuple'>

3
Plaintext

Item Indexing in Tuple

big_box_tpl = ("First", "Second", "test", 123, 456, 789, "last")

print(big_box_tpl[0])
print(big_box_tpl[2])
print(big_box_tpl[6])
print(big_box_tpl[-1])
Python

Output:

First
test
last
last
Plaintext

Looping through Tuple

Case #1

big_box_tpl = ("First", "Second", "test", 123, 456, 789, "last")

for item in big_box_tpl:
    print(item)
Python

Output:

First
Second
test
123
456
789
last
Plaintext

Case #2

big_box_tpl = ("First", "Second", "test", 123, 456, 789, "last")

# Start from index 0
item_index = 0

while item_index < len(big_box_tpl):
    print(f"Index: {item_index} || Item value: {big_box_list[item_index]}")
    
    # Increase the index by 1
    item_index += 1
Python

Output:

First
Second
test
123
456
789
last
Plaintext

Tuple Concatenation

big_box_tpl = ("First", "Second", "test", 123, 456, 789, "last")

tuple2 = ("new 1", "new 2", "new 3")

print(big_box_tpl + tuple2)
Python

Output:

('First', 'Second', 'test', 123, 456, 789, 'last', 'new 1', 'new 2', 'new 3')
Plaintext

Check Item Existence

big_box_tpl = ("First", "Second", "test", 123, 456, 789, "last")

print("First" in big_box_tpl)
print(123 in big_box_tpl)

print("123" in big_box_tpl)
print("Unknown" in big_box_tpl)
Python

Output:

True
True

False
False
Plaintext

Slicing a Tuple

big_box_tpl = ("First", "Second", "test", 123, 456, 789, "last")

print(big_box_tpl[0:4])
print(big_box_tpl[:4])
print(big_box_tpl[4:])
print(big_box_tpl[0:-1])
Python

Output:

('First', 'Second', 'test', 123)
('First', 'Second', 'test', 123)
(456, 789, 'last')
('First', 'Second', 'test', 123, 456, 789)
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.