Labs ICT
โญ Pro Login

Tuples

Python Tuples

A tuple is an ordered, immutable sequence written with parentheses (). Once created, its items cannot be changed.

Creating Tuples

Tuples can hold any data type. A single-element tuple needs a trailing comma.

colors = ("red", "green", "blue")
single = (5,)
print(colors[1])
print(len(colors))
Try it Yourself โ†’

Tuple Unpacking

Assign tuple elements to multiple variables in one line.

point = (4, 7)
x, y = point
print(f"x={x}, y={y}")

data = ("Alice", 25, "Engineer")
name, age, job = data
print(name, age, job)
Try it Yourself โ†’

Tuple Methods

Tuples have only two built-in methods: count() and index().

nums = (1, 3, 3, 3, 5, 7)
print(nums.count(3))
print(nums.index(5))
Try it Yourself โ†’

๐Ÿงช Quick Quiz

What makes a tuple different from a list?