Python Basics — Creating Tuples
Creating Tuples
A tuple is an ordered, immutable sequence. Once created, you cannot add, remove, or change its elements.
Tuple Literal Syntax
# Parentheses (standard way)
colors = ("red", "green", "blue")
print(colors) # ('red', 'green', 'blue')
# Without parentheses (comma makes the tuple)
numbers = 1, 2, 3
print(numbers) # (1, 2, 3)
print(type(numbers)) # <class 'tuple'>
Single-Element Tuple Gotcha
A trailing comma is required for single-element tuples:
# ❌ NOT a tuple — just an integer in parentheses
not_a_tuple = (42)
print(type(not_a_tuple)) # <class 'int'>
# ✅ THIS is a tuple
single = (42,)
print(type(single)) # <class 'tuple'>
# Also works without parentheses
single = 42,
print(type(single)) # <class 'tuple'>
This is one of the most common Python beginner mistakes!
tuple() Constructor
Convert other iterables to tuples:
# From list
from_list = tuple([1, 2, 3])
print(from_list) # (1, 2, 3)
# From string
from_string = tuple("hello")
print(from_string) # ('h', 'e', 'l', 'l', 'o')
# From range
from_range = tuple(range(5))
print(from_range) # (0, 1, 2, 3, 4)
# From set (order not guaranteed)
from_set = tuple({3, 1, 2})
print(from_set) # (1, 2, 3) — may vary
Empty Tuples
empty1 = ()
empty2 = tuple()
print(len(empty1)) # 0
print(bool(empty1)) # False (empty = falsy)
Tuple Indexing
Tuples support the same indexing as lists:
fruits = ("apple", "banana", "cherry", "date")
# Positive indexing (0-based)
print(fruits[0]) # apple
print(fruits[2]) # cherry
# Negative indexing
print(fruits[-1]) # date
print(fruits[-2]) # cherry
# IndexError for out-of-range
# print(fruits[10]) # IndexError: tuple index out of range
Tuple Slicing
numbers = (0, 1, 2, 3, 4, 5)
print(numbers[1:4]) # (1, 2, 3)
print(numbers[:3]) # (0, 1, 2)
print(numbers[3:]) # (3, 4, 5)
print(numbers[::2]) # (0, 2, 4)
print(numbers[::-1]) # (5, 4, 3, 2, 1, 0)
Slicing a tuple returns a new tuple — the original is unchanged.
Tuples Are Immutable
point = (10, 20)
# ❌ Cannot modify
# point[0] = 99 # TypeError: 'tuple' object does not support item assignment
# del point[0] # TypeError: 'tuple' object doesn't support item deletion
# point.append(30) # AttributeError: 'tuple' object has no attribute 'append'
However, if a tuple contains a mutable object (like a list), the inner object can be modified:
mixed = (1, 2, [3, 4])
mixed[2].append(5)
print(mixed) # (1, 2, [3, 4, 5])
# The tuple itself didn't change — it still references the same list object
Mixed-Type Tuples
Tuples can hold any combination of types:
record = ("Alice", 30, True, 3.14, [1, 2])
print(record) # ('Alice', 30, True, 3.14, [1, 2])
Nested Tuples
matrix = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
print(matrix[1][2]) # 6
Common Operations
t = (1, 2, 3, 4, 5)
print(len(t)) # 5
print(min(t)) # 1
print(max(t)) # 5
print(sum(t)) # 15
print(3 in t) # True
print(9 in t) # False
Summary
- Tuples use parentheses or just commas:
(1, 2, 3)or1, 2, 3 - Single-element tuple requires trailing comma:
(42,) tuple()converts iterables to tuples- Indexing and slicing work like lists
- Tuples are immutable — no modifications after creation
- Mutable objects inside tuples can still be changed
Next up: Tuple Unpacking →